PrevNext

Input & Output

Authors: Darren Yao, Benjamin Qi, Allen Li

How to read input and print output for USACO contests.

Edit This Page
Resources
IUSACO

module is based off this

CPH

cin, getline, files

PAPS

cin, getline

The code snippets below will read in three integers as part of a single line and output their sum. For example, given the input

1 2 3

the output will be as follows:

The sum of these three numbers is 6

Feel free to test them out at ide.usaco.guide.

Out of the methods below, which one should I use?

It doesn't matter. Whichever you're most comfortable with!

Standard I/O

In most websites (such as Codeforces and CSES), and in USACO problems after December 2020, input and output are standard.

Method 1 - <iostream>

More straightforward to use. Calling the extraction operator operator>> on cin reads whitespace-separated data from standard input. Similarly, calling the insertion operator operator<< on cout writes to standard output. The escape sequence \n represents a new line.

#include <iostream>
using namespace std;
int main() {
int a;
int b;
int c;
cin >> a >> b >> c;
// "\n" can be replaced with endl as well
cout << "The sum of these three numbers is " << a + b + c << "\n";
}

endl vs \n

endl and \n are not equivalent; see Fast I/O for details.

Method 2 - <cstdio>

This library includes the scanf and printf functions, which are slightly more complicated to use.

#include <cstdio>
using namespace std;
int main() {
int a;
int b;
int c;
/*
* %d specifies that a value of type int is being input.
* To input a 64-bit (long long) number,

Input Speed

The second method is significantly faster (generally only an issue with large input sizes). However, the first method can be sped up so that the difference in speed is not significant; see Fast I/O for details.

Example Problem - Weird Algorithm

Focus Problem – try your best to solve this problem before continuing!

Try to implement this yourself!

Resources
GCP

example C++ solution for this problem

Solution - Weird Algorithm

#include <iostream>
using namespace std;
int main() {
long long x;
cin >> x;
while (x != 1) {
cout << x << " ";
if (x % 2 == 0) {
x /= 2;

Warning!

As noted in the resource above, this problem requires 64-bit integers. The following solution, which uses int instead of long long, does not pass all of the test cases.

#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
while (x != 1) {
cout << x << " ";
if (x % 2 == 0) {
x /= 2;

This happens because numbers in the sequence may exceed the maximum possible value for the int data type (23112^{31}-1, as mentioned in the prerequisite module).

How to Submit Your Solution

Choice of IDE

We assume in this section that you are using an online IDE, such as the USACO Guide IDE. The instructions for using a local IDE are similar, except you skip the download step.

Do the following to submit on CSES. Submitting on other platforms (such as USACO) is similar.

  1. Run your solution code with the sample input, and make sure that it produces the sample output.
  2. Download your solution code into a file. The file extension should be one of .cpp, .java, .py or their equivalents, depending on your programming language.
  3. Open the problem statement. Log in if you aren't already logged in.
  4. Press the submit tab (for USACO, scroll to the bottom of the page).
  5. Upload your solution file. For CSES, assuming the file had the proper extension, the language should be automatically detected (for USACO, select the language yourself). Some platforms (such as Codeforces) allow you to paste your code into a text box instead of uploading a file.
  6. Submit your solution. For CSES, you will be redirected to the results page (for USACO, results will appear at the top of the page). If your solution is correct on all the test cases, you're done! Otherwise, fix your code and start again from step 1.

Submitting to Past USACO Problems

If you are using the USACO Guide IDE, you can submit to some past USACO problems through that (Settings -> Judge). You cannot use this to submit during a live contest.

File I/O

USACO File I/O

USACO problems from December 2020 onwards use standard I/O rather than file I/O. You'll still need to use file I/O to submit to earlier problems.

In older USACO problems, the input and output file names are given and follow the convention problemname.in. After the program is run, output must be printed to a file called problemname.out.

Focus Problem – try your best to solve this problem before continuing!

You must use the correct file names when opening the .in and .out files, depending on the problem. The file names are given on USACO problems which require file opening. For example, you would open paint.in and paint.out in the above problem.

Method 1 - freopen

You will need the <cstdio> library. The freopen statements reuse standard I/O for file I/O. Afterwards, you can simply use cin and cout (or scanf and printf) to read and write data.

#include <cstdio>
#include <iostream>
using namespace std;
int main() {
freopen("problemname.in", "r", stdin);
// the following line creates/overwrites the output file
freopen("problemname.out", "w", stdout);
// cin now reads from the input file instead of standard input

To test your solution locally without file I/O, just comment out the lines with freopen.

For convenience, we can define a function that will redirect stdin and stdout based on the problem name:

#include <cstdio>
#include <iostream>
using namespace std;
// the argument is the input filename without the extension
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}

Method 2 - <fstream>

You cannot use C-style I/O (scanf, printf) with this method.

#include <fstream>
using namespace std;
int main() {
ifstream fin("problemname.in");
ofstream fout("problemname.out");
int a;
int b;
int c;
fin >> a >> b >> c;
fout << "The sum of these three numbers is " << a + b + c << "\n";
}

Example Solution - Fence Painting

Resources
USACO

Make sure to read this.


For an explanation of the solutions below, check the Rectangle Geometry module.

Method 1 - freopen

#include <iostream>
#include <vector>
using namespace std;
int main() {
// Use standard input to read from "paint.in"
freopen("paint.in", "r", stdin);
// Use standard output to write to "paint.out"
freopen("paint.out", "w", stdout);

Method 2 - <fstream>

#include <fstream>
#include <vector>
using namespace std;
int main() {
ifstream fin("paint.in");
ofstream fout("paint.out");
vector<bool> cover(100);
int a, b, c, d;

USACO Note - Extra Whitespace

Importantly, USACO will automatically add a newline to the end of your file if it does not end with one.

Warning!

Occasionally, there is a period of time near the beginning of the contest window where the model outputs do not end in newlines. This renders the problem unsolvable.

Make sure not to output trailing spaces, or you will get an error such as the following:

bad

Here are some examples of what is allowed and what isn't when the intended output consists of a single integer ans:

C++

cout << ans; // OK, no newline
cout << ans << endl; // OK, newline
cout << ans << "\n"; // OK, newline
cout << ans << " "; // NOT OK, extra space
cout << ans << "\n\n"; // NOT OK, extra newline

Module Progress:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

PrevNext