标签:华为
题目:算日期
输入一个日期,输出这是这一年的第几天。(题目中没有给出闰年的定义)
输入 20131231
输出 365
#include<stdio.h> #include<stdlib.h> #include<string.h> int months[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int y,m,d; int isLeapYear(int y) { if((y%4==0&&y%100!=0)|| y%400==0)return 1; else return 0; } int solve() { int ans=0; int i=1; while(i<m) ans+=months[i++]; ans+=d; return ans; } int main(int argc, char *argv[]) { char s[10]; while(~scanf("%s",s)) { char tmp[5]; strncpy(tmp,s,4); tmp[4]='\0'; y=atoi(tmp); strncpy(tmp,s+4,2); tmp[2]='\0'; m=atoi(tmp); strncpy(tmp,s+6,2); tmp[2]='\0'; d=atoi(tmp); if(isLeapYear(y)) { months[2]+=1; printf("%d\n",solve()); months[2]-=1; } else printf("%d\n",solve()); } return 0; }
标签:华为
原文地址:http://blog.csdn.net/wdkirchhoff/article/details/42122117