题目1:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
#include"stdio.h"
#define N 5
int fun(int n);
void main()
{
int a;
a = fun(N);
printf("the 5th people is %d years old\n", a);
}
int fun(int n)
{
if(n == 1)
return 10;
else
return 2+fun(n-1);
}
题目2:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字
#include"stdio.h"
void main()
{
int n=1, i, x, a, t;
printf("please input a number( <=5 digits ):");
scanf("%d", &x);
t = x;
while(t/10 != 0){
n++;
t = t/10;
}
if(n > 5){
printf("error\n");
return;
}
else
printf("%d\n", n);
t = x;
for(i = 1;i <= n; i++)
{
a = t%10;
printf("%d", a);
t = t/10;
}
}
题目3:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
#include"stdio.h"
void main()
{
int x, t, n=1;
printf("please input a five-digit:");
scanf("%d", &x);
t = x;
while(t/10 != 0){
n++;
t = t/10;
}
if(n == 5){
t = x;
if((t%10 == t/10000) && (t/10%10 == t/1000%10))
printf("%d is a palindrome.\n", x);
else
printf("%d is not a palindrome.\n", x);
}
else{
printf("data error\n");
return;
}
}
题目4:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。
总结:这里如果连用两个scanf时注意 scanf取字符默认以\n作为字符取尽的结尾, \n也是一个字符 他在scanf结束的时候是不会别取走的 会保存在缓冲区中 当下次输入字符时就会取到他
#include"stdio.h"
void main()
{
char i, j;
printf("please input the first letter of someday: ");
scanf("%c", &i);
getchar();
switch(i)
{
case 'M':printf("Today is Monday!\n");
break;
case 'W':printf("Today is Wednesday!\n");
break;
case 'F':printf("Today is Friday!\n");
break;
case 'T':printf("please input the second letter of someday:");
scanf("%c", &j);
getchar();
if(j == 'u')
printf("Today is Tuesday!\n");
else if(j == 'h')
printf("Today is Thursday!\n");
else
printf("data error\n");
break;
case 'S':printf("please input the second letter of someday:");
scanf("%c", &j);
getchar();
if(j == 'a')
printf("Today is Saturday!\n");
else if(j == 'u')
printf("Today is Sunday!\n");
else
printf("data error\n");
break;
default:printf("data error\n");
break;
}
}