From 76346a331aa661b77f122b9d9a5e37cb30422510 Mon Sep 17 00:00:00 2001 From: Hankai Zhang Date: Sun, 28 Jun 2020 21:13:40 -0400 Subject: [PATCH] Input and output using scanf/printf; minor addition to data types. --- content/1_Intro/Data_Types.md | 2 +- content/1_Intro/Input_Output.md | 38 +++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/content/1_Intro/Data_Types.md b/content/1_Intro/Data_Types.md index 5eed15d..564bdd9 100644 --- a/content/1_Intro/Data_Types.md +++ b/content/1_Intro/Data_Types.md @@ -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. diff --git a/content/1_Intro/Input_Output.md b/content/1_Intro/Input_Output.md index fa03cff..dfcf960 100644 --- a/content/1_Intro/Input_Output.md +++ b/content/1_Intro/Input_Output.md @@ -16,7 +16,7 @@ In most websites (such as CodeForces and CSES), input and output are **standard* ### C++ -The [](http://www.cplusplus.com/reference/iostream/) library suffices. +The [](http://www.cplusplus.com/reference/iostream/) library suffices. ```cpp #include @@ -28,9 +28,35 @@ int main() { } ``` +Alternatively, you can use the [](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 +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 [](http://www.cplusplus.com/reference/cstdio/) or the [](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 [](http://www.cplusplus.com/reference/cstdio/) or the [](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 `` is used: +If `` is used: ```cpp #include @@ -126,7 +152,7 @@ int main() { } ``` -If `` is used: (Note that you cannot use C-style I/O (`scanf`, `printf`) with this method): +If `` is used: (Note that you cannot use C-style I/O (`scanf`, `printf`) with this method): ```cpp #include @@ -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 } }