UVa - 最长的午睡
UVa - Longest 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 中添加一个 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
}
好吧,很难找到答案,因为我不知道如何用英语表达这个问题!所以,
我正在尝试做最长的午睡问题: 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
}