Input and Output
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 we looked at on monday 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++)karris[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
"printf" works just like it does in java, except without the "System.out" part.
Testing
go to icpc.egr.duke.edu (currently speedyguy17.info/ucpccc-master) navigate to "log in". Your creditials are netid/netid. Note: this is NOT your OIT password. your password is literally your netid. under submissions, select the problem, language, and browse to find the approriate .java or .cc file click submit and you should get a result soon! Possible results Correct Incorrect Output....your program ran, but gave the wrong answer excessive output....consider it incorrect output output format exception....consider it incorrect output runtime error....your program crashed compile error....your program didn't compile...if you think this is in error, let me know, since sometimes it's just version mismatches NOTE: do NOT!!! include the java "package" declaration in the top of your file....it won't work. also don't start your class names with underscores.
Eclipse
There are a couple things to know when doing these problems on eclipse:
1) you can type or copy paste input into the console in eclipse. windo->show view->console. Just run your program, and then type the input exactly how the problem says 2) the debugger. If you HAVEN'T used it yet, it's extremely powerful. Double click on the bar on the left hand side of your code to set a breakpoint somewhere near the start of your program. then click the little ant to the left of the run button. If you type in your input just like in point 1, eventually eclipse will ask you if you want to drop into the debugging perspective. say "yes" at this point, you can go through your program line by line, and see the values of all your variables in the upper right hand corner. It's exceptional.