我试图做最长的Nap 问题: 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中添加一个案例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!
答案 0 :(得分:0)
好吧,昨天我一直坚持这一点,当我决定寻求帮助时,我找到了解决方案!实际上非常简单!
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
}