Description
Gardon的18歲生日就要到了,他當然很開心,可是他突然想到一個問題,是不是每個人從出生開始,到達18歲生日時所經過的天數都是一樣的呢?似乎并不全都是這樣,所以他想請你幫忙計算一下他和他的幾個朋友從出生到達18歲生日所經過的總天數,讓他好來比較一下。
Input
一個數T,后面T行每行有一個日期,格式是YYYY-MM-DD。如我的生日是1988-03-07。
Output
T行,每行一個數,表示此人從出生到18歲生日所經過的天數。如果這個人沒有18歲生日,就輸出-1。
Sample Input
1
1988-03-07
Sample Output
6574
Description
Gardon的18歲生日就要到了,他當然很開心,可是他突然想到一個問題,是不是每個人從出生開始,到達18歲生日時所經過的天數都是一樣的呢?似乎并不全都是這樣,所以他想請你幫忙計算一下他和他的幾個朋友從出生到達18歲生日所經過的總天數,讓他好來比較一下。
Input
一個數T,后面T行每行有一個日期,格式是YYYY-MM-DD。如我的生日是1988-03-07。
Output
T行,每行一個數,表示此人從出生到18歲生日所經過的天數。如果這個人沒有18歲生日,就輸出-1。
Sample Input
1
1988-03-07
Sample Output
6574
#include <iostream>
using namespace std;
bool isrun(int a){
if(a%4==0&&a%100!=0||a%400==0)
return true;
else
return false;}
int main(){
int T;
cin>>T;
char a[100];
while(T&&T--){
cin>>a;
int b,c,d,i,j;
int t[3];
t[0]=(a[0]-'0')*1000+(a[1]-'0')*100+(a[2]-'0')*10+a[3]-'0';
t[1]=(a[5]-'0')*10+a[6]-'0';
t[2]=(a[8]-'0')*10+a[9]-'0';
if(isrun(t[0])&&t[1]==2&&t[2]==29&&!isrun(t[0]+18)){
cout<<-1;}
else{
if(isrun(t[0])&&isrun(t[0]+18)||isrun(t[0])&&!isrun(t[0]+18)&&t[1]<3||
!isrun(t[0])&&isrun(t[0]+18)&&t[1]>2){
c=366;}
else{
c=365;}
for(j=1;j<18;j++){
if(isrun(t[0]+j))
c+=366;
else{
c+=365;}}
cout<<c;}}}
求大佬找出錯誤
uj5u.com熱心網友回復:
這個是格式錯誤,注意空格和回車uj5u.com熱心網友回復:
看的好累啊1
#include <cstdio>
scanf("%d-%d-%d", &year, &month, &day); 就好了,不用那么多轉化
2 什么時候輸出-1,只要輸出的是2月29日一定是-1, 判讀年是浪費的
3 輸入的是否3月以后, 是就看下一年是否閏年決定增加多少;否則看當年是否閏年決定增加多少
#include <iostream>
#include <cstdio>
using namespace std;
bool isLeap(int a){
return (a%4==0&&a%100!=0) ||a%400==0 ;
}
int main(){
int T;
cin>>T;
while(T--){
int year, month, day;
scanf("%d-%d-%d", &year, &month, &day);
// 輸入年份是合理的, 如果是2月29那么必然只能四年一次生日,沒有18歲生日
if ( 2==month && 29 == day ) {
cout << -1 << endl;
continue;
}
int delta = month>2 ? 1 : 0; // 3月開始的話,下一年閏年才要考慮,否則當年
int nDays = 0;
for (int i=0; i<18; i++)
nDays += isLeap(year+i+delta) ? 366 : 365;
cout << nDays << endl;
}
return 0;
}
沒測驗, 可能有問題,但是至少看起來邏輯會清楚的多
uj5u.com熱心網友回復:
拋磚引玉
#include <iostream>
using namespace std;
int main()
{
char c;
int y, m, d, t, days = 0;
int cy;
cin >> t;
for (int i = 0; i < t; ++i)
{
cin >> y >> c >> m >> c >> d;
if (m == 2 && d == 29)
{
days = -1;
}
else
{
days = 0;
for (int i = 1; i <= 18; i++)
{
days += 365;
cy = (m < 3 ? y + i - 1 : y + i);
if (cy % 4 == 0 && (cy % 100 != 0 || cy % 400 == 0))
days++;
}
}
cout << days << '\n';
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/143552.html
標籤:C++ 語言
上一篇:C語言大一小白求助!!!
