// 输入一个整数a,和一个正整数n,计算乘方a的n次方 Console.WriteLine("请输入底数:"); int a = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入指数:"); int n = Convert.ToInt32(Console.ReadLine()); int power = 1 for (int i = 0; i < n; i++) { power *= a; } Console.WriteLine(power);
题目3-乘方累加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 输入 q 与 n,求下面公式的结果: // q^0 + q^1 + q^2 + q^3 +...+ q^n Console.WriteLine("请输入q:"); int q = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入n:"); int n = Convert.ToInt32(Console.ReadLine()); int total = 1;
for (int i = 0; i < n; i++) { int power = 1; for (int j = 0; j < i; j++) { power *= q; } total += power; } Console.WriteLine("结果是" + total);
题目4-分数累加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 已知:Sn = 1 + 1/2 + 1/3 + ... + 1/n。显然对于任意整数k,都有当n足够大的时候,Sn > k. // 先输入一个整数K(1<=K<=15),要求计算出一个最小的n,使得Sn > K。 Console.WriteLine("请输入整数K:"); double K = Convert.ToDouble(Console.ReadLine()); double Sn = 0;
for (double n = 1.0; true; n++) { Sn += 1 / n; Console.WriteLine("n = {0}, Sn = {1}", n, Sn); if (Sn > K) { Console.WriteLine("当 Sn > K 时,n 为" + n); break; } }
题目5-人口
1 2 3 4 5 6 7 8 9 10
// 我国现有x亿⼈⼝,按照每年0.1%的增⻓速度,n年后将有多少⼈? Console.Write("请输入我国人口(单位:亿):"); double populataion = Convert.ToDouble(Console.ReadLine()); Console.Write("请输入时间(单位:年):"); int years = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < years; i++) { populataion += populataion * 0.1; } Console.WriteLine("{0}年之后,我国的人口是{1}亿。", years, populataion);
while (!R_yes) { Console.Write("请输入复合年利率 R(百分之0~20):"); R = Convert.ToDouble(Console.ReadLine()); if (R >= 0 && R <= 20) { R_yes = true; } } while (!M_yes) { Console.Write("请输入本金 M(100~1,000,000元):"); M = Convert.ToDouble(Console.ReadLine()); if (M >= 100 && M <= 1000000) { M_yes = true; } } while (!Y_yes) { Console.Write("请输入投资时间 Y(0~400年):"); Y = Convert.ToUInt16(Console.ReadLine()); if (Y >= 0 && Y <= 400) { Y_yes = true; } }
for (ushort years = 0; years < Y; years++) { M += M * (R / 100); } Console.WriteLine("最终金额为 {0:###.##} 元", M);
题目7-水仙花数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 寻找水仙花数 for (int num = 100; num < 1000; num++) { int a = num / 100; int b = num % 100 / 10; int c = num % 10; int result = a*a*a + b*b*b + c*c*c; if (result == num ) { Console.WriteLine( "{0}是水仙花数,其中\n{1}³+{2}³+{3}³ = {4}+{5}+{6} = {7}", num, a, b, c, a*a*a, b*b*b, c*c*c, result ); } }
题目8-依次输出
1 2 3 4 5 6 7 8
// 输出各位上的数 // 将输入值作为字符串逐位截取应该是更直观的方案 Console.Write("请输入一个整数:"); int num = Convert.ToInt32(Console.ReadLine()); while (num > 0) { Console.WriteLine(num%10); num /= 10; }
题目9-剔除零并依次输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 随机输⼊⼀个位数未知的整数,去除这个整数各个位上的0,形成新的数,并输出 // 输入的数字n大于0,小于1000000 Console.Write("请输入一个整数:"); uint num = Convert.ToUInt32(Console.ReadLine()); uint total = 0; uint singleNum; byte numIndex = 0; while (num > 0) { singleNum = (byte)(num % 10); num /= 10; if (singleNum > 0) { for (byte i = 0; i < numIndex; i++) { singleNum *= 10; } total += singleNum; numIndex++; } } Console.WriteLine(total);
// 数字和字符混合在⼀起了,作为⼀个优秀的挖掘⼈员,把输⼊的数字挖出来,并计算这些数字的和,并输出。输⼊以@作为结束。 char x; int total = 0; Console.WriteLine("请输入字符串,并按回车结束:"); do { x = (char)(Console.Read()); if (x >= '0' && x <= '9') { int y = x - '0'; total += y; } } while (x != '@'); Console.WriteLine(total);
题目13-钻石挖掘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 假设有个隧道,隧道以字符 ‘#’ 结束,挖矿的过程中,会遇到钻⽯ ‘*’ 和美⾦ ‘1’ ~ ‘9’ ,让矿⼯⼩六挖到隧道的尽头,假设每个钻⽯价值500美⾦,统计⼩六挖到了价值多少美⾦的收获? Console.WriteLine("创建隧道:"); char mine; int total = 0; while (true) { mine = (char)(Console.Read()); if (mine >= '0' && mine <= '9') { int metal = mine - '0'; total += metal; } elseif (mine == '*') { total += 500; } elseif (mine == '#') { break; } } Console.WriteLine(total);
题目14-因数
1 2 3 4 5 6 7 8 9 10 11
// 输入一个整数,输出该整数的因数个数和所有的因数 Console.Write("请输入一个整数:"); uint num = Convert.ToUInt16(Console.ReadLine()); uint factorTotal = 0; for (uint i = 1; i <= num; i++) { if (num % i == 0) { factorTotal++; Console.WriteLine("{0}是{1}的因数", i, num); } } Console.WriteLine("整数{0}有{1}个因数。", num, factorTotal);
题目15-质数
1 2 3 4 5 6 7 8 9 10
// 输入一个正整数,判断该数是否是质数 Console.Write("请输入一个整数:"); uint num = Convert.ToUInt16(Console.ReadLine()); uint factorTotal = 0; for (uint i = 2; i < num; i++) { if (num % i == 0) { factorTotal++; } } Console.WriteLine(factorTotal == 0 ? "yes" : "no");
题目16-奇数
1 2 3 4 5 6 7
// 输出1~100中的所有奇数 for (byte i = 1; i <= 100; i++) { if (i % 2 == 0) { continue; } Console.WriteLine(i); }