百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,总共只有100文钱,如何在凑够100只鸡的情况下刚好花完100文钱?
1 //公鸡g 2 //母鸡m, 3 //小鸡x 4 int g; 5 int m; 6 int x; 7 int biao=0; 8 9 for (g = 0; g <= 50; g++)10 {11 for(m=0;m<=100;m++)12 {13 for (x = 0; x <= 200; x++)14 {15 if ((g*2)+m+(0.5*x)==100&&g+m+x==100)16 {17 biao++;18 Console.WriteLine("这是第"+biao+"种买法,公鸡有{0},母鸡有{1},小鸡有{2}只",g,m,x);19 }20 }21 }22 }23 24 Console.ReadLine();
有1分钱,2分钱,5分钱的硬币,要组合出来2角钱,有几种组合方式,分别各多少个?
1 //设1分钱x个 2 // 2分钱y个 3 // 5分钱z个 4 int x; 5 int y; 6 int z; 7 int biao = 0; 8 int sum = 0; 9 for (x = 0; x <= 20; x++)10 {11 for (y = 0; y <= 10;y++ )12 {13 for (z = 0; z <= 4; z++)14 { 15 sum++;16 if (x + y*2 + z*5 == 20)17 {18 biao++;19 Console.WriteLine("这是第"+biao+"种买法,1分钱"+x+"个,2分钱"+y+"个,5分钱"+z+"个。");20 }21 22 }23 24 }25 }26 Console.WriteLine("共"+sum+"种组合");27 Console.ReadLine();