Input and output using scanf/printf; minor addition to data types.
This commit is contained in:
parent
f1d3dcf7dc
commit
76346a331a
|
@ -18,7 +18,7 @@ There are several main **data types** that are used in contests: 64-bit integers
|
|||
|
||||
It's a good idea to use 64-bit integers (`long long` in C++) instead of 32-bit integers (`int`). 64-bit integers are less likely to have overflow issues, since they can store any number between $-9\,223\,372\,036\,854\,775\,808$ and $9\,223\,372\,036\,854\,775\,807$ which is roughly equal to $\pm 9 \times 10^{18}$. Contest problems are usually set such that the 64-bit integer is sufficient. Of course, you shouldn't do this when time and/or memory limits are tight, which may be the case in higher divisions of USACO.
|
||||
|
||||
**Floating point numbers** are used to store decimal values. It is important to know that floating point numbers are not exact, because the binary architecture of computers can only store decimals to a certain precision. Hence, we should always expect that floating point numbers are slightly off. Contest problems will accommodate this by either asking for the greatest integer less than $10^k$ times the value, or will mark as correct any output that is within a certain $\epsilon$ of the judge's answer. (what's epsilon?)
|
||||
**Floating point numbers** are used to store decimal values. It is important to know that floating point numbers are not exact, because the binary architecture of computers can only store decimals to a certain precision. Hence, we should always expect that floating point numbers are slightly off. Never try to compare two floating-point numbers for exact equality (==); instead, check if their absolute difference is less than some small constant like $10^{-9}$. Contest problems will accommodate this by either asking for the greatest integer less than $10^k$ times the value, or will mark as correct any output that is within a certain $\epsilon$ of the judge's answer. (what's epsilon?)
|
||||
|
||||
**Boolean** variables have two possible states: `true` and `false`. We'll usually use booleans to mark whether a certain process is done, and arrays of booleans to mark which components of an algorithm have finished.
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ In most websites (such as CodeForces and CSES), input and output are **standard*
|
|||
|
||||
### C++
|
||||
|
||||
The [<iostream\>](http://www.cplusplus.com/reference/iostream/) library suffices.
|
||||
The [<iostream\>](http://www.cplusplus.com/reference/iostream/) library suffices.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
|
@ -28,9 +28,35 @@ int main() {
|
|||
}
|
||||
```
|
||||
|
||||
Alternatively, you can use the [<cstdio\>](http://www.cplusplus.com/reference/cstdio/) library's `scanf` anf `printf` functions, which are slightly more complicated to use, but are significantly faster (generally only an issue with large input sizes):
|
||||
|
||||
```cpp
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int x, y;
|
||||
// %d specifies that a value of type int is being input.
|
||||
// Use %lld (a few judging platforms might need %I64d)
|
||||
// to input a long long (64-bit) integer.
|
||||
// Many other specifiers are also available; see link for more details.
|
||||
// Be sure to add a & character (address-of operator) when using
|
||||
// scanf, UNLESS you are inputing a string with %s.
|
||||
// It is possible to input multiple values at a time as shown below.
|
||||
scanf("%d%d", &x, &y);
|
||||
|
||||
// Specifiers for printf are mostly the same as those used
|
||||
// by scanf, with the notable exception of floating-point numbers.
|
||||
// Use a backslash character followed by the lowercase
|
||||
// letter n to denote a newline.
|
||||
// The address-of operator, &, is not used here.
|
||||
printf("Found %d and %d\n", x, y);
|
||||
}
|
||||
```
|
||||
|
||||
### Java
|
||||
|
||||
In your CS classes, you've probably implemented input and output using standard input and standard output, or using [`Scanner`](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) to read input and `System.out.print` to print output. These methods work, but `Scanner` and `System.out.print` are slow when we have to handle inputting and outputting tens of thousands of lines. Thus, we use `BufferedReader` and `PrintWriter` instead, which are faster because they buffer the input and output and handle it all at once as opposed to parsing each line individually.
|
||||
In your CS classes, you've probably implemented input and output using standard input and standard output, or using [`Scanner`](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) to read input and `System.out.print` to print output. These methods work, but `Scanner` and `System.out.print` are slow when we have to handle inputting and outputting tens of thousands of lines. Thus, we use `BufferedReader` and `PrintWriter` instead, which are faster because they buffer the input and output and handle it all at once as opposed to parsing each line individually.
|
||||
|
||||
Here is a Java template for input and output, which is effectively a faster Scanner. We import the entire `util` and `io` libraries for ease of use.
|
||||
|
||||
|
@ -109,11 +135,11 @@ In order to test a program, create a file called `problemname.in`, and then run
|
|||
|
||||
## C++
|
||||
|
||||
You will need the [<cstdio\>](http://www.cplusplus.com/reference/cstdio/) or the [<fstream\>](http://www.cplusplus.com/reference/fstream/) library. Essentially, replace every instance of the word `problemname` in the word below with the input/output file name, which should be given in the problem.
|
||||
You will need the [<cstdio\>](http://www.cplusplus.com/reference/cstdio/) or the [<fstream\>](http://www.cplusplus.com/reference/fstream/) library. Essentially, replace every instance of the word `problemname` in the word below with the input/output file name, which should be given in the problem.
|
||||
|
||||
Below, we have included C++ templates for input and output. We use `using namespace std;` so that we don't have to preface standard library functions with `std::` each time we use them.
|
||||
|
||||
If `<cstdio>` is used:
|
||||
If `<cstdio>` is used:
|
||||
|
||||
```cpp
|
||||
#include <cstdio>
|
||||
|
@ -126,7 +152,7 @@ int main() {
|
|||
}
|
||||
```
|
||||
|
||||
If `<fstream>` is used: (Note that you cannot use C-style I/O (`scanf`, `printf`) with this method):
|
||||
If `<fstream>` is used: (Note that you cannot use C-style I/O (`scanf`, `printf`) with this method):
|
||||
|
||||
```cpp
|
||||
#include <fstream>
|
||||
|
@ -189,7 +215,7 @@ public class template {
|
|||
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("template.out")));
|
||||
|
||||
// YOUR CODE HERE
|
||||
|
||||
|
||||
pw.close(); // flushes the output once printing is done
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue