• 大小: 33KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-29
  • 语言: C#
  • 标签: 最少钱币  

资源简介

设有n种不同面值的硬币,各硬币的面值存于数组T〔1:n〕中。现要用这些面值的硬币来找钱。可以使用的各种面值的硬币个数存于数组Coins〔1:n〕中。 对任意钱数0≤m≤20001,设计一个用最少硬币找钱m的方法。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace coinsanswer
{
    public partial class Form1 : Form
    {
        int[] t = new int[5];
        int [] sum=new int [55];
        int i = 0;
        int k = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender EventArgs e)
        {   
             t[0]=Convert .ToInt16 ( label2 .Text); 
             t[1]= Convert .ToInt16 (label3 .Text);
             t[2]= Convert .ToInt16 (label4 .Text);
             t[3]= Convert .ToInt16 (label5 .Text);
             t[4]= Convert .ToInt16 (label6 .Text);
            //t有序
            
                   label1.Text = coin(t Convert.ToInt16(textBox1.Text) sum ).ToString();
        }

        public int coin(int[] t int c int[] sum)
        {
            int temp = 0 ys = c result = k ;
            
            if (i >(t.Length-1))  return k;
             
            if (result == 0)
                { 
                for (int s = 0; s <= t.Length-1; s++)
                    if ((ys / t[s] == 1) && (ys%t[s]==0)) return 1; 
                }
                if ((ys < t[0]) ||( ys > t[0] && ys < t[1])) { textBox2.Text = “无解“; return 0; }     
               
               else
                    {
                        
                        sum[i i] = ys / t[i];
                        temp = ys % t[i];
                        k = sum[i i];
                        
                    if ((i == 0)&&(temp==0)) { ++i; coin(t ys sum); } //全部用最小硬币找
                   
                         while (i                        {    

                            for (int j = i - 1; j >= 0; j--)//找钱
                            {
                                
                                if (temp >= t[j])
                                {
                                    sum[i j] = temp / t[j];
                                    temp = temp % t[j];
                                }
                                else
                                    sum[i j] = 0;
                                    k += sum[i j];
                                
                            }

                            if ((temp > 0) && (sum[i i] > 1))//不能以这种方式找钱,直到找到一个可行方式
                            {
                                sum[i i]--; k = sum[i i]; temp = ys % sum[i i]; continue;
                            }
                            else
                            {

                                if ((temp == 0) && (k < result))//能以这种方式找钱,但不一定是最优的
                                {
                                    result = k; i++; break;
                                }
                                else { i++; break; }
           

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      20480  2010-09-15 18:22  coinsanswer\coinsanswer\bin\Debug\coinsanswer.exe

     文件      26112  2010-09-15 18:22  coinsanswer\coinsanswer\bin\Debug\coinsanswer.pdb

     文件       5632  2005-11-11 22:25  coinsanswer\coinsanswer\bin\Debug\coinsanswer.vshost.exe

     文件       3229  2010-09-09 19:54  coinsanswer\coinsanswer\coinsanswer.csproj

     文件       3264  2010-09-15 18:22  coinsanswer\coinsanswer\Form1.cs

     文件       7408  2010-09-12 16:37  coinsanswer\coinsanswer\Form1.Designer.cs

     文件       5814  2010-09-12 16:37  coinsanswer\coinsanswer\Form1.resx

     文件        296  2010-09-15 18:22  coinsanswer\coinsanswer\obj\coinsanswer.csproj.FileList.txt

     文件        842  2010-09-13 20:21  coinsanswer\coinsanswer\obj\Debug\coinsanswer.csproj.GenerateResource.Cache

     文件      20480  2010-09-15 18:22  coinsanswer\coinsanswer\obj\Debug\coinsanswer.exe

     文件        180  2010-09-13 20:21  coinsanswer\coinsanswer\obj\Debug\coinsanswer.Form1.resources

     文件      26112  2010-09-15 18:22  coinsanswer\coinsanswer\obj\Debug\coinsanswer.pdb

     文件        180  2010-09-12 08:54  coinsanswer\coinsanswer\obj\Debug\coinsanswer.Properties.Resources.resources

     文件        470  2010-09-09 19:33  coinsanswer\coinsanswer\Program.cs

     文件       1194  2010-09-09 19:33  coinsanswer\coinsanswer\Properties\AssemblyInfo.cs

     文件       2876  2010-09-09 19:33  coinsanswer\coinsanswer\Properties\Resources.Designer.cs

     文件       5612  2010-09-09 19:33  coinsanswer\coinsanswer\Properties\Resources.resx

     文件       1094  2010-09-09 19:33  coinsanswer\coinsanswer\Properties\Settings.Designer.cs

     文件        249  2010-09-09 19:33  coinsanswer\coinsanswer\Properties\Settings.settings

     文件        922  2010-09-09 19:33  coinsanswer\coinsanswer.sln

    ..A..H.     14336  2010-09-15 18:30  coinsanswer\coinsanswer.suo

     目录          0  2010-09-09 19:33  coinsanswer\coinsanswer\obj\Debug\TempPE

     目录          0  2010-09-12 16:03  coinsanswer\coinsanswer\bin\Debug

     目录          0  2010-09-15 18:22  coinsanswer\coinsanswer\obj\Debug

     目录          0  2010-09-09 19:33  coinsanswer\coinsanswer\bin

     目录          0  2010-09-12 08:54  coinsanswer\coinsanswer\obj

     目录          0  2010-09-09 19:33  coinsanswer\coinsanswer\Properties

     目录          0  2010-09-15 18:22  coinsanswer\coinsanswer

     目录          0  2010-09-09 19:33  coinsanswer

----------- ---------  ---------- -----  ----

............此处省略2个文件信息

评论

共有 条评论

相关资源