1.輸入輸出
ACM和TopCoder不同,TopCoder只用讓參賽者寫一個class,而ACM需要參賽者完成整個console程序.在TopCoder中,輸入輸出是通過parameter傳遞的,不用過多考慮,在ACM中,卻需要自己編寫.
(1).只有一組輸入時,這種情況不用我說了,都會,但是通常也不會有這么水的題
(2).固定組數的輸入,可以利用一個計數器和while語句完成,
01 #include <iostream>
02
03 int main(void){
04 int n;
05 scanf("%d", &n);
06 while (n--){
07 //...
08 }
09 //...
10 return 0;
11 }
(3).測試數據結尾有標志字符(例如最后一組輸入后給一個0),這個只用加一個if語句判斷讀入的數據是什么,是結束標志跳出就ok了.也不多說了
(4).題目沒有告訴你有多少組數據,這個通常是最令新手疑惑的,這種情況,一般用文件結束標志EOF判斷
01 #include <iostream>
02
03 int main(void){
04 int n;
05 while (scanf("%d", &n) != EOF){
06 //...
07 }
08 //...
09 return 0;
10 }
其實這里也可以用c++的cin輸入流判斷,例如
01 #include <iostream>
02
03 using namespace std;
04
05 int main(void){
06 int n;
07 while (cin>>n){
08 //...
09 }
10 //...
11 return 0;
12 }
但是這樣不是特別好,為什么?下面會說.
對于輸出,最好采用的接收一組數據,處理一組數據,把結果保存在一個緩沖數組中,待所有輸入結束后,再一起輸出,而不是待接收完所有輸入后,再處理,再輸出,這樣會消耗更多的memory,而且會更慢.
2.關于效率
第一,上面的所有例子,均采用的c標準I/O,為什么不用c++的cin,cout呢?是有原因的,經實踐,在大規模輸入輸出下,cin,cout效率遠遠低于scanf()和printf(),原因據我估計應該是以為scanf(),printf()是匯編寫的(這點可以從這兩個函數均可以接受任意多組parameter(s)看出,c/c++函數是不具備這樣的性質的),而cin,cout均是直接c/c++寫的流操作,本來c/c++就比匯編慢,還引入流,所以自然比scanf(),printf()慢了.因此,在輸入輸出數據量很小的情況下,出于方便的原因,可以采用cin,cout,而在輸入輸出數據量比較大的情況下用scanf(),printf()比較保險,避免超時.
第二.ACM中,除了c/c++,一般還支持java等語言,但是由于java是解釋執行的,效率十分低下,為此,一般的JudgeOnline都把java的time limit設置為題目給定值(也就是c/c++的time limit)的三倍,而且給每一組輸入再額外提供150ms.即使是這樣,java遇上復雜或者高精度計算的題目,還是很容易超時,因為效率有時候還遠遠未到c/c++的1/3.因此,一般來說,除了個別java極其有利的情況(例如字符串處理),不建議使用java.
3.關于所得結果的解釋
下面是一般JudgeOnline系統所有可能產生的結果,如果對返回的結果不明,看看解釋吧
Waiting: Your program is being judged or waiting to be judged.
Accepted (AC): Congratulations! Your program has produced the correct output!
Presentation Error (PE): Your program's output format is not exactly the same as required by the problem, although the output is correct. This usually means the existence of omitted or extra blank characters (white spaces, tab characters and/or new line characters) between any two non-blank characters, and/or blank lines (a line consisting of only blank characters) between any two non-blank lines. Trailing blank characters at the end of each line and trailing blank lines at the of output are not considered format errors. Check the output for spaces, blank lines, etc. against the problem's output specification.
Wrong Answer (WA): Your program does not produce the correct output. Special judge programs will possibly return Wrong Answer in place of Presentation Error for simplicity and robustness.
Runtime Error (RE): Your program has failed during the execution. Possible causes include illegal file access, stack overflow, out of range in pointer reference, floating point exception, division by zero and many others. Programs that stay not responding for a long time (not consuming CPU cycles) may also be considered to have encountered runtime errors.
Time Limit Exceed (TLE): The total time your program has run for has exceeded the limit.
Each problem has two time limits - TOTAL TIME LIMIT and CASE TIME LIMIT. The former is the total time allowed for your program to deal with all input files. And the latter is the total time allowed for your program to deal with a single input file. Exceeding either one will lead to Time Limit Exceed. If you get Time Limit Exceed but find that your program has run for less time than is limited, your program must have exceeded that CASE TIME LIMIT.
Problems without a special demand on the time limit for a single input file will have its case time limit is trivially set as the same as its total time limit and the phrase "Case Time Limit" will not show up under the problem title.
Memory Limit Exceed (MLE): The maximum amount of memory that your program has used has exceeded the limit.
Output Limit Exceed (OLE): Your program has produced too much output. Currently the limit is twice the size of the file containing the expected output. The most common cause of this result is that your programs falls into an infinite loop containing some output operations.
Compile Error (CE): The compiler fails to compile your program. Warning messages are not considered errors. Click on the judge's reply to see the warning and error messages produced by the compiler.
No such problem: Either you have submitted with a non-existent problem id or the problem is currently unavailable (probably reserved for upcoming contests).
System Error: The judge cannot run your program. One example is that your program requires much more memory than hardware limitation.
Validate Error: The special judge program fails in checking your output, which means it may contain some bugs. If you get this result, please contact the administrator. (Of course, this also means your output is probably wrong).