题解 | #今年的第几天?#

今年的第几天?

http://www.nowcoder.com/practice/ae7e58fe24b14d1386e13e7d70eaf04d

关键点

  • 判断是否是闰年: 400的倍数, 不是100但是是4的倍数
  • 存储每个月有所少天, 二维数组->闰年
  • 小心: 不要把本月的总天数也算上了

代码如下

#include <iostream>
#include <cstdio>
#include <math.h>
#include <string>

using namespace std;

//判断是否是闰年: 400的倍数, 不是100但是是4的倍数
bool run(int year){
    if((year % 100 != 0 && year %4 == 0) || year % 400 ==0){
        return true;
    }
    return false;
}

//存储每个月有所少天, 二维数组->闰年
int daynum [2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},
                      {31,29,31,30,31,30,31,31,30,31,30,31}};


int main(){
    int year, month, day;
    while(scanf("%d%d%d", &year, &month, &day)!=EOF){
        int isrun = 0;
        if(run(year)){
            isrun = 1;
        }
        
        int sum = 0;
        //! 小心: 不要把本月的总天数也算上了
        for(int i=0; i<month-1; i++){
            sum+=daynum[isrun][i];
        }
        sum+=day;
        cout<<sum<<endl;
    }
}
全部评论

相关推荐

09-01 11:31
门头沟学院 Java
buul:七牛云的吧,感觉想法是好的,但是大家没那么多时间弄他这个啊。。。不知道的还以为他是顶尖大厂呢还搞比赛抢hc,只能说应试者的痛苦考察方是无法理解的,他们只会想一出是一出
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务