C语言基础题目练习
第1题 输入某年某月某日,判断是这一年的第几天
int everymonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int allday = 0;
struct date
{
int year,month,day;
}date1;
int fun_judge(int year,int month,int day)
{
int run = 0;
int monthday = 0;
int all = 0;
for(int i = date1.month - 1-1; i>=0; i--){
monthday += everymonth[i];
}
if(0 == year % 4) //判断闰年
{
all = day + monthday + 1;
printf("闰年\n");
}else
{
all = day + monthday;
}
return all;
}
//输入某年某月某日,判断是这一年的第几天?
int main()
{
printf("请输入年:");
scanf("%d",&date1.year);
printf("请输入月:");
scanf("%d",&date1.month);
printf("请输入日:");
scanf("%d",&date1.day);
printf("您输入的年月日为:%d年%d月%d日\n",date1.year,date1.month,date1.day);
allday = fun_judge(date1.year,date1.month,date1.day);
printf("这一年的第%d天\n",allday);
return 0;
}
第2题 打印3位数的水仙花数
打印所有水仙花数,水仙花数指一个三位数,其各位数字的立方和等于这个数本身。
int a=0,b=0,c=0;
int sum = 0;
for(int i =100; i<=999; i++)
{
a = i%10;
b = i/10%10;
c = i/100%10;
if( i == a*a*a + b*b*b + c*c*c)
{
printf("%d\n",i);
sum ++;
}
}
printf("水仙花数共有%d个。\n",sum);
return 0;