HDU——算法练习1000 1089-1096

全篇都是讲数字之间的运算的:

由上自下难度逐渐升级 ,没耐心者建议一拉到底:

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;
}

我的代码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;
}

小结:可以把自己的代码简化 ,如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
}

我的代码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;
}

小结:这里很蠢:我以为题目的意思是连续输入两行,然后连续输出结果,有了代码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 }

我的代码:

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 }

小结:变式多了一个在最开始加一个式子总数的输入,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 }

我的代码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 }

小结:这个变式我从代码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 }

我的代码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 }

小结: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;
}

全篇总结:

不足    :计划上应该是两天练完的,但是我搞了四天,属实是懒

学到了:1.我能灵活的使用循环语句,尤其是while(n--),真好用,推荐;

    2.让文段循环下去的EOF的使用

    3.不至于太生疏的使用数组,写入数据和读出数据

最后,武汉加油!白衣天使们加油!

原文地址:https://www.cnblogs.com/lumc5/p/12359923.html

时间: 2024-10-21 14:57:43

HDU——算法练习1000 1089-1096的相关文章

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU 题目分类

基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196.1197.1201.1202.1205.1219.1234.1235.1236.1248.1

hdu 2141 Can you find it? 【时间优化+二分】

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others) Total Submission(s): 16411    Accepted Submission(s): 4172 Problem Description Give you three sequences of numbers A, B, C, then we give you a numbe

算法总结之母函数

1.概念 生成函数即母函数,是组合数学中尤其是计数方面的一个重要理论和工具.生成函数有普通型生成函数和指数型生成函数两种,其中普通型用的比较多.形式上说,普通型生成函数用于解决多重集的组合问题,而指数型母函数用于解决多重集的排列问题.母函数还可以解决递归数列的通项问题(例如使用母函数解决斐波那契数列的通项公式). 2.组合问题中的应用 先放两句百度百科上的原话: 1.“把组合问题的加法法则和幂级数的乘幂对应起来” 2.“母函数的思想很简单 — 就是把离散数列和幂级数一 一对应起来,把离散数列间的

acm steps

1.1 根蒂根基输入输出:LCY的 A+B 8题 (1089~1096) 1.2 C说话根蒂根基:根蒂根基入门题 (2104,2088,1076,2095,1061,1170,3361,1197) 1.3 排序,贪婪: 1052 很恶心的一道贪婪题 3177 我推荐的这题貌似卡了不少人,遵守差值排序 (1236,1084,2093,2111,2187,1157) 2.1 简单数学题:GCD和素数生成占了很大一项目组 1071 积分题 1717 这题斗劲麻烦 (1108,2138,2504,121

杭电ACM——自我强化步骤

第一阶段:开始入门吧!(15天,53题) http://blog.csdn.net/always2015/article/details/44966019#t0 一.输入输出练习(2天,10题) 1000.1089—1096.1001 二.简单操作:(2—4天,12题) 2000—2011.2039 三.英文题试水(3—4天,8题) 1720.1062.2104.1064.2734.1170.1197.2629 四.回归水题(4-6天,24题) 2012—2030.2032.2040.2042.

(转)

来源:http://blog.csdn.net/vsooda/article/details/7293655 1001       这个就不用说了吧1002       简单的大数1003       DP经典问题,最大连续子段和1004       简单题1005       找规律(循环点)1006       感觉有点BT的题,我到现在还没过1007       经典问题,最近点对问题,用分治1008       简单题1009       贪心1010       搜索题,剪枝很关键10

hd题目分类

分类一(详细): 分类二: 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196.1197.1201.1202.1205.1219.1234.12

HD ACM 水题顺序

原文传送门:https://blog.csdn.net/liuqiyao_01/article/details/8477645 第一阶段:开始入门吧!(15天,53题) 一.输入输出练习(2天,10题) 1000.1089-1096.1001 二.简单操作:(2-4天,12题) 2000-2011.2039 三.英文题试水(3-4天,8题) 1720.1062.2104.1064.2734.1170.1197.2629 四.回归水题(4-6天,24题) 2012-2030.2032.2040.2