题解 | #计算日期到天数转换#

计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded

#include <stdio.h>
int judgeyear(int x) {
    if (x % 4 == 0 && x % 100 != 0) {
        return 1;
    } else {
        if (x % 400 == 0)
            return 1;
        else
            return 0;
    }
}
int main() {
    int n, y, r;
    scanf("%d %d %d", &n, &y, &r);
    int count = 0;
    if (judgeyear(n) == 1) {
        for (int i = 1; i < y; i++) {
            switch (i) {
                case 1:
                    count += 31;
                    break;
                case 2:
                    count += 29;
                    break;
                case 3:
                    count += 31;
                    break;
                case 4:
                    count += 30;
                    break;
                case 5:
                    count += 31;
                    break;
                case 6:
                    count += 30;
                    break;
                case 7:
                    count += 31;
                    break;
                case 8:
                    count += 31;
                    break;
                case 9:
                    count += 30;
                    break;
                case 10:
                    count += 31;
                    break;
                case 11:
                    count += 30;
                    break;
                case 12:
                    count += 31;
                    break;
            }
        }
    } else {
        for (int i = 1; i < y; i++) {
            switch (i) {
                case 1:
                    count += 31;
                    break;
                case 2:
                    count += 28;
                    break;
                case 3:
                    count += 31;
                    break;
                case 4:
                    count += 30;
                    break;
                case 5:
                    count += 31;
                    break;
                case 6:
                    count += 30;
                    break;
                case 7:
                    count += 31;
                    break;
                case 8:
                    count += 31;
                    break;
                case 9:
                    count += 30;
                    break;
                case 10:
                    count += 31;
                    break;
                case 11:
                    count += 30;
                    break;
                case 12:
                    count += 31;
                    break;
            }
        }
    }
    printf("%d\n", count + r);
    return 0;
}

全部评论
小老弟,你的方法过时啦,给你看看我的哈哈哈
1 回复 分享
发布于 03-16 18:12 重庆

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务