文章

55

粉丝

57

获赞

11

访问

9.9k

头像
日期计算 题解:C++
P1051 中南大学机试题
发布于2024年3月7日 17:21
阅读数 192

#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;

struct Date{
    int year;
    int month;
    int day;
};

int main(){
    struct Date date;
    int day[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int sum=0;
    while(cin>>date.year>>date.month>>date.day){//判断日期输入是否合法
        if(date.month>=1&&date.month<=12 && date.day>=0&&date.day<=day[date.month]){
            bool Leap = date.year % 400 == 0 || (date.year % 100 != 0&&date.year%4==0);//判断是否为闰年
            if(Leap){
                day[2] = 29;
            }else{
                day[2] = 28;
            }
            for(int i = 1; i < date.month; i++){
                sum+=day[i];//把该月之前的天数都加上
            }
            sum += date.day; //加上本月天数
            cout<<sum<<endl;
            sum = 0;
        }else{
            cout<<"Input error!"<<endl;
        }
...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发