HDoj 2005 第几天?
Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20 2006/3/12
Sample Output
20 71
Author
lcy
Source
Recommend
1首先得注意的是 在本地dev-C++编译器上,在cpp语法中,定义以及使用,输入输出string类型时不加头文件#include<string>是可以运行成果的,结果也没有发现什么问题
但是在在线oj,例如HDoj上,使用string类型时,必须要加上头文件#include<string>否则会报错。
2判断闰年的方法 可以被4整除但不能被100整除,或者能被400整除的年份是闰年(能被400整除的叫世纪闰年)
3平年一年有365天,闰年一年有366天。平年二月28天,闰年二月29天
C++代码如下:
#include<stdio.h> #include<iostream> #include<math.h> #include<string> using namespace std; int main() { int m[13]; m[1]=31; m[2]=28; m[3]=31; m[4]=30; m[5]=31; m[6]=30; m[7]=31; m[8]=30; m[9]=31; m[10]=30; m[11]=31; m[12]=30; int year,month,day; string str; char temp[5]; int len,i,j; int n=0; while(cin>>str) { i=0; len=0; n=0; year=month=day=0; while(str[i]!='/') //将年份字符串赋值到temp字符数组中 { temp[len++]=str[i]; i++; } for(int j=0;j<len;j++) //将temp字符数组的年份转换到整型变量year中 year=year+((temp[len-j-1]-'0')*pow(10,j)); i++; //跳过一个'/'字符 len=0; while(str[i]!='/') //将月份字符串赋值到temp字符数组中 { temp[len++]=str[i]; i++; } for(int j=0;j<len;j++) //将temp字符数组的月份转换到整型变量month中 month+=(temp[len-j-1]-'0')*pow(10,j); i++; //跳过一个'/'字符 len=0; while(str[i]!='\0') //将日期字符串赋值到temp字符数组中 { temp[len++]=str[i]; i++; } for(int j=0;j<len;j++) //将temp字符数组中的日期转换到整型变量day中 day+=(temp[len-j-1]-'0')*pow(10,j); if( (year%4==0&&year%100!=0) || year%400==0 ) //判断是否是闰年 ,如果是闰年则二月为29天,如果是平年则是28天 m[2]=29; else m[2]=28; for(int i=1;i<month;i++) //统计天数 n+=m[i]; n+=day; printf("%d\n",n); } }
浙公网安备 33010602011771号