Problem Solving Tutorial
For most of the problems you will be required to read some data from the standard input (keyboard) and write your answers to the standard output (console). It is important to follow the input/output specifications EXACTLY. If your output is different from the correct by a single white space your solution will not be counted as ACCEPTED and you will be penalized.
Input and Output
If this is your first programming contest and you have decided to use C++, it is highly recommended that you use cin/cout for input and output. You will need to add the following lines to the top of your program to use cin/cout:
#include <iostream> #include <string> #include <other_stuff> using namespace std;
Supposing you wanted to read an int, a char, a word, and the entire next line, you could do the following:
int d; char c; string s1, s2; cin >> d >> c >> s1; getline(cin, s2); getline(cin, s2);
Note the first getline is necessary to ignore the rest of the first line, even if it's empty. Supposing you wanted to print them back out:
cout << d << " " << c << " " << s1 << endl << s2 << endl;
It's recommended that you use the Scanner class to do input with Java 5. You will need to add the following to the start of your code:
import java.util.*; import java.io.*;
Before reading input with Java 5 you will need to create a Scanner object by adding this line to your main method:
Scanner in = new Scanner(System.in);
Supposing you wanted to read an int, a char, and a word, and the entire next line, you could do the following:
int d = in.nextInt(); char c = in.next().charAt(0); s1 = in.next(); in.nextLine(); s2 = in.nextLine();
Note the first nextLine is necessary to ignore the rest of the first line, even if it's empty. Supposing you wanted to print them back out:
System.out.println(d + " " + c + " " + s1); System.out.println(s2);
Use System.out.print("Blah") to avoid printing a newline.
C-style input and output can be used in both C and C++ programs. C-style input is somewhat more cryptic than cin/cout, but usually less verbose. You will need to add the line:
Supposing you wanted to read an int, a char, and a word, you could do the following:
int d; char c; char s1, s2; scanf ("%d %c %s\n", &d, &c, s1); gets(s2);
Note the final \n in the scanf format string is necessary to ignore the blank space and end-of-line character at the end of the first line. Otherwise, you will read a blank line in s2. Don't forget to use the address-of operator on scalar variables, and to allocate enough space for C-style strings.
Supposing you wanted to print them back out:
printf ("%d %c %s\n%s\n", d, c, s1, s2);
Check the C runtime library documentation for all the fun things one can do with printf and scanf format specifiers.