- 6.9数字黑洞
对于一个自然数, 若为偶数, 则把它除以2, 若为奇数,则把它乘以3加1, 经过如此有限次变换后, 总可以得到自然数值1. 编写程序, 输入一个自然数,求经过多少次变换可得到自然数1.例如:
输入:
9
输出:
19
输入:
63
输出:
107
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x;
int num=0;
scanf("%d",&x);
while(x!=1)
{
if(x%2==0)
{
x=x/2;
num++;
}
else
{
x=3*x+1;
num++;
}
}
printf("%d",num);
return 0;
}
- 4.13显示一个月的日历
输入一个月的1号是星期几以及该月的天数,显示该月的日历。例如输入2和30,表示该月的1号是星期二并且该月有30天,显示日历如下:
mon | tue | wed | thu | fri | sat | sun |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |