博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
已知一个日期和天数, 求多少天后的日期(是那个超时代码的AC版)
阅读量:4949 次
发布时间:2019-06-11

本文共 4246 字,大约阅读时间需要 14 分钟。

1 #include 
2 #include
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\n", s);101 // printf("%d %d %d\n", L_year, m2, L_month);102 103 }104 return 0;105 106 }
View Code

 

一个学姐路过,看到我的代码,认为没有那么麻烦,于是自己写了一个,

1 #include 
2 #include
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 else29 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 else38 break;39 }40 day+=sum;41 printf("%4d-%02d-%02d %s\n",year,month,day,s[week]);42 }43 return 0;44 }
View Code

跟我的执行时间等等一样,但长度明显短了许多。

 

转载于:https://www.cnblogs.com/the-one/p/3232626.html

你可能感兴趣的文章
Delphi通用的序列化代码
查看>>
Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays 二分
查看>>
设计模式:职责链模式(Chain Of Responsibility)
查看>>
stm32f429i disc usb cdc vcp 虚拟串口 example project (CubeMX Hal)
查看>>
Robust PCA via Outlier Pursuit
查看>>
实战练习细节(分行/拼接字符串/字符串转int/weak和copy)
查看>>
wddm 部署问题解决
查看>>
Strict Standards: Only variables should be passed by reference
查看>>
Slab-based Intersection
查看>>
将输入流转为字符串工具类
查看>>
hiho_offer收割18_题解报告_差第四题
查看>>
高斯消元
查看>>
AngularJs表单验证
查看>>
regasm.exe 注册dll
查看>>
什么是死锁,简述死锁发生的四个必要条件,如何避免与预防死锁
查看>>
静态方法是否属于线程安全
查看>>
fegin 调用源码分析
查看>>
Linux的基本命令
查看>>
02号团队-团队任务3:每日立会(2018-12-05)
查看>>
sql 语法大全
查看>>