c#问题 编一个程序,把一张1元的钞票换成5分,2分和1分的硬币,要求每种至少一枚,并且所换硬币数不超过

71.编一个程序,把一张1元的钞票换成5分,2分和1分的硬币,要求每种至少一枚,并且所换硬币数不超过30枚。请问,有哪几种换法?(控制台应用程序,用for语句)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int Count5 = 0;
int Count2 = 0;
int Count1 = 0;
int Count = 0;
for (Count5 = 0; Count5 < 100; Count5++)
{
for (Count2 = 0; Count2 < 100; Count2++)
{
for (Count1 = 0; Count1 < 100; Count1++)
{
if (Count1 + Count2 * 2 + Count5 * 5 == 100)
{
if (Count1 != 0 && Count2 != 0 && Count5 != 0)
{
System.Console.WriteLine("一分硬币个数:{0},二分硬币个数:{1},五分硬币个数:{2}", Count1, Count2, Count5);
Count++;
}
}
}
}
}

System.Console.WriteLine("总共{0}种方法", Count);

System.Console.WriteLine("Press any key to exit!");

System.Console.ReadLine();
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-04
我自己写的,希望你能满意吧
class Program
{int a,b,c; //a为5分,b2分,c1分
static void main()
{
for(a=1;a<20;a++)
for(b=1;b<50;b++)
{int c=100-5*a-2*b;
if((a+b+c)<=30)
Console.Write(a,b,c);
}

}

}
第2个回答  2010-05-04
50000种
第3个回答  2010-05-04
ArrayList list = new ArrayList();
for (int i = 0; i <= 30; i++)
{
for (int j = 0; j <= 30-i; j++)
{
if (100 - i * 5 - j * 2<=30)
{
list.Add(i.ToString() + "," + j.ToString() + "," + (100 - i * 5 - j * 2).ToString());
}
}
}
其中 list.Count 就是换法的数量
相似回答