Escolar Documentos
Profissional Documentos
Cultura Documentos
Robust I/O
General advice
Bruce Merry
August 2013
Outline
1 A Sample Problem
Problem
Solution
2 Robust I/O
Whitespace
End of Input
3 General advice
Problem-solving
High-level strategy
Problem description
Problem description
Problem description
Input
Your input will consist of a number of records. Each record is a
sequence of integers in the range 2 to 232 1, terminated by
the value 0.
The last record in the input set will be terminated by the number
1.
5
7
21
9
11
101
13
0
65535
131071
-1
Input
Your input will consist of a number of records. Each record is a
sequence of integers in the range 2 to 232 1, terminated by
the value 0.
The last record in the input set will be terminated by the number
1.
5
7
21
9
11
101
13
0
65535
131071
-1
Output
For each input record, your output should be the largest prime
number in the sequence.
101
131071
Output
For each input record, your output should be the largest prime
number in the sequence.
101
131071
Time Limit
5 seconds
Outline
1 A Sample Problem
Problem
Solution
2 Robust I/O
Whitespace
End of Input
3 General advice
Problem-solving
High-level strategy
Solution Outline
Drilling Down
Testing
Testing
Testing
Testing
Testing
1
4294967291
2
-1
Outline
1 A Sample Problem
Problem
Solution
2 Robust I/O
Whitespace
End of Input
3 General advice
Problem-solving
High-level strategy
The Problem
The Problem
The Problem
The Problem
Solutions (Java)
N = Long.parseLong(
in.readLine().trim());
Solutions (C++)
Outline
1 A Sample Problem
Problem
Solution
2 Robust I/O
Whitespace
End of Input
3 General advice
Problem-solving
High-level strategy
Conventions
Special Marker
Java
Special Marker
C++
int N;
while (cin >> N && N != 0) {
// process N
}
End of File
Java
Do not do this:
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
String line = in.readLine();
while (line != null) {
N = Integer.parseInt(line.trim());
// process N
line = in.readLine();
}
End of File
Java
Do not do this:
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
String line = in.readLine();
while (line != null) {
N = Integer.parseInt(line.trim());
// process N
line = in.readLine();
}
End of File
Java
End of File
Java
End of File
Java
End of File
C++
End of File
C++
Outline
1 A Sample Problem
Problem
Solution
2 Robust I/O
Whitespace
End of Input
3 General advice
Problem-solving
High-level strategy
Avoiding Failure
Avoiding Failure
Avoiding Failure
Avoiding Failure
Avoiding Failure
Handling Failure
Handling Failure
Handling Failure
Handling Failure
Handling Failure
Coding Strategy
Coding Strategy
Coding Strategy
Outline
1 A Sample Problem
Problem
Solution
2 Robust I/O
Whitespace
End of Input
3 General advice
Problem-solving
High-level strategy
Scoring
Scoring
Scoring
Scoring
Scoring
Scoring
Team Strategy
Team Strategy
Team Strategy
Pair Programming
Pair Programming
Pair Programming
Pair Programming