百文买鸡题目:
⽤100⽂买⼀百只鸡,其中公鸡,⺟鸡,⼩鸡,都必须要有,公鸡3⽂⼀只,⺟鸡5⽂⼀只,⼩鸡2⽂⼀只,请问公鸡、⺟鸡、⼩鸡要买多少只刚好凑⾜100⽂。
// x y z 公鸡、⺟鸡、⼩鸡
int count = 0;
for (int x = 1; x <= 100 / 3; x++) // 公鸡
{for (int y = 1; y < 100 / 5; y++) // ⺟鸡{for (int z = 1; z < 100 / 2; z++) // 小鸡{if (3 * x + 5 * y + z * 2 == 100) // 条件{count++;Console.WriteLine("公鸡{0}只,母鸡{1}只,小鸡{2}只", x, y, z);}}}
}
Console.WriteLine(count + "种方法");
水仙花数题目:
打印出所有的“⽔仙花数”,所谓“⽔仙花数”是指⼀个三位数,其各位数字⽴⽅等于该数本⾝。例如153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3,所以153是“⽔仙花数”。
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 100; i < n + 1; i++)
{int a = i / 100;// 百int b = i / 10 % 10;// 十 int c = i % 10;// 个if (a * a * a + b * b * b + c * c * c == i){Console.WriteLine("{0}是水仙花数。", i);}
}