|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
百度上计算1996-06-06到2018-07-26相差为8085天 下代码算得为8350天. 求助问题出在哪#include <stdio.h>
int main()
{
long count = 0; // count用于存放一共活了多少天
int year1, year2; // year1是你的生日年份,year2是今天的年份
int month1, month2;
int day1, day2;
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);
while (year1 <= year2)
{
days[1] = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
while (month1 <= 12)
{
while (day1 <= days[month1 - 1])
{
if (year1 == year2 && month1 == month2 && day1 == day2)
{
goto FINISH; // 跳出多层循环才被迫用goto语句
}
day1++;
count++;
}
day1 = 0;
month1++;
}
month1 = 1;
year1++;
}
FINISH: printf("你在这个世界上总共生存了%d天\n", count);
return 0;
}
本帖最后由 无符号整形 于 2018-7-26 20:18 编辑
#include <stdio.h>
int main()
{
long count = 0; // count用于存放一共活了多少天
int year1, year2; // year1是你的生日年份,year2是今天的年份
int month1, month2;
int day1, day2;
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);
while (year1 <= year2)
{
days[1] = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
while (month1 <= 12)
{
while (day1 <= days[month1 - 1])
{
if (year1 == year2 && month1 == month2 && day1 == day2)
{
goto FINISH; // 跳出多层循环才被迫用goto语句
}
day1++;
count++;
}
day1 = 1;
month1++;
}
month1 = 1;
year1++;
}
FINISH: printf("你在这个世界上总共生存了%d天\n", count);
while (1);
return 0;
}
嘿,老兄,日是从1开始的,你没有过过7月0日吧。。。
|
|