练习
1.输出0-999的水仙花数
水仙花数算法:一个数=它各位的立方和,例如:153=1 * 1 *1 + 5 * 5 * 5 + 3 * 3 *3
#include<stdio.h>
int main()
{
int i = 0;
int a, b, c;
for (i; i <= 999; i++)
{
a = i / 100;
b = (i - 100 * a) / 10;
c = i % 10;
if (a * a * a + b * b * b + c * c * c == i)
{
printf("%d\t", i);
}
}
return 0;
}
2.任意给出一个年、月、日,判断是这一年的第几天:
闰年算法:能被4整除且不能被100整除,或者能被400整除
如:2012.5.10是这一年的第131天
#include<stdio.h>
int main()
{
int years, month, day;
int sum;
printf("please input year month day:\n");
scanf_s("%d %d %d", &years, &month, &day);
switch (month)
{
case 1:
sum = day;
break;
case 2:
sum = 31 + day;
break;
case 3:
sum = 31 + 28 + day;
break;
case 4:
sum = 31 + 28 + 31 + day;
break;
case 5:
sum = 31 + 28 + 31 + 30 + day;
break;
case 6:
sum = 31 + 28 + 31 + 30 + 31 + day;
break;
case 7:
sum = 31 + 28 + 31 + 30 + 31 + 30 + day;
break;
case 8:
sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
break;
case 9:
sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
break;
case 10:
sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
break;
case 11:
sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
break;
case 12:
sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
break;
default:
printf("请重新输入日期!\n");
break;
}
if ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0))
{
sum+=1;
}
printf("%d %d %d 是一年中的第%d天!\n",years,month,day,sum);
return 0;
}