c - UVa - 最长的午睡

标签 c automation switch-statement scanf ansi

好吧,很难找到答案,因为我不知道如何用英语表达这个问题!所以,

我正在尝试做最长的午睡问题: https://uva.onlinejudge.org/external/101/10191.pdf

我的代码可以正常工作,但我不断收到法官的错误回答,我认为问题出在我连续输入两个测试用例时。

我输入:

1 
12:00 13:00 schedule A

所以:

Day #1: the longest nap starts at 13:00 and will last for 5 hours and 0 minutes.

但是如果我输入:

1
2

数字 2 作为一个新的测试用例被忽略了,我想这就是我从法官那里得到错误答案的原因

所以我希望我输入的第二个数字,即我代码中的第二个 scanf 捕获的数字是新的测试用例。我试图在我的 switch case 中添加一个 case 1: 我强制 testcase 是 initH 因为我输入的新测试用例被这个变量捕获但没有成功

    while(scanf("%d", &testcase) == 1) {

    int result = 0, start;

    if(testcase > MAXVALUE)  continue;
     //here I ignore testcase > 100
    if(testcase == 0) {ret = SCANF; start = STARTIME; result = WORK;}
     //if there's no testcase my longest nap will be the 8 hours! SCANF = 5

    for(i = 0; i < testcase; i++) {
        ret = scanf("%d:%d %d:%d %255[a-zA-Z ]", &initH, &initM, &fintH, &fintM, appoint);
         //variables: H(hour), M(minute), appointment

        switch (ret){
        case 5:
            schedule[i].start = initH*HOUR + initM; //struct here
            schedule[i].endin = fintH*HOUR + fintM; //to keep this data
            break;
        default:
            i = testcase;
            break;
        }

        if((initH < INIT) || (fintM + fintH*HOUR) > ENDTIME) {error++; break;}
         //10:00 < time < 18:00 
        if((initH*HOUR + initM) > (fintH*HOUR + fintM)) {error++; break;}
         //initial hour in a schedule < end time in a schedule

        while(getchar() != '\n');
    }

    if(error != 0) {error = 0; continue;} //if error then ignore everything!

最佳答案

好吧,我昨天一整天都被困在这个问题上,当我决定寻求帮助时,我想出了一个解决方案!其实很简单!

if(ret == 5){ //only if I have 5 arguments in my scanf!
    if((initH < INIT) || (fintM + fintH*HOUR) > ENDTIME) {error++; break;}
    //10:00 < time < 18:00 
    if((initH*HOUR + initM) > (fintH*HOUR + fintM)) {error++; break;}
    //initial hour in a schedule < end time in a schedule
}

关于c - UVa - 最长的午睡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38482058/

相关文章:

java - 使用 Java 框架的 GUI 桌面应用程序自动化

ubuntu - 运行特定的剧本

c++ - "crosses initialization of variable"仅当初始化结合声明时

使用 switch 语句将数字(最多 4 位)转换为单词(C 程序)

android - ScrollView 在 Android Studio 中显示内容但在应用程序中不显示

c - 大多数程序都可以运行,但我的数组没有按应有的方式打印

c - 如何为 EtherCAT 主站实现字符设备的用户空间实现?

c++ - 使用内存文件

c - pthreads create_pthread() 传递多个参数

linux - 为 kickstart 自动下载 redhat