给一个不多于5位数的正整数,要求:1.求出它是几位数;2.分别打印出每一位数字;3.按逆序打印出各位数字,例如原数为321,应输入123。
string num = Console.ReadLine();
try
{
uint x = uint.Parse(num);
if (x < 100000)
{
if (x > 0 && x < 10)
{
Console.WriteLine("输入值是1位数");
uint x0 = x;
Console.WriteLine("顺序:{0}", x0);
Console.WriteLine("逆序:{0}", x0);
}
if (x >= 10 && x < 100)
{
Console.WriteLine("输入值是2位数");
uint x0 = x / 10;
uint x1 = x - 10 * x0;
Console.WriteLine("顺序:{0},{1}", x0, x1);
Console.WriteLine("逆序:{1},{0}", x0, x1);
}
if (x >= 100 && x < 1000)
{
Console.WriteLine("输入值是3位数");
uint x0 = x / 100;
uint x1 = (x - 100 * x0) / 10;
uint x2 = x - 100 * x0 - 10 * x1;
Console.WriteLine("顺序:{0},{1},{2}", x0, x1, x2);
Console.WriteLine("逆序:{2},{1},{0}", x0, x1, x2);
}
if (x >= 1000 && x < 10000)
{
Console.WriteLine("输入值是4位数");
uint x0 = x / 1000;
uint x1 = (x - 1000 * x0) / 100;
uint x2 = (x - 1000 * x0 - 100 * x1) / 10;
uint x3 = x - 1000 * x0 - 100 * x1 - 10 * x2;
Console.WriteLine("顺序:{0},{1},{2},{3}", x0, x1, x2, x3);
Console.WriteLine("逆序:{3},{2},{1},{0}", x0, x1, x2, x3);
}
if (x >= 10000 && x < 100000)
{
Console.WriteLine("输入值是5位数");
uint x0 = x / 10000;
uint x1 = (x - 10000 * x0) / 1000;
uint x2 = (x - 10000 * x0 - 1000 * x1) / 100;
uint x3 = (x - 10000 * x0 - 1000 * x1 - 100 * x2) / 10;
uint x4 = x - 10000 * x0 - 1000 * x1 - 100 * x2 - 10 * x3;
Console.WriteLine("顺序:{0},{1},{2},{3},{4}", x0, x1, x2, x3, x4);
Console.WriteLine("逆序:{4},{3},{2},{1},{0}", x0, x1, x2, x3, x4);
}
}
else
{
Console.WriteLine("输入值应不多于5位");
}
}
catch
{
Console.WriteLine("输入值应为正整数");
}
输出杨辉三角形
int[,] nums = new int[10, 10];
for (int i = 0; i < nums.GetLength(0); i++)
{
for (int j = 0; j <= i; j++)
{
if (j == 0 || i == j)
{
nums[i, j] = 1;
}
else
{
nums[i, j] = nums[i - 1, j - 1] + nums[i - 1, j];
}
}
}
for (int i = 0; i < nums.GetLength(0); i++)
{
for (int j = 0; j <= i; j++)
{
Console.Write(nums[i, j] + "\t");
}
Console.WriteLine();
}
用下面公式求π得近似值
π / 4 = 1 - 1 / 3 + 1 / 5 - 1 / 7 +···
直到最后一项的绝对值小于1e - 7(10的负7次方)为止。
int s = 1;
double pi = 0, n = 1, x = 1; //double类型,π是小数
while (Math.Abs(x) > 1e-7) //Math.Abs(x)表示x的绝对值
{
pi = pi + x;
n += 2;
s = -s;
x = s / n;
}
pi = pi * 4;
Console.WriteLine(pi);
求Fibonacci数列前40个数。这个数列有如下特点:第1,2两个数为1,从第三个数开始,该数是前两个数之和。
即 F1 = 1(n = 1)
F2 = 1(n = 2)
Fn = Fn - 1 + Fn - 2(n = 3)
int[] num = new int[40];
num[0] = 1;
num[1] = 1;
for (int i = 0; i < num.Length; i++)
{
if (i <= 1)
{
Console.WriteLine(num[i]);
}
else
{
num[i] = num[i - 1] + num[i - 2];
Console.WriteLine(num[i]);
}
}
输入两个正整数m和n,求其最大公约数和最小公倍数。
Console.WriteLine("输入正整数m");
int m = int.Parse(Console.ReadLine());
Console.WriteLine("输入正整数n");
int n = int.Parse(Console.ReadLine());
if (m < n) //使m大于n
{
m += n;
n = m - n;
m = m - n;
}
int a = m; //a 被除数
int b = n; //b 除数
int x;
while (b != 0) //辗转相除求最大公约数
{
x = a % b; //第一步大数除以小数,下一步永远都是除数除以余数。直到余数为0,此时的除数就是最大公约数。跳出循环,输出除数a
//所以这里的while循环的条件表达式也可以为 x != 0
a = b;
b = x;
}
Console.WriteLine("max:" + a);
Console.WriteLine("min:" + m * n / a); //最小公倍数等于两个数字乘积除以最大公约数
有一分数序列
2 / 1,3 / 2,5 / 3,8 / 5,13 / 8,21 / 13,···
求出这个数列的前20项之和。
double top = 2, bot = 1, sum = 0; //分子、分母
for (int i = 0; i < 20; i++) //前20项
{
sum += top / bot; //和
double temp = top;
top = bot + top; //分子等于上一个数的分子加分母
bot = temp; //分母等于上一个数的分子
}
Console.WriteLine(sum);