思路:按字符数组输入,再映射为整型;
/* 按yyyy-mm-dd格式输入一个日期,判断这是这一年的第几天 提示:'0'- 48就等于0,所以字符的数字减一个48就等于对应的数字 */ #include <stdio.h> #include<string.h> /*int func(int y){ int rs; rs=0; if(y%4==0&&y%100!=0||y%400==0){ rs=1; } return rs; } void main(int argc,char *argv[]) { int i,n,y,m,d,rs,days; n=10;days=0; char ch[n]; int num[n]; int mou[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; for(i=0;i<n;i++){ scanf("%c",&ch[i]); num[i]=ch[i]-48; } y=num[0]*1000+num[1]*100+num[2]*10+num[3]; if(num[5]==0){ m=num[6]; }else{ m=num[5]*10+num[6]; } if(num[7]==0){ d=num[9]; }else{ d=num[8]*10+num[9]; } rs=func(y); if(rs){ mou[2]++; } for(i=1;i<m;i++){ days+=mou[i]; } days+=d; printf("%d ",days); }*/ int func(int y){ int rs; rs=0; if(y%4==0&&y%100!=0||y%400==0){ rs=1; } return rs; } void main(int argc,char *argv[]) { int i,y,m,d,rs,days; days=0; int mou[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; scanf("%d-%d-%d",&y,&m,&d); printf("y=%d,m=%d,d=%d ",y,m,d); /*printf("y=%d,m=%d,d=%d ",y,m,d);*/ rs=func(y); if(rs){ mou[2]++; } for(i=1;i<m;i++){ days+=mou[i]; } days+=d; printf("%d ",days); }