全篇都是講數字之間的運算的:
由上自下難度逐漸升級 ,沒耐心者建議一拉到底:
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
搜到的答案1000.1:
#include <stdio.h> int main() { int a,b; while(scanf("%d %d",&a,&b)!=EOF) { printf("%d\n",a+b); } return 0; }View Code
我的代碼1000.2:
#include<stdio.h> #include <stdlib.h> int main() { int a=0,b=0; int sum = 0; scanf_s("%d %d", &a,&b); sum = a + b; printf("%d", sum); return 0; }View Code
小結:可以把自己的代碼簡化 ,如1000.2中的sum求和可以省略直接用printf進行輸出,
1089:
Your task is to Calculate a + b.
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. Sample Input 1 5 10 20 Sample Output 6 30
搜的代碼1089.1:
#include<stdio.h> int main() { int a, b; while (scanf_s("%d %d", &a, &b) != EOF) // 輸入結束時,scanf函式回傳值為EOF,即沒有資料輸入時則退出while回圈 printf("%d\n", a + b); return 0; //回傳值為0 }View Code
我的代碼1089.2:
#include<stdio.h> #include <stdlib.h> int main() { int a=0,b=0,c=0,d=0; int sum = 0,sumo=0; scanf_s("%d %d", &a,&b); getchar(); scanf_s("%d %d", &c, &d); sum = a + b; sumo = c + d; printf("%d\n%d", sum,sumo); return 0; }View Code
小結:這里很蠢:我以為題目的意思是連續輸入兩行,然后連續輸出結果,有了代碼1089.2.
但是實際上output:For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.的意思是,每輸入一行后面就要接一行output,審題不明確,
1090:
Your task is to Calculate a + b.
Input Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line. Output For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. Sample Input 2 1 5 10 20 Sample Output 6 30
搜的代碼:
1 #include<stdio.h> 2 3 int main(void) 4 { 5 int a=0,b=0,N=0,i=0; 6 scanf("%d\n",&N); 7 while(i<N) 8 { 9 scanf("%d%d",&a,&b); 10 printf("%d\n",a+b); 11 i++; 12 } 13 return 0; 14 }View Code
我的代碼:
1 #include<stdio.h> 2 int main() 3 { 4 int n,a, b; 5 scanf_s("%d",&n); 6 while (scanf_s("%d %d", &a, &b) != EOF) // 輸入結束時,scanf函式回傳值為EOF,即沒有資料輸入時則退出while回圈 7 printf("%d\n", a + b); 8 return 0; //回傳值為0 9 }View Code
小結:變式多了一個在最開始加一個式子總數的輸入,1090.2沒有使用到這個總數,而1090.1使用到了這個總數,用while(N--)控制,,
1091:
Problem Description Your task is to Calculate a + b. Input Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed. Output For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. Sample Input 1 5 10 20 0 0 Sample Output 6 30
搜到的代碼1091.1:
1 #include<stdio.h> 2 int main() 3 { 4 int a, b; 5 while (scanf_s("%d%d", &a, &b) == 2) 6 { 7 if (a == 0 && b == 0) break; 8 else printf("%d\n", a + b); 9 } 10 return 0; 11 }View Code
我的代碼1091.2:
1 #include<stdio.h> 2 int main() 3 { 4 int a, b; 5 int i = 0; 6 while (i != -1) 7 { 8 scanf_s("%d%d", &a, &b); 9 if (a == 0 && b == 0) 10 return 0; 11 else 12 printf("%d\n", a + b); 13 i++; 14 15 } 16 }View Code
小結:這個變式我從代碼1091.1發現可以通過scanf(***)==n來控制每一組元素的個數,如果scanf設定為n,那么vs會按照輸入端輸入的值依次選取n個值進行操作(比如:設定n=2,即使鍵盤輸入第一行三個數a1,a2,a3,回車運行,得到的答案依舊是對a1,a2進行操作的結果,接著在輸入一行a4,a5,那么結果輸出的是對a3,a4操作的結果),有助于進行嚴格的操作,當然如果不進行設定n值,scanf(****),那么vs會操作每一行相應的元素,不會涉及到上一行,
1092:
Problem Description Your task is to Calculate the sum of some integers. Input Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed. Output For each group of input integers you should output their sum in one line, and with one line of output for each line in input. Sample Input 4 1 2 3 4 5 1 2 3 4 5 0 Sample Output 10 15搜的代碼1092.1:
1 #include<stdio.h> 2 int main() 3 { 4 int n,m,i,sum; 5 while(scanf("%d",&n)!=EOF) 6 { 7 if(n==0) 8 break; 9 sum=0; 10 for(i=0;i<n;i++) 11 { 12 scanf("%d",&m); 13 sum+=m; 14 } 15 printf("%d\n",sum); 16 } 17 return 0; 18 }以輸入0判斷結尾
我的代碼1092.2:
以輸入0判斷結尾
小結:我發現回圈這一部分用while運算式:“while(n--)”做判斷句挺好的,如果n自減到0退出回圈,不然就繼續回圈,相當于:“for(i=n;i>0;i--)”
1093:
Problem Description Your task is to calculate the sum of some integers. Input Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line. Output For each group of input integers you should output their sum in one line, and with one line of output for each line in input. Sample Input 2 4 1 2 3 4 5 1 2 3 4 5 Sample Output 10 15搜的代碼1093.1:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define max 100 4 int main() 5 { 6 int x, i, j, n; 7 scanf_s("%d", &x); 8 for (j = 0; j < x; j++) 9 { 10 scanf_s("%d", &n); 11 int s = 0, a[max]; 12 for (i = 1; i <= n; i++) 13 scanf_s("%d", &a[i]); 14 15 for (i = 1; i <= n; i++) 16 s = s + a[i]; 17 printf("%d\n", s); 18 if (j != x - 1)printf("\n"); 19 } 20 return 0; 21 }View Code
我的代碼1093.2:
1 #include<stdio.h> 2 int main() 3 { 4 int n,m; 5 int a, sum=0; 6 scanf_s("%d", &m); 7 while (m--) 8 { 9 scanf_s("%d", &n); 10 { 11 while (n--) 12 { 13 scanf_s("%d", &a); 14 sum += a; 15 } 16 printf("%d", sum); 17 } 18 } 19 return 0; 20 }View Code
小結:1093.1使用的陣列做的,嗯可能是vs版本的問題吧(我用的2017) ,搜到的的代碼都會有報錯,然后加了個#define max 100,然后能正常運行,
1094:
Problem Description Your task is to calculate the sum of some integers. Input Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line. Output For each test case you should output the sum of N integers in one line, and with one line of output for each line in input. Sample Input 4 1 2 3 4 5 1 2 3 4 5 Sample Output 10 15搜的代碼:
多行輸入,要求按行輸出
我的代碼:
第一個元素是數字個數
小結:忘記了多行輸入的判斷方法了:scanf_s("***")!=EOF,然后就扔下了一天,,,說一下EOF:
"EOF 是end of file的縮寫 ,
在用函式讀入檔案資料的時候,函式總會回傳一個狀態,是讀取成功還是失敗,那么這個狀態怎么表示呢,所以就約定俗成定義一個識別符號表示這個狀態,就有了EOF,
scanf函式只有在第一個引數為NULL(空指標)的情況下,才可能回傳EOF,否則,回傳成功格式化并賦值的引數個數(>=0),
所以,這個回圈,將是一個死回圈,"
我太喜歡這個回圈了
1095:
Problem Description Your task is to Calculate a + b. Input The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. Output For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line. Sample Input 1 5 10 20 Sample Output 6 30 搜的代碼: 沒搜到(額,,,可能代碼太簡單了,沒有人寫)我的代碼:
1 #include<stdio.h> 2 int main() 3 { 4 int i,a,b,sum=0; 5 while (scanf_s("%d %d", &a,&b) != EOF) 6 { 7 printf("%d\n\n",a+b); 8 } 9 return 0; 10 }多行輸入,要求按行輸出,并帶有空行
1096:
Your task is to calculate the sum of some integers.
Input Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line. Output For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs. Sample Input 3 4 1 2 3 4 5 1 2 3 4 5 3 1 2 3 Sample Output 10 15 6 也沒搜到代碼,估計這題被覺得沒什么創新,所以被拋棄了我的代碼:
#include<stdio.h> int main() { int i,n,m; scanf_s("%d", &n); while(n--) { scanf_s("%d", &i); int sum = 0; while (i--) { scanf_s("%d",&m); sum += m; } printf("%d\n\n", sum); } return 0; }View Code
全篇總結:
不足 :計劃上應該是兩天練完的,但是我搞了四天,屬實是懶
學到了:1.我能靈活的使用回圈陳述句,尤其是while(n--),真好用,推薦;
2.讓文段回圈下去的EOF的使用
3.不至于太生疏的使用陣列,寫入資料和讀出資料
最后,武漢加油!白衣天使們加油!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/30958.html
標籤:其他
上一篇:HTTP協議 有這篇文章足夠了
下一篇:一波雜亂的分享
