我是编程新手,我正试图解决在线网站的一些问题。其中一个问题阻止了我。 您可以在此处下载问题:https://uva.onlinejudge.org/external/101/10191.pdf
我不知道如何更改此代码以确定自己从控制台读取数据的位置。
这是我写的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 256
int second_time(char a[])
{
int hour1, minute1, time;
hour1 = a[6] - '0';
hour1 *= 10;
hour1 += a[7] - '0';
minute1 = a[9] - '0';
minute1 *= 10;
minute1 += a[10] - '0';
time = (60*hour1) + minute1;
return time;
}
int first_time(char b[])
{
int hour2, minute2, time;
hour2 = b[0] - '0';
hour2 *= 10;
hour2 += b[1] - '0';
minute2 = b[3] - '0';
minute2 *= 10;
minute2 += b[4] - '0';
time = (60*hour2) + minute2;
return time;
}
int main(void) {
char line[MAX_LINE];
int i, j, s, k;
int first, second, delta;
scanf("%i", &s);
getchar();
int max[4], max_hour[4];
int flag = 1;
for(k=0; k<4; k++)
{
max[k] = 0;
}
while(flag)
{
for(i=0;i<s-1; i++)
{
if(i%2 == 0)
{
scanf ("%[^\n]%*c", line);
second = second_time(line);
if(s%2 == 0)
{
i++;
}
}
scanf ("%[^\n]%*c", line);
first = first_time(line);
delta = first - second;
if (delta > max[k])
max_hour[k] = second;
max[k] = delta;
second = second_time(line);
}
}
for(k=0; k<4; k++)
{
if(delta < 60)
printf("Day #%i: the longest nap starts at %.2i:%.2i and will last for %i minutes.\n", k+1, max_hour[k]/60, max_hour[k]%60, max[k]);
else
printf("Day #%i: the longest nap starts at %.2i:%.2i and will last for %i hours and %i minutes.\n", k+1, max_hour[k]/60, max_hour[k]%60, max[k]/60, max[k]%60);
}
return 0;
}
答案 0 :(得分:1)
函数scanf()返回成功匹配的项目数。使用此返回值设置无限循环并检查是否已达到EOF。此外,对于这些问题,您通常应该按实例打印结果实例,您不需要将所有答案保存在向量中(您不知道它有多大)。
答案 1 :(得分:1)
我使用以下代码获得了AC
<强>代码:强>
#include <stdio.h>
int main()
{
int s, c, day = 0, z, cp, cb, m;
int f[480], beg, max;
int h1, m1, h2, m2;
int p1, p2;
char line[256];
while (scanf("%d\n", &s) != EOF) {
++day;
for (z = 0; z < 480; ++z) f[z] = 1;
beg = 0;
max = 0;
for (c = 0; c < s; ++c) {
scanf("%d:%d %d:%d", &h1, &m1, &h2, &m2);
fgets(line, 256, stdin);
p1 = (h1 - 10) * 60 + m1;
p2 = (h2 - 10) * 60 + m2;
for (cp = p1; cp < p2; ++cp) f[cp] = 0;
}
c = 0;
while (c < 480) {
if (f[c]) {
cb = c;
m = 0;
while (f[c] && c < 480) {
++c;
++m;
}
if (m > max) {
beg = cb;
max = m;
}
} else {
++c;
}
}
printf("Day #%d: the longest nap starts at %02d:%02d and will last for ", day, 10 + beg / 60, beg % 60);
if (max > 59) printf("%d hours and ", max / 60);
printf("%d minutes.\n", max % 60);
}
return 0;
}
答案 2 :(得分:0)
您假设时间表按排序顺序给出。它不是。因此,您需要对其进行排序,然后线性检查最大可用间隙。
何时停止阅读数据?
只需输入这样的输入
while(scanf("%d",&s)!=EOF)
{
read s schedule.
sort
get maximum free gap
****BINGO****
....
}
Note: sorting is not relevant to looping but to your AC solution it is.
struct schedule
{
int start;
int end;
}
struct schedule ss[MAX+1];
while(scanf("%d",&s)!=EOF)----> This is when you know you have to stop when it return EOF
{
First element of ss I would set as ss[0]={6000,6000}
last element of ss I would set as ss[s]={1080,1080}
Get s schedules and put them in ss
sort them
for each of the elements starting from 1 to s
get max ( ss[i].start - ss[i-1].end)
and corresponding end time.
get maximum free gap
****BINGO****
....
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 256
int second_time(char a[])
{
int hour1, minute1, time;
hour1 = a[6] - '0';
hour1 *= 10;
hour1 += a[7] - '0';
minute1 = a[9] - '0';
minute1 *= 10;
minute1 += a[10] - '0';
time = (60*hour1) + minute1;
return time;
}
int first_time(char b[])
{
int hour2, minute2, time;
hour2 = b[0] - '0';
hour2 *= 10;
hour2 += b[1] - '0';
minute2 = b[3] - '0';
minute2 *= 10;
minute2 += b[4] - '0';
time = (60*hour2) + minute2;
return time;
}
int main(void) {
char line[MAX_LINE];
int i, j, s, k;
int first, second, delta;
int max[200], max_hour[200];
int flag = 1;
for(k=0; k<200; k++)
{
max[k] = 0;
}
while(scanf("%i", &s)!=EOF)
{
getchar();
for(i=0;i<s-1; i++)
{
if(i%2 == 0)
{
scanf ("%[^\n]%*c", line);
second = second_time(line);
if(s%2 == 0)
{
i++;
}
}
scanf ("%[^\n]%*c", line);
first = first_time(line);
delta = first - second;
if (delta > max[k])
max_hour[k] = second;
max[k] = delta;
second = second_time(line);
}
for(k=0; k<s; k++)
{
if(delta < 60)
printf("Day #%i: the longest nap starts at %.2i:%.2i and will last for %i minutes.\n", k+1, max_hour[k]/60, max_hour[k]%60, max[k]);
else
printf("Day #%i: the longest nap starts at %.2i:%.2i and will last for %i hours and %i minutes.\n", k+1, max_hour[k]/60, max_hour[k]%60, max[k]/60, max[k]%60);
}
}
return 0;
}
typedef struct {
int st, ed;
} schedule;
int cmp(const void *i, const void *j) {
schedule *a, *b;
a = (schedule *)i, b = (schedule *)j;
return a->st <= b->st;
}
int main() {
int n, i, a, b, c, d, day = 0;
schedule TT[100];
while(scanf("%d", &n) !=EOF) {
for(i = 0; i < n; i++) {
scanf("%d:%d %d:%d", &a, &b, &c, &d);
getchar();
TT[i].st = a*60 + b;
TT[i].ed = c*60 + d;
}
qsort(TT, n, sizeof(schedule), cmp);
int beg = 600, ans = 0, point;
for(i = 0; i < n; i++) {
if(( TT[i].st-beg ) > ans)
ans = (TT[i].st-beg ), point = beg;
beg = TT[i].ed;
}
if((1080 - beg) > ans)
ans = (1080- beg), point = beg;
printf("Day #%d: the longest nap starts at ", ++day);
printf("%02d:%02d and will last for ", point/60, point%60);
if(ans >= 60)
printf("%d hours and ", ans/60);
printf("%d minutes.\n", ans%60);
}
return 0;
}