✋编程题
3n+1问题:对于任意⼤于1的⾃然数n,若n为奇数,将n编程3n+1,否则变成n的⼀半。经过若
⼲次这样的变化,n⼀定会最终变成1,⽐如,7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40
→ 20 → 10 → 5 → 16 → 8 → 4 →2 → 1
输⼊n,输出变换的次数。
⽐如输⼊3输出7 ,输⼊10,输出6
c# int n = Convert.ToInt32(Console.ReadLine());
int j = 0;
while (n != 1)
{
//变换
if (n % 2 == 1)
{
n = 3 * n + 1;
}
else
{
n /= 2;
}
j++;
Console.WriteLine("变换后的值为:" + n);
}
Console.WriteLine("变换的次数为:" + j);
✋编程题
2006年培养学员80000⼈,每年增⻓25%,请问按此增⻓速度,到哪⼀年培训学员⼈数将达到
20万⼈?
c# int number = 80000;
int year = 2006;
while( number<200000)
{
number = (int)(number * 1.25);//增加了一年
year++;
}
Console.WriteLine(year);
✋编程题
班上有若⼲名学⽣,输⼊学⽣的个数,然后输⼊每⼀个学⽣的年龄,计算出来平均年龄,保留到
⼩数点后两位,输出平均年龄。
样例输⼊:
2
20
19
样例输出
19.5
c# int stu = Convert.ToInt32(Console.ReadLine());//输入学生的个数
int i = 1;
int ageSum = 0;
while (i < stu + 1)
{
ageSum += Convert.ToInt32(Console.ReadLine());//输入每一个学生的年龄
i++;
}
double ave = 1.0*ageSum / stu;
// 4.5342 100 int 453 /100.0
ave = ((int)(ave * 100)) / 100.0;//保留小数点后2位
Console.WriteLine(ave);
✋编程题
输⼊⼀个整数n,输出1~n中的每个数,空格隔开。
c# int n = Convert.ToInt32(Console.ReadLine());
int i = 1;
while (i < n + 1)
{
Console.Write(i+" ");
i++;
}