很难找到答案,因为我不知道如何用英语表达!所以,
我正在尝试解决最长小睡问题:https://uva.onlinejudge.org/external/101/10191.pdf
我的代码正在工作,但我一直收到法官的错误答案,我认为问题是当我连续键入两个测试用例时。
I类型:
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作为一个新的测试用例被忽略了,我想这就是为什么我从法官那里得到了错误的答案
所以我想要我键入的第二个数字,它被我代码中的第二次扫描捕获,作为新的测试用例。我试图在我的switch案例中添加一个案例1:其中我强制测试用例为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
}