【问题标题】:Check if one person is 100 days older than the other in C检查一个人是否比 C 中的另一个人大 100 天
【发布时间】:2020-12-25 18:36:10
【问题描述】:

我的任务是检查小组中的一名学生是否比另一名学生大 100 天。学生人数上限为 1000 人。计算时应考虑闰年和月份天数等其他特征。我不知道为什么我的代码不起作用。如果我输入了错误的日期(2000 年 32 月 33 日),我会出现“输入错误”的无限循环。如果日期正确,则代码不会打印即使相差100天也可以。我是初学者,希望能帮到你。

#include <stdio.h>
int main() {
    int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int daysleap[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};

    int s, month, year, day, i, j = 0, totaldays, a[1000];
    do {
        printf("Number of students: ");
        scanf("%d", & s);
        if (s < 1 || s > 100)
            printf("Incorrect input");
    } while (s < 1 || s > 1000);

    for (i = 0; i < s; i++) {
        scanf("%d,%d,%d", & day, & month, & year);
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
            if (day < 1 || day > daysleap[month]) {
                printf("Incorrect input");
                i = i - 1;
                continue;
            } else if (month < 1 || month > 12) {
                printf("Incorrect input");
                i = i - 1;
                continue;
            } else {
                for (i = 1; i < year; i++) {
                    totaldays = 0;
                    totaldays += 366;
                }
                for (i = 1; i < month; i++)
                    totaldays += daysleap[i];
                totaldays += day;
                a[j] = totaldays;
                j++;
            }

        } else {
            if (day < 1 || day > days[month]) {
                printf("Incorrect input");
                i = i - 1;
                continue;
            } else if (month < 1 || month > 12) {
                printf("Incorrect input");
                i = i - 1;
                continue;
            } else {
                for (i = 1; i < year; i++) {
                    totaldays = 0;
                    totaldays += 365;
                }
                for (i = 1; i < month; i++)
                    totaldays += days[i];
                totaldays += day;
                a[j] = totaldays;
                j++;
            }
        }
    }

    for (i = 0; i < s; i++) {
        for (j = i; j < s; j++) {
            while (i < s && j < s) {
                if (i != j && (a[j] - a[i] == 100))
                    printf("Student %d is 100 days older from Student %d", j + 1, i + 1);
                if (i != j && (a[j] - a[i] == -100))
                    printf("Student %d is 100 days older from Student %d", i + 1, j + 1);
            }
        }

    }
}

【问题讨论】:

  • 请在发布前正确缩进您的代码。这是勉强可读的。并提供一个失败的测试用例,说明实际和预期的行为。
  • 我希望这没问题。 @klutt
  • @Michael 更好,但远非好。为你修好了。
  • 但是你真的应该学会调试。这可能会有所帮助:github.com/klutt/debug-small-c-programs

标签: arrays c loops


【解决方案1】:

这个

for (i = 1; i < year; i++) {
    totaldays = 0;
    totaldays += 366;
}

相当于这个

totaldays = 366;

你可能想这样做

totaldays = 0;
for (i = 1; i < year; i++) {
    totaldays += 366;
}

但是逻辑不正确。假设年份是 2000 年。这是闰年,但 1999 年不是。闰年应加 366,非闰年应加 365。

我不知道修复它是否足以让程序正常工作。无论如何,我渴望编码,我制作了一个程序,它依赖于日期之间的差异,而不是依赖于日期之间的差异,即距离 0 年 1 月 1 日的距离。检查这种方法。

#include <stdio.h>

#define N 50

int main() {

    int m[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int p[N][3]; // person: day, month, year
    int n; // number of people
    // if the date is in a leap year, then one of the two subscripts gets a value of 1
    // if the date is before the 29th February leap[][0] = 1, otherwise leap[][1] = 1
    int leap[N][2] = {};
    int tot[N]; // sum of days in the birth year
    int dif; // difference in days between two dates

    puts("How many people?");
    do {
        scanf("%d", &n);
    }
    while( (n < 2 || n > N) && puts("Try again...") );
    
    puts("Enter their birth days please.");
    
    for(int i = 0; i < n; i++) {
        
        if( scanf("%d%*c%d%*c%d", &p[i][0], &p[i][1], &p[i][2]) == 3 && p[i][0] > 0 && p[i][1] > 0 && p[i][1] < 13 ) {
            
            if( (!(p[i][2]%4) && p[i][2]%100) || !(p[i][2]%400) ) {
            
                if( p[i][1] == 0 || (p[i][1] == 2 && p[i][0] < 29) ) leap[i][0] = 1;
                else leap[i][1] = 1;
            }
            
            if( ( leap[i][1] && ((p[i][1] == 1 && p[i][0] > 29) || (p[i][1] != 1 && p[i][0] > m[p[i][0]-1])) ) || (!leap[i][1] && p[i][0] > m[p[i][0]-1]) ) {
                
                puts("Wrong input, try again");
                --i;
            }
        }
        else {
            puts("Wrong input, try again");
            --i;
        }
    }
    
    for(int i = 0; i < n; i++) {
            
        tot[i] = p[i][0];
            
        for(int j = 0; j < p[i][1]-1; j++ ) {
                
            tot[i] += m[j];
        }
    }
    
    for(int i = 0; i < n-1; i++) {
    
        if( p[i][2] - p[i+1][2] < - 1 ) dif = 999;
        else if( p[i][2] - p[i+1][2] > 1 ) dif = -999;
        else {
            dif = tot[i+1] - tot[i];
        
            if( leap[i][0] && leap[i+1][1] ) ++dif;
            else if( leap[i+1][0] && leap[i][1] ) --dif;
        
            if( p[i][2] - p[i+1][2] == - 1 ) dif += 365;
            else if( p[i][2] - p[i+1][2] == 1 ) dif -= 365;
        }
        
        if( dif > 100) printf("Student #%d is 100 days older than student #%d", i+1, i+2);
        else if( dif < 100) printf("Student #%d is 100 days older than student #%d", i+2, i+1);
    }
    
    return 0;
}

是的,使用 day[]month[]year[] 而不是 p[][0]p[][1]p[][2] 会更具可读性。

使用结构数组会更好,例如Date dates[N],其类型为Date,定义如下

typedef struct {
    int day, month, year;
    int leapPre = 0, leapPost = 0;
    int tot;
} Date;

leapPreleapPostleap[][0]leap[][1]

【讨论】:

  • 但是,问题是 diff 应该等于 100 而不是 >= 或
  • 是的,我有一个逻辑错误,现在再测试一下
  • 只有一个问题:有什么办法可以限制日、月、年的输入以逗号分隔?
  • 如果 day=29 表示闰年,这段代码会报错,例如 29 2 2000
  • 而且也没有给出正确的结果;对于我输入的任何日期,它都会打印出一个比另一个早 100 天
猜你喜欢
  • 2020-03-20
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
相关资源
最近更新 更多