分享

(初级)1月15日-输入输出练习

 印度阿三17 2021-01-16

第一次参加学校组织的ICPC的培训,这个寒假想写下一写东西记录一下这段时光,接下来我讲叙述一下自己在解题时的一些思路和心得吧,虽然自己的水平有待提高,如果此文章有什么错误欢迎大家帮我改正,一起成长😊。

A - A B Problem!在这里插入图片描述

Calculate A B.(题目要求计算A B)
Input(输入格式)
Each line will contain two integers A and B. Process to end of file.
(每一行将包含两个整数A和B。将文件进程到文件结束)
Output(输出格式)
For each case, output A B in one line.(对于每种情况,在一行中输出A B)
Sample Input(样例输入)
1 1
Sample Output(样例输出)
2

当第一眼看到这个题目,感觉特别简单,我当时却犯了一致命的错误, 当我没有考虑到多组数据的输入和的情况下,我针对这到题目所编写的代码是:

 #include <stdio.h>
int main()
{int a,b; 
    scanf("%d %d",&a,&b);
    printf("%d\n",a b);
    return 0;
}

虽然运行结果满足题目的样例但只能进行一次输入输出便结束了,在题目要求的输入格式一栏中明确要求将文件进程到文件结束,那么这个要求是什么意思呢?
关于 Process to end of file:计算机操作系统要以某种方式判断文件的开始和结束,在C语言中检测文件结尾的一种方法是:在文件结尾放一个特殊的字符来标记文件结尾。通常,在C语言中,用getchar()或scanf()函数读取文件检测到文件结尾时返回一个特殊的值,即EOF(end of file的缩写)。
关于 EOF

  • EOF是一个值,标志着检测到文件结尾,并不是在文件中可以找到的符号。

  • EOF在stdio.h中已经被定义过,不需要重新定义。

  • 一般情况下EOF的值为-1,因为getchar()函数的返回值通常在0—127之间(对应标准字符集),当系统所识别的范围超过标准字符集,则函数的返回值可能在0—255之间,而-1则无论那种情况都不会被检测到,该值作为文件结尾恰到好处;

关于多组数据的输入while((scanf())!=EOF):
  • while(scanf()!=EOF),代表的意思是可持续输入,直到scanf返回的值是-1时才会停止输入,实现了多组数据的输入。

  • 我们也可以在while里面加上一些约束条件,这样的话输入在特定的条件下就会停止,我们也可以通过输入完成后按Ctrl z(好多微型计算机都把Ctrl z识别为文件结尾的信号),可以强行停止输入。

  • 因为scanf函数需要有一个返回值, 当你在持续输入时,scanf就永远不会返回-1,当你通过某种方式,比如按下ctrl z停止输入后,scanf就会返回-1,此时等价于EOF,则这个循环就跳出停止。

正解

#include <stdio.h>

int main()
{int a,b;
    while (scanf("%d%d",&a,&b)!=EOF)
    {printf("%d\n",a b);
    }
    return 0;   
}

注意输出的时候要换行!!

B - A B for Input-Output Practice (I)

在这里插入图片描述
Your task is to Calculate a b(你的任务是计算a和b)
Too easy?! Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.(作者专门为acm初学者设计了这个问题)
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.(输入的数字将由一系列整数a和b组成,他们之间用空格隔开,每行输入一对整数)
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.(对于每一对输入整数a和b,你应该在一行中输出a和b的总和,并在输入的每一行后输出一行)
Sample Input(样例输入)
1 5
10 20
Sample Output(样例输出)
6
30

正解

相比与上一个问题,这个题目的要求略微发生了一点变化,整数a和b之间用空格隔开

#include <stdio.h>

int main()
{int a,b;
    while (scanf("%d %d",&a,&b)!=EOF)
    {printf("%d\n",a b);
    }
    return 0;   
}

C - A B for Input-Output Practice (II)

