HDU——算法練習1000 1089-1096

全篇都是講數字之間的運算的:數組

由上自下難度逐漸升級 ,沒耐心者建議一拉到底:ide

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:this

#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:spa

#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:code

Your task is to Calculate a + b.blog

Inputip

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.get

 

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.不至於太生疏的使用數組,寫入數據和讀出數據

 

最後,武漢加油!白衣天使們加油!

相關文章
相關標籤/搜索