input problem
Moderator: Board moderators
-
- New poster
- Posts: 5
- Joined: Sat Sep 28, 2002 3:46 pm
input problem
Hello, I got trouble when I meet problems with input like this
1 2
6 7
8 9
my trouble is i must repeat read until when?
repeat
read(a);
read(b);
until ???
1 2
6 7
8 9
my trouble is i must repeat read until when?
repeat
read(a);
read(b);
until ???
It depends. Sometimes you're given a number of test cases, and you can do
[pascal]
for k:=1 to n do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]
When not, try something like
[pascal]
while not eof(input) do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]
Hope this helps.
[pascal]
for k:=1 to n do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]
When not, try something like
[pascal]
while not eof(input) do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]
Hope this helps.
Best regards,
Fernando N
Fernando N
i think you should be careful about read and eof. example in this case after the last read there is still a newline char in the input, and eof will still return false, but the next read will raise an exception (that will cause runtime error if not caught)
btw i'am not using pascal, but it's not really "fair" that pascal runtime error is not signaled to sender, just wrong answer. (i know it's different as c/c++ because pascal catches all exceptions and raises no signals)
btw i'am not using pascal, but it's not really "fair" that pascal runtime error is not signaled to sender, just wrong answer. (i know it's different as c/c++ because pascal catches all exceptions and raises no signals)
-
- New poster
- Posts: 5
- Joined: Sat Sep 28, 2002 3:46 pm
[quote="fpnc"]It depends. Sometimes you're given a number of test cases, and you can do
[pascal]
for k:=1 to n do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]
When not, try something like
[pascal]
while not eof(input) do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]
thanx, but in the line
while not eof(input)
what is input? it is a text file?
[pascal]
for k:=1 to n do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]
When not, try something like
[pascal]
while not eof(input) do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]
thanx, but in the line
while not eof(input)
what is input? it is a text file?
Input and Output are two "special" files which you don't open or close.
For example, let's have a problem which says "You'll have a lot of lines, in which there are two numbers a and b. Your output should be a+b for each couple".
[pascal]
program pXXX(input, output);
var a,b,sum:integer;
begin
while not eof(input) do begin
readln(a,b); { could be readln(input, a, b) also }
sum:=a+b;
writeln(sum); { could be writeln(output, sum) also }
end;
end.[/pascal]
So just use the "program" header as in this example, and don't care about input/output files. Just use input as the file for the keyboard, and output as the file for the console, and do not try to open or close them.
Good luck!
For example, let's have a problem which says "You'll have a lot of lines, in which there are two numbers a and b. Your output should be a+b for each couple".
[pascal]
program pXXX(input, output);
var a,b,sum:integer;
begin
while not eof(input) do begin
readln(a,b); { could be readln(input, a, b) also }
sum:=a+b;
writeln(sum); { could be writeln(output, sum) also }
end;
end.[/pascal]
So just use the "program" header as in this example, and don't care about input/output files. Just use input as the file for the keyboard, and output as the file for the console, and do not try to open or close them.
Good luck!
Best regards,
Fernando N
Fernando N
btw, just want to add that we still can open/close that special input/output file and direct them to standard input/output by using '' as parameter.fpnc wrote:Just use input as the file for the keyboard, and output as the file for the console, and do not try to open or close them.
for example:
[pascal]
assign(output,''); rewrite(output);
assign(input,''); reset(input);
[/pascal]
-
- New poster
- Posts: 5
- Joined: Sat Sep 28, 2002 3:46 pm
thanx.fpnc wrote:Input and Output are two "special" files which you don't open or close.
For example, let's have a problem which says "You'll have a lot of lines, in which there are two numbers a and b. Your output should be a+b for each couple".
[pascal]
program pXXX(input, output);
var a,b,sum:integer;
begin
while not eof(input) do begin
readln(a,b); { could be readln(input, a, b) also }
sum:=a+b;
writeln(sum); { could be writeln(output, sum) also }
end;
end.[/pascal]
So just use the "program" header as in this example, and don't care about input/output files. Just use input as the file for the keyboard, and output as the file for the console, and do not try to open or close them.
Good luck!
but what about with multiple input? How to handle that?
there are two ways:
the first, which i used, is:
or you can also use this:
the first, which i used, is:
Code: Select all
readln(input,count);
while (count>0) do
begin
readln(input); {read blank space}
.... {read input like usual problem}
count:=count-1;
end.
Code: Select all
readln(input,temp);
while not eof(input) do
begin
readln(input); {read blank space}
.... {read input like usual problem}
end.
Hi to all!
I'd like to resume discussion on input problems.
All advices above work well in case of known amount of numbers in the line. But if you need to read indefinite (may be very big) number of integers? Same problem with very long Strings.
I've got many WA, TLE, MLE with several "primitive" ACM probs (like 492-Pig Latin, 484 and other) and consider their source is incorrect reading of input.
Has anybody used long strings in Free Pascal, and what kind of them (PChar or AnsiStrings)? Is there some tricks with using of them?
I will appreciate any hints and help.
I'd like to resume discussion on input problems.
All advices above work well in case of known amount of numbers in the line. But if you need to read indefinite (may be very big) number of integers? Same problem with very long Strings.
I've got many WA, TLE, MLE with several "primitive" ACM probs (like 492-Pig Latin, 484 and other) and consider their source is incorrect reading of input.
Has anybody used long strings in Free Pascal, and what kind of them (PChar or AnsiStrings)? Is there some tricks with using of them?
I will appreciate any hints and help.
-
- New poster
- Posts: 12
- Joined: Sun Dec 07, 2003 10:45 am
-
- Experienced poster
- Posts: 169
- Joined: Wed Oct 31, 2001 2:00 am
- Location: Singapore
To SMBfromRU:
For Pig latin what I do is to read and process each word individually. In other words, read a stretch of letters, read a stretch of non-letters, read a stretch of letters, etc etc. You get the point. This also works for many other problems.
For 484 I suppose your problem is not with reading, but the memory you need to declare. Since no limits are given it is rather difficult to guage. I suggest using pointers in this case, so you only need as much memory as the test case needs. You could try using linked list or something similar to the realloc() function if C if it exists.
To ggggqqqqihc:
Ctrl+Z works well for me. Perhaps you could send me your code through pm so I can tell you what's wrong? The problem may be like what's discussed above, where you use read with eof.
For Pig latin what I do is to read and process each word individually. In other words, read a stretch of letters, read a stretch of non-letters, read a stretch of letters, etc etc. You get the point. This also works for many other problems.
For 484 I suppose your problem is not with reading, but the memory you need to declare. Since no limits are given it is rather difficult to guage. I suggest using pointers in this case, so you only need as much memory as the test case needs. You could try using linked list or something similar to the realloc() function if C if it exists.
To ggggqqqqihc:
Ctrl+Z works well for me. Perhaps you could send me your code through pm so I can tell you what's wrong? The problem may be like what's discussed above, where you use read with eof.