← Back to topics
Topic

Test problem

m
mbalunovic
Problem is so simple, but I
am getting TLE ( test case 4 ) every time when I submit my code.
a
adminModerator
The problem is indeed really simple. But it is still well designed, to make you think of all the cases.

The problem does not say that there will always be number 42! What if you reach the end of the file and still try to read numbers. then your scanf will wait forever!

Some people complained that the problem is too easy, but I'll keep it since, obviously some people didn't think of all the cases, and didn't solve it.
m
mbalunovic
But how I know when it is end of file?
Text says that input is read from the standard input, not from file.
A
Al3kSaNdaR
In pascal Keyboard is a "file". Probeably in C++ too. Just use EOF.
a
adminModerator
standard input can also be considered as a file.
m
mbalunovic
Can you please look at my last submission?
What is wrong ?
I cannot understand.
i
iggy91
I don't know about cin.eof(), but i used the fact that (cin >> a) returns false whet it reads EOF, and true otherwise.

So, this worked:

while (cin>>a && a!=42) { cout << a << endl; }
A
Al3kSaNdaR
Here is the solution in Pascal.

Program TestProblem;
Var
Ans:Integer;
Begin
While Not EOF Do
Begin
ReadLn(Ans);
If ( Ans = 42 ) Then Halt
Else WriteLn(Ans);
End;
End.


This is how you use keyboard as a file. ;)
A
Amtrix
This is too a solution:
while( scanf("%d",&p)!=EOF)
T
Tavo92
You can write it in C++ using eof:

while(!cin.eof())
{
cin >> a;
}
t
tgudlek
Or just while( scanf( "%d", &p ) == 1 ){}

scanf() returns number of successfully inputed parameters.