在这里插入图片描述
Your task is to Calculate a b.(你的任务是计算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.
(输入一个整数N的在第一行,之后是的N行里,每行由一对整数a和b组成,用空格隔开,每行有一对整数)
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.
(对于每一对输入整数a和b,你应该在一行中输出a和b的总和,并在输入的每一行后输出一行结果)
Sample Input(样例输入)
2
1 5
10 20
Sample Output(样例输出)
6
30

正解

相比于第二题,题目又多了一个条件,先输入一个整形变量N,用N来控制输入和输出的次数,那么需要设置一个循环条件来控制,这里我用的while语句

#include <stdio.h>

int main()
{int a,b,n;//题目要求多定义一个变量n
    scanf("%d",&n);//首先输入一个整数n
    while (n!=0)//当n不等于0时进入循环
    {scanf("%d %d",&a,&b);
    printf("%d\n",a b);
        n--;//每输入输出一次后减少n的值,达到题目要求,当n的值为0时跳出循环
        
    }
    return 0;   
}

D - A B for Input-Output Practice (III)

在这里插入图片描述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.
(输入包含多个测试用例,每个测试用例包含一对整数a和b,每行有一对整数。包含0 0的测试用例作为终止输入,并且不处理这个测试用例)
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.
(对于每一对输入整数a和b,你应该在一行中输出a和b的和,并在输入的每一行后输出一行结果)
Sample Input(样例输入)
1 5
10 20
0 0
Sample Output(样例输出)
6
30

正解

这个题目要求将0 0输入作为终止结束测试的方法,那么我们要巧妙运用这个条件设置循环

#include <stdio.h>

int main()
{int a,b;
    scanf("%d %d",&a,&b);//先输入两个数据
    while (a!=0||b!=0)//对这两个输入的数据进行分析,看他们是否都为0,当他们都为0时直接跳过循环
    {printf("%d\n",a b);//不为0则输出他们的和
        scanf("%d %d",&a,&b);//而后继续进行下一组数据的输入
    }
    return 0;
    
}

不知道这个题还有什么巧妙的方法,希望大家也和我交流交流。

E - A B for Input-Output Practice (IV)

在这里插入图片描述
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.
(输入包含多个测试用例,每个测试用例包含一个整数N,然后在同一行有N个整数。以0开头的测试用例将终止输入,并且不处理这个测试用例)
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

正解

题目的要求发生了变化,我们先确定一个非0整数来决定有多少个数字来进行相加,但是这个整数为0就直接输出,但是不处理,我们需要用一个嵌套循环,其中一个来控制这个整数是否为0,另一个用来控制将n个数字相加。

#include <stdio.h>

int main()
{int n,i,sum=0,num;
    scanf("%d",&n);//输入一个整数
    while (n!=0)//判断这个整数是否为0,非0进入循环,0则直接结束
    {for (i=0;i<n;i  )//设置一个for循环来对接下来的n个数字求和
        {scanf("%d",&num);//
            sum =num;//
        }
        printf("%d\n",sum);//输出n个数字的和
        sum=0;//这里还需要把sum的值变为0
        scanf("%d",&n);//再输入一个整数重复循环
    }
    return 0;
    
}

这里有一个小细节就是要把Sum再次赋值为0,保证下一组数据的准确性。

F - A B for Input-Output Practice (V)

在这里插入图片描述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.(输入的第一行是整数N,之后的输入的N行里,每行以一个整数M开头,然后同一行跟着M个整数)
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

正解

对比上一个问题,题目要求用一个数字n来控制输入的行数,同样我们需要设置一个嵌套循环,第一个循环来判断输入的行数,;另一个循环用来计算M个数字的和。

#include <stdio.h>

int main()
{int m,n,i,sum=0,a;
    scanf("%d",&n);//输入控制行数的整数n
    while (n!=0)//若n不为0则进入循环
    {scanf("%d",&m);//输入一个整数m来控制有多少个数字进行相加
        for(i=0;i<m;i  )
        {scanf("%d",&a);//输入这些数
            sum =a;//把他们相加
        }
        printf("%d\n",sum);//输出结果
        sum=0;//重新赋值0于m进行下一组运算
        n--;//每进行一次运算后,n的值减少一次
    }
    return 0;
}

G - A B for Input-Output Practice (VI)

在这里插入图片描述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.
(输入包含多个测试用例,一个用例一行。每一种情况都以一个整数N开始,然后N个整数在同一行)
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.
(对于每个测试用例,你应该在一行中输出N个整数的和,在输入中每一行输出一行)
Sample Input(样例输入)
4 1 2 3 4
5 1 2 3 4 5
Sample Output(样例输出)
10
15

正解

因为题目要求多个测试用例,所以我们需要再次使用while(scanf("%d",&num)!=EOF)来解决

#include <stdio.h>

int main()
{int n,sum=0,i,a;
    while(scanf("%d",&n)!=EOF)//多组数据输入表达式,输入一个整数n来控制有多少个数字进行相加
    {for(i=0;i<n;i  )
        {scanf("%d",&a);//输入这些数
            sum =a;//求和
        }
        printf("%d\n",sum);//输出结果
        sum=0;//重新赋值0于m进行下一组运算

    }
    return 0;
}

H - A B for Input-Output Practice (VII)

在这里插入图片描述Your task is to Calculate a b.(你的任务是计算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.
(输入将由一系列整数a和b组成,用空格隔开,每行一对整数)
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.
(对于每一对输入的整数a和b,你应该输出a和b的和,后面跟着一个空行)
Sample Input(样例输入)
1 5
10 20
Sample Output(样例输出)
6

30

正解

同样是多组数据的输入,相比于第一题,它的输出格式发生了变化,注意换行格式即可。

#include <stdio.h>

int main()
{int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)
    {printf("%d\n\n",a b);
    }
    return 0;
}

I - Sum Problem

在这里插入图片描述Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).

In this problem, your task is to calculate SUM(n) = 1 2 3 … n.
(在这个问题中,你的任务是计算SUM(n) = 1 2 3 … n)
Input(输入格式)
The input will consist of a series of integers n, one integer per line.(输入将由一系列整数n组成,每行一个整数)
Output(输出格式)
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
(对于每种情况,输出SUM(n)在一行中,后面跟着一个空行。你可以假设结果将在32位带符号整数的范围内)
Sample Input(样例输入)
1
100
Sample Output(样例输出)
1

5050

正解

不同于一般的计算SUM(n) =,此题也是一个多数据输入,此外还需要注意输出的格式需要换行。

#include <stdio.h>

int main()
{int n,sum=0,i;
    while (scanf("%d",&n)!=EOF)
    {for ( i = 1; i <=n; i  )
        {sum =i;
        }
        printf("%d\n",sum);
        sum=0;
        printf("\n");
        
    }
    return 0;  
}

以上内容就是我这次学习的感想和收获了,希望对大家的学习有所帮助,如果代码有bug或其他问题请与我联系,感谢浏览。

来源:https://www./content-4-823751.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多