1. 程式人生 > >那個虐了我的杭電1000

那個虐了我的杭電1000



        沒有參加過ACM,做杭電上的題,只是為了複習資料結構,萬萬沒想到剛開始就被打擊了大哭

        第1000題:

Problem Description                                                                                                      Calculate A + B.                                                                                                            
Input                                                                                                                                Each line will contain two integers A
and B. Process to end of file.                               
Output                                                                                                                              For each case, output A + B in one line.                                                                         
Sample Input                                                                                                                     1 1 Sample Output                                                                                                                  2 樣例害人啊發火

    很丟人一開始竟然沒看懂題意大哭   

AC的程式碼如下

#include <stdio.h>

int main()
{
    int a,b,d=0;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        printf("%d\n", a+b);
    }
    return 0;
}

    這個題的關鍵是“Process to end of file”這句話,處理到檔案的結束,以前從沒見過,

這裡要先了解一下scanf()這個函式和EOF

   1.scanf()

scnaf()的函式原型:int scanf( const char *format, ... );從這可以看出它返回一個整形,以第1000題為例  “scanf("%d%d",&a,&b);"

如果a和b都被成功讀入,那麼scanf的返回值就是2;

如果只有a被成功讀入,返回值為1;

如果a和b都未被成功讀入,返回值為0;

如果遇到錯誤或遇到end of file,返回值為EOF,即-1。

ACM中輸入多組資料時用這種方式結束ctrl+z