用一张1元换1分、2分和5分的硬币
用C#求:用一张1元换1分、2分和5分的硬币,问有几种换法?每种换发中1分、2分和5分的硬币各几枚?新手的我觉得很难呀!
回复 楼主 Lyz贞贞
static void Main (string[] args){
ArrayList x_1 = new ArrayList();
ArrayList x_2 = new ArrayList();
ArrayList x_5 = new ArrayList();
int i, j, k;
int sum =0;
int count = 0;
for (i = 0; i < 101; i++)
{
for (j = 0; j < 51; j++)
{
for (k = 0; k < 21; k++)
{
sum = i + 2 * j + 5 * k;
if (sum == 100)
{
x_1.Add(i);
x_2.Add(j);
x_5.Add(k);
count++;
}
if(sum >100)
{
break;
}
}
}
}
Console.Write("一共有几种分法: ");
Console.WriteLine(count);
for (int g = 0; g < count; g++ )
{
Console.Write(x_1[g] + ", ");
Console.Write(x_2[g] + ", ");
Console.Write(x_5[g] + "; |");
if (g>0 && g % 6 == 0)
{
Console.WriteLine();
}
}
Console.ReadLine();
}