本帖最后由 1613551 于 2022-4-17 14:37 编辑
题目下图,就是计算你活了多少天的,我的程序逻辑上应该是和小甲鱼一模一样的,但是结果却和小甲鱼的天壤之别,为什么?小甲鱼的结果见第1张图,我的结果见第2张图
这个我的程序#include <stdio.h>
int main() {
int a, b, c;
int d, e, f;
int h = 0;
printf("请输入你的生日(如果1988-05-20):" );
scanf("%d-%d-%d", &a, &b, &c);
printf("请输入今年的日期(如2016-03-28):");
scanf("%d-%d-%d", &d, &e, &f);
for (; a <= d; a++) {
int g[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
g[1] = (a % 400 == 0 || (a % 4 == 0 & a % 100 == 0)) ? 29 : 28;
for (; b <= 12; b++) {
for (; c < g[b - 1]; c++) {
if (a == d && b == e && c == f) {
goto result;
}
h++;
}
c = 1;
}
b = 1;
}
result:
printf("你在这个世界上总共生存了%d天\n", h);
}
这个是小甲鱼的#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);
return 0;
}
for (; c < g[b - 1]; c++)
应该是 for (; c <= g[b - 1]; c++)
|