题解 | #计算日期到天数转换#
计算日期到天数转换
http://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include<iostream> using namespace std; int main() { static int array[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int year,month,day; int ret = 0,flag = 0; while(cin>>year>>month>>day) { int i = 1; while(i < month)//计算第n-1月的天数 { ret += array[i]; i++; } ret += day;//计算第n月的天数 if(month > 2 && ((year % 4 == 0 && year % 100 != 0 )|| year % 400 == 0)) { ret += 1; //闰年的情况下加1天 } cout<< ret <<endl; } }