日历中的PE(演示文稿错误)

时间:2015-12-04 10:27:43

标签: c

输入随机年份和当年1月1日的星期几,然后输入您想要的月份作为输出。

输入= year, day of the week of Jan 1st(0-6 for sunday to saturday, and sunday is the beginning of a week), a random month

输出= the calendar of the month. if the input of weekday or month is illegal, output "ERROR"(no quotation mark)

示例输入

2004 4 9

示例输出

             1   2   3   4
 5   6   7   8   9  10  11
12  13  14  15  16  17  18
19  20  21  22  23  24  25
26  27  28  29  30

这是我的代码:

#include<stdio.h>

int main(){
    int year, day7, month;
    int firstday;
    int delay[12];
    int leap = 0;
    scanf("%d %d %d", &year, &day7, &month);
    if (year % 4 == 0 && year % 100 != 0)leap = 1;
    if (year % 400 == 0)leap = 0;
    if (day7<0|| day7>6|| month<1 || month>12){
        printf("%s\n", "ERROR"); return 0;
        //I tried both with and without \n here, both 1pe
    }
    for (int i = 1; i <= 12; i++){
        switch (i){
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:delay[i - 1] = 3; break;
        case 2:delay[i - 1] = leap; break;
        default:delay[i - 1] = 2; break;
        }
    }
    int days = 28 + delay[month - 1];
    firstday = day7;
    for (int i = 1; i < month; i++){
        firstday += delay[i - 1];
        if (firstday>6)firstday -= 7;
    }

    for (int i = 0; i < firstday; i++){
        printf("%4c", ' ');
    }
    for (int i = 0; i < days; i++){
        printf("%4d",i+1);
        if ((i + firstday+1) % 7 == 0)printf("%c", '\n');
    }


    return 0;
}
它是5ac,1pe。 正确答案如下

#include<stdio.h>
int main()
{
    int year,firstofyear, month;
    int firstofmonth;
    int days = 0; 
    int beforedays = 0;
    int m;
    int i;
    int j;
    int k;
    scanf("%d %d %d", &year, &firstofyear, &month);
    if (month<1 ||month>12||firstofyear<0||firstofyear>6)printf("ERROR");
    else{
        for (i = 1; i <= month; i++)
        {
            switch (i){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 2:
                if ((year % 4 == 0 && year % 100 != 0 )|| year % 400 ==0)
                    days = 29;
                else days = 28;
                break;
            default:
                days = 30;
            }
            if (i < month)
                beforedays += days;
        }
        k = firstofyear+beforedays % 7; 
        if (k>6)k = k - 7;
        if (k == 0)
            firstofmonth = 0;
        else
            firstofmonth = k;
        for (j = 0; j < firstofmonth; j++)
        {
            printf("%4c", ' ');
        }
            for (m = 1; m <= days; m++)
            {
                printf("%4d", m);
                if ((firstofyear+beforedays + m-1 ) % 7 == 6)
                    printf("\n");
            }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

闰年计算中存在错误:

if (year % 400 == 0)leap = 0; 

应该是

if (year % 400 == 0) leap = 1;