《算法笔记》3.4小节——入门模拟->日期处理
B题跟A题类型一样,多组输入初始化刚开始不小心放在外面了,看了好久
这种在发生错误的时候结果会发生变化,单输入又没问题的,考虑一下是不是在新的一轮忘记了初始化
#include<stdio.h> #include<string.h> using namespace std; int month[13][2]={0,0,31,31,28,29,31,31,30,30,31,31,30,30,31,31,31,31,30,30,31,31,30,30,31,31}; bool same(char str1[],char str2[]) { int len=strlen(str1); if(len!=strlen(str2)) return false; for(int i=0;i<len;i++) if(str1[i]!=str2[i]) return false; return true; } int changemonth(char str[]) { if(same(str,"January")) return 1; if(same(str,"February")) return 2; if(same(str,"March")) return 3; if(same(str,"April")) return 4; if(same(str,"May")) return 5; if(same(str,"June")) return 6; if(same(str,"July")) return 7; if(same(str,"August")) return 8; if(same(str,"September")) return 9; if(same(str,"October"))return 10; if(same(str,"November"))return 11; return 12; } void shuchu(int day) { if(day==0) printf("Sunday "); else if(day==1) printf("Monday "); else if(day==2) printf("Tuesday "); else if(day==3) printf("Wednesday "); else if(day==4) printf("Thursday "); else if(day==5) printf("Friday "); else printf("Saturday "); } bool isrun(int year) { if((year%4==0 && year%100!=0)||(year%400==0)) return true; return false; } int main() { int day1,day2,year1,year2,ans; char month1[100],month2[100]; int num1,num2,data1,data2; bool xiaoyu=true; while(scanf("%d %s%d",&day2,month2,&year2)!=EOF) { xiaoyu=true; num1=8;year1=2019;day1=17; num2=changemonth(month2); data1=year1*10000+num1*100+day1; data2=year2*10000+num2*100+day2; if(data1>data2) { xiaoyu=false; int temp=data2; data2=data1; data1=temp; temp=year2; year2=year1; year1=temp; temp=num1; num1=num2; num2=temp; temp=day1; day1=day2; day2=temp; } int sum=0; while(year2-year1>1) { if(data1%10000<=229) { if(isrun(year1)) { sum+=366; if(data1%10000==229) { day1=1; num1=3; data1=year1*10000+num1*100+day1; } } else sum+=365; } else { if(isrun(year1+1)) sum+=366; else sum+=365; } year1++; } while(year1<year2||num1<num2||day1<day2) { day1++; sum++; if(day1>month[num1][isrun(year1)]) { day1=1; num1++; if(num1>12) { num1=1; year1++; } } } //printf("%d ",sum); //printf("%d ",xiaoyu); if(xiaoyu) ans=(sum+6)%7; else ans=(6-sum%7); shuchu(ans); } return 0; }