Input and Output: Difference between revisions

From programming_contest
Jump to navigation Jump to search
imported>Kmk21
No edit summary
imported>Kmk21
m Kmk21 moved page Reading Input to Input and Output without leaving a redirect
 
(No difference)

Latest revision as of 05:15, 16 February 2015

Java

Input

In java reading input can seem a bit overwhelming at first, but once you do it a couple times it will become second nature. We will use the "scanner" class. At the top of your code, inside the class definition, but outside the "main" function, you'll want to add the line

Scanner in = new Scanner(System.in);  //note the compiler will yell at you if you use this in a static method...in which case you just have to add "static" before this bit

The scanner class has a whole host of methods we can use so we don't have to worry about things like converting strings to integers and such. Whatever you ask it to read, it will read until the next whitespace character (tab, space, newline). So you can do things like

int number = in.nextInt();
long big_number = in.nextLong();
double decimal = in.nextDouble();
String word = in.next();
String line = in.nextLine();

NOTE: try not to mix nextLine() with the other ones. There is a technical reason for this, but if you HAVE to, you'll have to use an extra in.nextLine()...as the first line you try to read will be blank. This does NOT apply if you're only using nextLine(). example: http://speedyguy17.info/algorithms/icpc/midatl2011/src/b.java

The way I do this is to simply write the input code as I'm reading the problem. Here we can use the problem server as an example:

"one line with one integer n (1 ≤ n ≤ 105), the number of submissions;"

//I know I have to read and integer...might as well call it "n" so i don't get confused
int n = in.nextInt();

"n lines, each with a result of the judging by DOMjudge, in arbitrary order;"

//I have to read some number of lines, and i know the number...so i might as well just put them in an array!
//normally if it says "lines" you should use in.nextLine()...but since it says there are only letters, in.next() will give us
//the same thing and we don't have to worry about mixing nextLine with nextInt like i mentioned

String[] dom = new String[n];
for(int i=0; i<n; i++) dom[i] = in.next();

" n lines, each with a result of the judging by Kattis, in arbitrary order."

//This will be the same as before

String[] kattis = new String[n];
for(int i=0;i<n;i++)kattis[i]=in.next();


And that's it! we're done reading the input. An example of how it might look in code is: http://speedyguy17.info/algorithms/icpc/midatl2010/src/b.java

Output

Oftentimes you'll see problems that ask you to "print out put in the form "Case <case number>: <answer> meters"". One way to do this is to do something like:

System.out.println("Case " + case_num + ": " + ans + " meters");

This will work, but becomes unwieldy. It also doesn't help with situations where (as often happens) the problem asks you to "print the answer to 2 decimal places". To solve all these problems we have a slightly different print function called "printf". It works by allowing you to put special characters in as placeholders for numbers which we fill in later.

%d becomes an integer %x becomes a hex integer %f becomes a floating point number %s becomes a string %% becomes a "%"

For instance the above code would be changed to:

System.out.printf("Case %d: %d meters\n", case_num, meters); //the first %d is replaced by case_num, and the second %d by meters

The last bit here is printing to "x decimal places." It turns out we can just use a slightly different of %f. You can use the following to print to a number of decimal places:

%.1f = 1 decimal place %.2f = 2 decimal places %.3f = 3 decimal places (I think you get the gist)

There's a bunch of cooler stuff you can do, but you can look that up, and this covers like 99% of the cases you'll see.

NOTE: if you use printf, you must explicitly print the newline character.


C++

Input

Scanf is a C function that works a lot like printf as described above... except in the opposite direction.

    int my_num;
    scanf(" %d", &my_num);

This code reads an integer. There are different codes for different types of variables:

  • Integers - %d
  • Long Integers - %lld
  • Floats - %f
  • Doubles (better floats) - %lf
  • Characters - %c
  • Strings - %s

You MUST add this "&" before the name of the variable, because internaly scanf works in a different way than printf. Also, for security reasons, always put a blank space before %something, e.g. " %d" instead of "%d".

The safest way of reading an entire line of characters, up to the "new line" is:

    int my_line;
    scanf(" %[^\n]s", my_line);

Note: You do not have to add the "&" when reading strings with scanf, but ONLY when reading strings.

If you want you can also use the C++ functin cin:

    int my_num;
    std::cin >> my_num;

Output

The stdio function "printf" works just like System.out.println works in Java. However, printf can only be used with output formatting, i.e. "printf(something + " " + more);" does not work, you have to use "printf("%d %d", something, more);".