1 #include <stdio.h> 2 #include <string.h> 3 int m2 = 29; 4 int judge_year(int x) 5 { 6 7 if(x % 400 == 0 || x % 4 == 0 && x % 100 != 0) 8 return 1; 9 else 10 return 0; 11 12 } 13 14 //extern int m2; 15 int calculate_year(int *x, int y) 16 { 17 18 //extern int m2 = ;//出现 cannot initialize extern variables with block scope错误,即不能在块作用域里声明全局变量 19 20 int span = 366; 21 while(*x - span >= 0){ 22 23 y++;/*天数满一年加1*/ 24 *x -= span;/*减去今年的天数*/ 25 span = judge_year(y) ? 366 : 365;/*判断下一年的天数*/ 26 27 } 28 m2 = judge_year(y) ? 29 : 28;/*判断几天后2月份的天数*/ 29 return y; 30 31 } 32 //extern int m2; 33 int calculate_month(int *x, int m) 34 { 35 36 int span = 31; 37 while(*x - span >= 0){ 38 39 m++; 40 *x -= span; 41 if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) 42 { 43 44 span = 31; 45 46 } 47 else if(m == 4 || m == 6 || m == 9 || m == 11) 48 { 49 50 span = 30; 51 52 } 53 else 54 { 55 56 span = m2; 57 58 } 59 60 } 61 return m; 62 63 } 64 65 extern int m2; 66 int main() 67 { 68 69 int year = 2000, month = 1, day = 1, L_year, L_month, sum, X_sum; 70 char s[10]; 71 //int m2=29; 72 while(scanf("%d", &sum) && sum != -1){ 73 74 day = 1; 75 X_sum = sum; 76 L_year = calculate_year(&sum, year); 77 L_month = calculate_month(&sum, month); 78 day += sum; 79 switch(X_sum % 7) 80 { 81 82 case 6: strcpy(s, "Friday"); break; 83 case 0: strcpy(s, "Saturday"); break; 84 case 1: strcpy(s, "Sunday"); break; 85 case 2: strcpy(s, "Monday"); break; 86 case 3: strcpy(s, "Tuesday"); break; 87 case 4: strcpy(s, "Wednesday"); break; 88 case 5: strcpy(s, "Thursday"); 89 90 } 91 printf("%d-", L_year); 92 if(L_month <= 9) 93 printf("0%d-", L_month); 94 else 95 printf("%d-", L_month); 96 if(day <= 9) 97 printf("0%d ", day); 98 else 99 printf("%d ", day); 100 printf("%s ", s); 101 // printf("%d %d %d ", L_year, m2, L_month); 102 103 } 104 return 0; 105 106 }
一个学姐路过,看到我的代码,认为没有那么麻烦,于是自己写了一个,
1 #include <stdio.h> 2 #include <string.h> 3 int judge_year(int x) 4 { 5 6 if(x % 400 == 0 || x % 4 == 0 && x % 100 != 0) 7 return 1; 8 else 9 return 0; 10 11 } 12 int main() 13 { 14 int year = 2000, month = 1, day = 1,sum,week,i; 15 int ans[12]={31,28,31,30,31,30,31,31,30,31,30,31},a[2]={365,366}; 16 char s[7][10]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"}; 17 while(scanf("%d", &sum) && sum != -1) 18 { 19 week=sum%7; 20 year = 2000, month = 1, day = 1; 21 while(sum>=a[judge_year(year)]) 22 { 23 sum-=a[judge_year(year)]; 24 year++; 25 } 26 if(judge_year(year)) 27 ans[1]=29; 28 else 29 ans[1]=28; 30 for(i=0;i<12;i++) 31 { 32 if(sum>=ans[i]) 33 { 34 sum-=ans[i]; 35 month++; 36 } 37 else 38 break; 39 } 40 day+=sum; 41 printf("%4d-%02d-%02d %s ",year,month,day,s[week]); 42 } 43 return 0; 44 }
跟我的执行时间等等一样,但长度明显短了许多。