又磕了一晚上,多点测试真的很烦 ,完全不知道错哪里,后来发现是我变量名命名不规范导致自己晕了填错了,其实思路还是对的
我觉得书上的做法也还行,但我不太喜欢用二维数组,所以拿以前写的算天数的程序改装了一下
代码如下:
#include <stdio.h> int isRun(int year) { if((year%4==0&&year%100!=0)||year%400==0)return 1; else return 0; } int runDay(int month,int day) { int mon[12]={31,29,31,30,31,30,31,31,30,31,30,31}; for(int i=0;i<month-1;i++){ // printf("%d\n",mon[i]); day=day+mon[i]; } return day; } int unrunDay(int month,int day) { int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31}; for(int i=0;i<month-1;i++){ // printf("%d\n",mon[i]); day=day+mon[i]; } return day; } int main(){ int t1,t2,y1,y2,m1,m2,d1,d2,sum; while (scanf("%d %d",&t1,&t2)!=EOF){ sum=1; if(t1>t2){ int tmp; tmp=t1; t1=t2; t2=tmp; } y1=t1/10000; y2=t2/10000; for(int i=y1;i<y2;i++){ if(isRun(i)==1)sum=sum+366; else sum=sum+365; } m1=t1%10000/100; m2=t2%10000/100; d1=t1%100; d2=t2%100; int a,b; if(isRun(y1)==1)a=runDay(m1,d1); else a=unrunDay(m1,d1); if(isRun(y2)==1)b=runDay(m2,d2); else b=unrunDay(m2,d2); sum=sum+b-a; // printf("%d %d %d\n",a,b,sum); printf("%d\n",sum); } return 0; }
把变量名命名的很简单的话自己早晚会晕的!猫猫头哭哭哭哭