hdu2005 第幾天?【C++】
阿新 • • 發佈:2018-07-10
數據格式 problem 整型 int 輸出 des print bottom NPU
Sample Input
1985/1/20
2006/3/12
第幾天?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 181543 Accepted Submission(s): 64444
Input 輸入數據有多組,每組占一行,數據格式為YYYY/MM/DD組成,具體參見sample input ,另外,可以向你確保所有的輸入數據是合法的。
Output 對於每組輸入數據,輸出一行,表示該日期是該年的第幾天。
Sample Output 20 71
1 #include<string.h> 2 #include<cstdio> 3 #include<stdlib.h> 4 using namespace std; 5 int main() 6 { 7 char s[100]; 8 int time[3]; 9 char * p; 10 11 12 while(scanf("%s",s)!=EOF) 13 { 14 intresult = 0; 15 int count = 0; 16 int month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 17 p = strtok(s,"/");//s為要拆分的字符串,必須是char *類型,""裏是分隔符,可以" */#"等 18 while(p != NULL)//拆分字符串 19 { 20 time[count++] = atoi(p);//將字符串轉化為整型 21 p = strtok(NULL,"/"); 22 }23 if(time[0]%400 == 0 || ( time[0]%4==0 && time[0]%100!=0 )) 24 { 25 month[2] = 29; 26 } 27 for(int i = 1;i < time[1];++i) 28 { 29 result += month[i]; 30 } 31 result += time[2]; 32 printf("%d\n",result); 33 34 } 35 return 0; 36 }
hdu2005 第幾天?【C++】