看板 C_and_CPP 關於我們 聯絡資訊
題目網址 https://zerojudge.tw/ShowProblem?problemid=b964 找到參考解答 要問的是 while (scanf("%d",&n) !=EOF ) !=EOF 是什麼意思呢 常常看到 競賽題 為何都要這樣寫@@ 用C++的 while ( cin>>n ) 是否也可呢? #include <cstdio> #include <cstdlib> int cmp(const void * a, const void * b){ return ( *(int*)a - *(int*)b ); } int main(){ int n,score[20],a60,b60; while (scanf("%d",&n) !=EOF ){ a60=-1,b60=-1; for(int i=0;i<n;i++){ scanf("%d",&score[i]); } qsort(score,n,sizeof(int),cmp); for(int i=0;i<n;i++){//由前往後找小於60的最大數 if (score[i]<60) b60=score[i]; } for(int i=n-1;i>=0;i--){//由後往前找大於等於60的最小數 if (score[i]>=60) a60=score[i]; } printf("%d",score[0]); for(int i=1;i<n;i++){ printf(" %d",score[i]); } printf("\n"); if (b60 != -1) printf("%d\n",b60); else printf("best case\n"); if (a60 != -1) printf("%d\n",a60); else printf("worst case\n"); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.203.74.58 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1537450097.A.174.html
MOONRAKER: 安安你好 你有看過scanf()走路嗎 09/20 21:42
tyjh: 哈 對耶 scanf 是輸入 恩 我換個問法 09/20 21:58
※ 編輯: tyjh (203.203.74.58), 09/20/2018 22:00:46
MOONRAKER: ……不是這個意思好嗎 09/20 22:47
moebear: end of file 09/20 23:33
Ori185: 我在primer學到的是,EOF就像4樓講的叫做檔案終點,用來告 09/20 23:37
Ori185: 訴程式我已經輸入完畢的意思 09/20 23:37
Ori185: 利用EOF來傳達輸入結束,可以避免用一些符號作輸入結束時 09/20 23:42
Ori185: ,又遇到要輸出這些符號的狀況 09/20 23:42
bigbite: 其實你去看scanf的manpage有提到"The value EOF is retur 09/20 23:49
bigbite: ned if the end of input is reached..." 09/20 23:50
tyjh: 了解 09/21 09:24
MOONRAKER: 你scanf()到底在哪裡學的 為什麼有一種你的課本上 09/21 14:18
MOONRAKER: 都沒有範例程式的感覺 從上上次就是這樣了 09/21 14:18
bigbite: 原po應該是剛開始學程式對嗎?不懂的多問吧 09/21 16:44
moebear: 可以多到http://www.cplusplus.com/ 這邊查找 09/22 03:45