杭电oj第1000题—— A + B Problem
生活随笔
收集整理的這篇文章主要介紹了
杭电oj第1000题—— A + B Problem
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題述如下:
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. \\注意,此處強調一行一輸出,即輸出后跟“\n”Sample Input 1 1 \\注意,兩數字間存在的字符為空格Sample Output 2參考程序如下:
#include <stdio.h>int main() {int a,b;while(scanf("%d %d",&a,&b)!=EOF){printf("%d\n",a+b);} }?第一次自己編寫的程序如下:
#include <stdio.h> int main(int argc,char **argv) {int a,b,c,d;do{printf("a=");c=scanf("%d",&a);getchar();printf("b=");d=scanf("%d",&b);getchar();if(c!=EOF&&d!=EOF)printf("a+b=%d\n",a+b);}while(c!=EOF&&d!=EOF);return 0; }getchar()語句用于接收緩沖區的回車或空格符,由于此程序中存在兩個scanf()語句,若不處理回車符將導致程序陷入死循環,無法正常接收輸入。
代碼本身沒有錯誤,只是思路另類了些,但submit后顯示wrong answer,多次排查后發現將程序中的解釋性代碼(如printf("a="))刪去即可accepted,以下是accepted的代碼:
#include <stdio.h> int main(int argc,char **argv) {int a,b,c,d;do{c=scanf("%d",&a);getchar();d=scanf("%d",&b);getchar();if(c!=EOF&&d!=EOF)printf("%d\n",a+b);}while(c!=EOF&&d!=EOF);return 0; }總結:
總結
以上是生活随笔為你收集整理的杭电oj第1000题—— A + B Problem的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大学计算机作业互评评语简短,大学学生互评
- 下一篇: php 数组 时间戳排序,php –