考研机试真题--今天的第几天?--清华大学

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Void_worker/article/details/82194727

题目:
题目描述
输入年、月、日,计算该天是本年的第几天。
输入描述:
包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。
输出描述:
输入可能有多组测试数据,对于每一组测试数据,
输出一个整数,代表Input中的年、月、日对应本年的第几天。
示例1
输入
1990 9 20
2000 5 1
输出
263
122

链接:
https://www.nowcoder.com/practice/ae7e58fe24b14d1386e13e7d70eaf04d?tpId=40&tqId=21350&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking
代码:

#include<iostream>
using namespace std;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};

int main(){
    int year, month, day;
    int date;
    while(cin >> year >> month >> day){
        date = 0;
        if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            a[1] = 29;
        for(int i = 0; i < month - 1; ++i){
            date += a[i];
        }
        date += day;
        cout << date << endl;

    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Void_worker/article/details/82194727