JOJ 1042: Ambiguous Dates
来源:互联网 发布:腾讯视频连不上网络 编辑:程序博客网 时间:2024/06/05 19:15
这个题目的复杂性主要表现在细节太多了。其实不需要使用什么大的或者是复杂的算法。主要还是程序逻辑是否清晰。代码先贴出来。以后再写流程。
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- char *token1,*token2,*token3,date[12];
- char sep[]="///-.,";
- int n,sc=0,ambi_date[2048],number,y,m,d;
- unsigned short days[2][13]={
- 0,31,28,31,30,31,30,31,31,30,31,30,31,
- 0,31,29,31,30,31,30,31,31,30,31,30,31
- };
- const int debug=0;
- void solve();
- void parse(char *token1,char *token2,char *token3);
- void print();
- void d_parse(int d1,int d2,int d3);
- void countDays(int y,int m,int d);
- int precheck(char *year,char *month,char *day);
- int check(int y,int m,int d);
- int isLeapYear(int y);
- int main() {
- if(debug) {
- freopen("in.txt","r",stdin);
- freopen("out.txt","w",stdout);
- }
- scanf("%d",&n);
- while(n--) {
- scanf("%s",date);
- sc++; number=0;
- solve();
- }
- return 0;
- }
- void solve() {
- int len=strlen(date),i,j;
- /*
- token1=strtok(date,sep);
- if(token1 && strlen(token1)!=len) {
- token2=strtok(NULL,sep);
- token3=strtok(NULL,sep);
- if(token1&&token2&&token3)
- parse(token1,token2,token3);
- else { print(); return; }
- } */
- for(i=0;i<len;i++) if(!isdigit(date[i])) break;
- if(i<len) {
- y=m=d=0;
- for(j=0;j<i;j++)y=y*10+(date[j]-'0');
- for(j=i+1;date[j]!=date[i];j++)m=m*10+(date[j]-'0');
- for(j=j+1;j<len;j++)d=d*10+(date[j]-'0');
- countDays(y,m,d);
- countDays(y,d,m);
- countDays(m,y,d);
- countDays(m,d,y);
- countDays(d,y,m);
- countDays(d,m,y);
- } else {
- if(len==8) {
- d_parse(4,2,2);
- d_parse(2,4,2);
- d_parse(2,2,4);
- } else if(len==7) {
- d_parse(4,2,1);
- d_parse(4,1,2);
- d_parse(2,4,1);
- d_parse(2,1,4);
- d_parse(1,4,2);
- d_parse(1,2,4);
- } else if(len==6) {
- d_parse(4,1,1);
- d_parse(1,4,1);
- d_parse(1,1,4);
- d_parse(2,2,2);
- } else if(len==5) {
- d_parse(2,2,1);
- d_parse(2,1,2);
- d_parse(1,2,2);
- } else if(len==4) {
- d_parse(2,1,1);
- d_parse(1,2,1);
- d_parse(1,1,2);
- } else if(len==3) {
- d_parse(1,1,1);
- }
- }
- print();
- }
- void parse(char *token1,char *token2,char *token3) {
- int i;
- y=m=d=0;
- for(i=0;token1[i];i++) y=y*10+(token1[i]-'0');
- for(i=0;token2[i];i++) m=m*10+(token2[i]-'0');
- for(i=0;token3[i];i++) d=d*10+(token3[i]-'0');
- if(precheck(token1,token2,token3))
- countDays(y,m,d);
- if(precheck(token1,token3,token2))
- countDays(y,d,m);
- if(precheck(token2,token1,token3))
- countDays(m,y,d);
- if(precheck(token2,token3,token1))
- countDays(m,d,y);
- if(precheck(token3,token1,token2))
- countDays(d,y,m);
- if(precheck(token3,token2,token1))
- countDays(d,m,y);
- }
- void d_parse(int d1,int d2,int d3) {
- char tt1[12],tt2[12],tt3[12];
- int i=0,j;
- while(i<d1){tt1[i]=date[i];i++;}
- tt1[i]='/0'; j=0;
- while(i<d2+d1)tt2[j++]=date[i++];
- tt2[j]='/0';j=0;
- while(date[i])tt3[j++]=date[i++];
- tt3[j]='/0';
- parse(tt1,tt2,tt3);
- }
- void print() {
- int a,b,c;
- printf("Scenario #%d:/n",sc);
- debug?printf("date : %s/n", date):1;
- if(number==0)
- printf("Illegal date/n");
- else {
- for(a=1;a<number;a++) {
- c=ambi_date[a];
- for(b=a-1;b>=0&&ambi_date[b]>c;b--)
- ambi_date[b+1]=ambi_date[b];
- ambi_date[b+1]=c;
- }
- printf("%d/n",ambi_date[0]);
- for(a=1;a<number;a++)
- if(ambi_date[a]!=ambi_date[a-1])
- printf("%d/n",ambi_date[a]);
- number=0;
- }
- printf("/n");
- }
- void countDays(int y,int m,int d) {
- int i,f,dt=0,base=110246;
- if(!check(y,m,d)) return;
- if(y<100) {
- for(i=1700;i<=2200;i+=100)
- countDays(y+i,m,d);
- }
- if(y<1700) return;
- for(i=1700;i<y;i++) {
- if(isLeapYear(i)) dt+=366;
- else dt+=365;
- }
- f=isLeapYear(y);
- for(i=1;i<m;i++) dt += days[f][i];
- dt+=d;
- ambi_date[number++]=dt-base;
- }
- int check(int y,int m,int d) {
- int f;
- if(y>=100&&y<1700 || y>2299) return 0;
- if(m<1 || m>12) return 0;
- if(d<1 || d>31) return 0;
- f=isLeapYear(y);
- if(d>days[f][m]) return 0;
- return 1;
- }
- int isLeapYear(int y) {
- return ((y%4==0&&y%100!=0) || (y%400==0));
- }
- int precheck(char *year,char *month,char *day){
- int ly,lm,ld;
- ly=strlen(year);
- lm=strlen(month);
- ld=strlen(day);
- if(ly==3 || ly>=5) return 0;
- if(lm>=3)return 0;
- if(ld>=3)return 0;
- if(ly==4 && ly+lm+ld==8) {
- lm=0;
- for(ld=0;ld<ly;ld++) lm=lm*10+year[ld]-'0';
- if(lm<100) return 0;
- }
- return 1;
- }
- JOJ 1042: Ambiguous Dates
- Ambiguous Dates Gym-101522A
- ZOJ 1033 Ambiguous Dates (简单搜索)
- [La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017]Ambiguous Dates
- Dates
- 使用dates
- iphone Dates
- JOJ 2257
- joj 2319
- joj 2653
- JOJ 2687
- joj 2693
- joj 2443
- joj 1173
- joj 1003
- joj 1066
- joj 1182
- joj 1195
- BM算法(更好的字符串匹配算法)
- Sandy引擎学习笔记:摄影机
- Fax CED and CNG tones
- Linux 系统查看命令大全
- NS2初学笔记(二)之 ns2基本结构与交互问题
- JOJ 1042: Ambiguous Dates
- NS2初学笔记(三)之 最详细讲解ns2运行机制
- 一丝淡淡的牵挂 (转)
- 使用多媒体API枚举音频设备 - zgl7903的专栏 - CSDNBlog
- 终于手动做好了第一个稍微有的形状的MFC程序~一个简陋的图形绘图工具
- 收藏:消息中间件和JMS及其开发步骤
- Delphi写Sql2000扩展存储过程的例子
- 从VC6 VS2008遇到的问题总结
- TC图形界面下实现俄罗斯方块