水仙花数:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)
for (int a = 1; a <= 9; a++){for (int b = 0; b <= 9; b++){for (int c = 0; c <= 9; c++){if ((a * 100 + b * 10 + c) == a * a * a + b * b * b + c * c * c){Console.WriteLine("{0}{1}{2}", a, b, c);}}}}
for (int i = 100; i <= 999; i++){if (i == Math.Pow(i / 100, 3) + Math.Pow((i / 10) % 10, 3) + Math.Pow(i % 10, 3)){Console.WriteLine(i);}}
最大公倍数
try{Console.WriteLine("please input integer A");int a = Convert.ToInt32(Console.ReadLine());Console.WriteLine("please input integer B");int b = Convert.ToInt32(Console.ReadLine());for (int i = a; i >= 1; i--){if (a % i == 0 && b % i == 0){Console.WriteLine(i);return;}}}catch{Console.WriteLine("error");}
最大公倍数:辗转相除法
try{Console.WriteLine("please input integer A");int a = Convert.ToInt32(Console.ReadLine());Console.WriteLine("please input integer B");int b = Convert.ToInt32(Console.ReadLine());if (a < b){int temp = a;a = b;b = temp;}for (int c = 1;;){c = a % b;if (a == 0 || b == 0 || c == 0){Console.WriteLine(Math.Max(Math.Max(a, b), c));break;}a = c;b = b % c;if (a==0 || b==0 || c==0){Console.WriteLine(Math.Max(Math.Max(a,b),c));break;}}}catch{Console.WriteLine("error");}
