S1E18动动手第2题
本帖最后由 a89361461 于 2017-6-24 00:32 编辑如果出生日期输入31的话(如1988-05-31),程序就会无限循环下去。
代码如何改才能得到正确结果?
#include <stdio.h>
int main()
{
long count1 = 0, count2; // count用于存放一共活了多少天
int year1, year2, year3; // year1是你的生日年份,year2是今天的年份
int month1, month2, month3;
int day1, day2, day3;
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("请输入你的生日(如1988-05-20):");
scanf("%d-%d-%d", &year1, &month1, &day1);
printf("请输入今天的日期(如2016-03-28):");
scanf("%d-%d-%d", &year2, &month2, &day2);
year3 = year1 + 80;
month3 = month1;
day3 = day1;
while (1)
{
days = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
while (month1 <= 12)
{
while (day1 < days)
{
if (year1 == year2 && month1 == month2 && day1 == day2)
{
count2 = count1;
printf("你在这个世界上总共生存了%d天\n", count2);
}
if (year1 == year3 && month1 == month3 && day1 == day3)
{
printf("如果能活到80岁,你还剩下%d天\n", count1 - count2);
printf("你已经使用了%.2f\%的生命,请好好珍惜剩下的时间!\n", (double)count2 / count1 * 100);
goto FINISH;
}
day1++;
count1++;
}
day1 = 0;
month1++;
}
month1 = 0;
year1++;
}
FINISH: return 0;
} #include <stdio.h>
int main()
{
long count1 = 0, count2; // count用于存放一共活了多少天
int year1, year2, year3; // year1是你的生日年份,year2是今天的年份
int month1, month2, month3;
int day1, day2, day3;
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("请输入你的生日(如1988-05-20):");
scanf("%d-%d-%d", &year1, &month1, &day1);
printf("请输入今天的日期(如2016-03-28):");
scanf("%d-%d-%d", &year2, &month2, &day2);
year3 = year1 + 80;
month3 = month1;
day3 = day1;
while (1)
{
days = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
while (month1 <= 12)
{
while (day1 <= days)//这里改成<=
//如果你后面month1 = 0 ,这里month1 - 1会对吗?,当然有些编译器会自动修正为0(这跟问题无关)
{
if (year1 == year2 && month1 == month2 && day1 == day2)
{
count2 = count1;
printf("你在这个世界上总共生存了%d天\n", count2);
}
if (year1 == year3 && month1 == month3 && day1 == day3)
{
printf("如果能活到80岁,你还剩下%d天\n", count1 - count2);
printf("你已经使用了%.2f的生命,请好好珍惜剩下的时间!\n", (double)count2 / count1 * 100);
goto FINISH;
}
day1++;
count1++;
}
day1 = 1; //这里改成1
month1++;
}
month1 = 1;//month1 = 0; 首选还是这里,必须为1(这跟问题无关)
year1++;
}
FINISH: return 0;
} ba21 发表于 2017-6-24 10:39
#include
int main()
厉害了{:5_106:}
页:
[1]