已知一个日期和天数, 求多少天后的日期(是那个超时代码的AC版)

 #include <stdio.h>
#include <string.h>
int m2 = ;
int judge_year(int x)
{ if(x % == || x % == && x % != )
return ;
else
return ; } //extern int m2;
int calculate_year(int *x, int y)
{ //extern int m2 = ;//出现 cannot initialize extern variables with block scope错误,即不能在块作用域里声明全局变量 int span = ;
while(*x - span >= ){ y++;/*天数满一年加1*/
*x -= span;/*减去今年的天数*/
span = judge_year(y) ? : ;/*判断下一年的天数*/ }
m2 = judge_year(y) ? : ;/*判断几天后2月份的天数*/
return y; }
//extern int m2;
int calculate_month(int *x, int m)
{ int span = ;
while(*x - span >= ){ m++;
*x -= span;
if(m == || m == || m == || m == || m == || m == || m == )
{ span = ; }
else if(m == || m == || m == || m == )
{ span = ; }
else
{ span = m2; } }
return m; } extern int m2;
int main()
{ int year = , month = , day = , L_year, L_month, sum, X_sum;
char s[];
//int m2=29;
while(scanf("%d", &sum) && sum != -){ day = ;
X_sum = sum;
L_year = calculate_year(&sum, year);
L_month = calculate_month(&sum, month);
day += sum;
switch(X_sum % )
{ case : strcpy(s, "Friday"); break;
case : strcpy(s, "Saturday"); break;
case : strcpy(s, "Sunday"); break;
case : strcpy(s, "Monday"); break;
case : strcpy(s, "Tuesday"); break;
case : strcpy(s, "Wednesday"); break;
case : strcpy(s, "Thursday"); }
printf("%d-", L_year);
if(L_month <= )
printf("0%d-", L_month);
else
printf("%d-", L_month);
if(day <= )
printf("0%d ", day);
else
printf("%d ", day);
printf("%s\n", s);
// printf("%d %d %d\n", L_year, m2, L_month); }
return ; }

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

 #include <stdio.h>
#include <string.h>
int judge_year(int x)
{ if(x % == || x % == && x % != )
return ;
else
return ; }
int main()
{
int year = , month = , day = ,sum,week,i;
int ans[]={,,,,,,,,,,,},a[]={,};
char s[][]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
while(scanf("%d", &sum) && sum != -)
{
week=sum%;
year = , month = , day = ;
while(sum>=a[judge_year(year)])
{
sum-=a[judge_year(year)];
year++;
}
if(judge_year(year))
ans[]=;
else
ans[]=;
for(i=;i<;i++)
{
if(sum>=ans[i])
{
sum-=ans[i];
month++;
}
else
break;
}
day+=sum;
printf("%4d-%02d-%02d %s\n",year,month,day,s[week]);
}
return ;
}

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

上一篇:关于 jQuery中 function( window, undefined ) 写法的原因


下一篇:利用花生壳和IIS发布网页过程