for练习--凑法
static void Main14购物卡(string[] args) { //小明单位发了50元的购物卡,他到超市买洗化用品,一是牙刷(5元),二是香皂(2元),三是牙膏(10元)怎么可以正好把五十元花完。 for (int i = 0; i <= 10; i++) { for (int x = 0; x <= 25; x++) { for (int g = 0; g <= 5; g++) { if (i * 5 + x * 2 + g * 5 == 50) { Console.WriteLine("牙刷\t" + i + "个,香皂\t" + x + "个,牙膏\t" + g + "个"); } } } } }
static void Main15白文白鸡(string[] args) { //公鸡两文/只,母鸡一文/只,小鸡半文/只,花100文钱,买100只鸡,该如何购买? Console.WriteLine("买法有:"); int n = 0; for (int a = 0; a <= 50; a++) { for (int b = 0; b <= 100; b++) { for (int c = 0; c <= 200; c++) { if (a + b + c == 100 && a * 2 + b * 1 + c * 0.5 == 100) { n++; Console.WriteLine("公鸡" + a + "母鸡" + b + "小鸡" + c); } } } } Console.WriteLine("共有{0}种买法", n); }
static void Main16白马百担(string[] args) { //白马百担:大马驮2担粮食,中马驮1担粮食,两头小马驮1担粮食,要用100匹马,驮100担粮食,该如何调配? int n = 0; for (int a = 0; a <= 50; a++) { for (int b = 0; b <= 100; b++) { for (int c = 0; c <= 200; c++) { if (a + b + c == 100 && a * 2 + b * 1 + c * 0.5 == 100) { n++; Console.WriteLine("大马{0}\t中马{1}\t小马{2}", a, b, c); } } } } Console.WriteLine("共有{0}种驮法", n); }
static void Main17凑钱(string[] args) { //有1块、2块、5块的钱若干,凑出20块,有几种凑法? int n = 0; for (int a = 0; a <= 20; a++) { for (int b = 0; b <= 10; b++) { for (int c = 0; c <= 4; c++) { if (a * 1 + b * 2 + c * 5 == 20) { n++; Console.WriteLine("一块的{0},2块的{1},五块的{2}", a, b, c); } } } } Console.WriteLine("共有{0}种凑法", n); }