【C语言】蓝桥杯/ACM竞赛入门 A+B for Input-Output Practice-程序员宅基地

技术标签: 把题目都给刷干净  c语言  蓝桥杯  

引子

上次参加了学校的蓝桥杯校队选拔“集训”

第一次“测试”就直接被考傻了,虽然都是我“好像"学过的内容,但我里里外外真的看不出来到底怎么写,太离谱了!

而且学长用的都是C++,我只学了c语言,后面的题目完全看不懂了。

当然,归根结底还是我太菜了

在这里把我搞了好久终于弄懂的A+B题目分享给大家

  • 开始集训:A+B不有手就行?
  • 结束后:我手呢?

注:题目是英文的!!!!


A+B for Input-Output Practice (I)

Problem Description

Your task is to Calculate 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.

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 

这道题看起来非常简单,我一上来就打上了两行scanf,啪的一下就提交了

然后系统啪的一下给我返回了一个大大的红色WRONG ANSWER

正确做法如下:

#include<stdio.h>  
int main()  
{
     
   int a,b; 
   while(scanf("%d%d",&a,&b)!=EOF)
       printf("%d\n",a+b);  
}   

不能直接使用两行scanf的原因是

题目需要的是这串代码能完整地使用多次,而不是简单的只执行两次

在while里使用EOF的原因

这里使用eof,可以让程序在未输入错误的情况下一直进行循环计算a+b

scanf函数的返回值

scanf函数返回成功读入的数据项数,读入数据时遇到了“文件结束”则返回EOF。

如:scanf("%d %d",&a,&b);

函数返回值为int型。如果a和b都被成功读入,那么scanf的返回值就是2;

如果只有a被成功读入,返回值为1;

如果a和b都未被成功读入,返回值为0;

如果遇到错误或遇到end of file,返回值为EOF。end of file为Ctrl+z 或者Ctrl+d。

[摘自百度知道用户@纵横竖屏的回答]

EOF的值为-1,但不能简单的用-1代替EOF


A+B for Input-Output Practice (II)

Problem Description

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

这道题的意思其实就是在键入需要相加的数字之前,先键入需要相加的数字组数(也可以理解为行数)

要求我们用循环的方式完成“相加数字行数”的操作

完成n行后需要跳出该a+b的循环

#include<stdio.h>
int main()
{
    
    int a, b;
    int i, j;
    int n;
    scanf("%d", &n);//n即为行数
    int sum[20] = {
     0 };
    for (i = 0; i < n; i++)
    {
    
        scanf("%d %d", &a, &b);
        sum[i] = a + b;
    }
    for (j = 0; j < n; j++)
    {
    
        printf("%d\n", sum[j]);
    }
}

A+B for Input-Output Practice (III)

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

这道题的要求是a+b的程序在读取到两个0 0的时候会退出循环

且代码不会处理这两个0 0(把它们当作循环结束标志)

#include <stdio.h>
int main()
{
    
	int a = 0, b = 0, c;
	while (1)
	{
    
		scanf("%d%d", &a, &b);
		if (a == 0 && b == 0)//判断键入的是否为0 0
		{
    
			break;//是0 0,退出循环
		}
		else//不是0 0,继续运行a+b
		{
    
			c = a + b;
			printf("%d\n", c);
		}
		
	}
	return 0;
}

A+B for Input-Output Practice (IV)

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

与前面的两个数字相加不同,这道题的要求是,先输入你需要相加的数字的个数,再依次键入数字

遇到0的时候停止循环

  • 这个0必须是第一个数字,即代表需要“0个数字相加”
  • 如果是需要相加的数字里有0,不应退出循环
#include<stdio.h>
int main() 
{
    
	int n, sum, i, t;
	while (scanf("%d", &n) != EOF && n != 0) //判断是否等于0
	{
    
		sum = 0;
		for (i = 0; i < n; i++) 
		{
    
			scanf("%d", &t);
			sum = sum + t;//不断往sum里面加数字
		}
		printf("%d\n", sum);
	}
}

A+B for Input-Output Practice (V)

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

这道题和上一道题的要求差不多,结合了第二题的内容,即我们在键入需要相加的数字之前,要先键入“行数"

当然它也少了在遇到0的时候退出循环

#include<stdio.h>

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

A+B for Input-Output Practice (VI)

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

这道题和第四题很像,就缺少了遇到0停止

#include<stdio.h>
int main() 
{
    
	int n, sum, i, t;
	while (scanf("%d", &n) != EOF) 
	{
    
		sum = 0;
		for (i = 0; i < n; i++) 
		{
    
			scanf("%d", &t);
			sum = sum + t;
		}
		printf("%d\n", sum);
	}
}

A+B for Input-Output Practice (VII)

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

这道题多了一个要求,“followed by a blank line.”

需要我们在代码完成后,跟着打印一个空行,也就是\n

#include <stdio.h>

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

A+B for Input-Output Practice (VIII)

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

这道题的要求结合了上面的题目

  • 需要先键入“行数”
  • 需要在相加之前键入 需要相加的数字的个数
  • 打印答案后需要再打印一个 空行

也不怕大家笑话,这道题是我抄的,原博客在这 [点我]

原博客中在for循环的scanf前多了一个if(i==0)的代码,我实在没看懂这句代码的意义
去掉之后系统依旧判对!

#include<stdio.h>
int main()
{
    
    int test,t=0;
     scanf("%d",&test);//行数
    while(test--)//每执行一次,减少一行
    {
    
    int s=0,i;
    int n,a[1000];//用数组的方式完成相加
    scanf("%d",&n);//需要相加的数字的个数
    for(i=0;i<n;i++)
    {
    

            scanf("%d",&a[i]);
            s+=a[i];
    }
    t++;
    if(t>1)
        printf("\n");
    printf("%d\n",s);
    }
 return 0;
}

呼!终于写完这篇博客了

如果这对你有帮助,还请点赞关注收藏吧!

这对我真的很重要!

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/muxuen/article/details/120979866

智能推荐

Java实现非递归归并排序-程序员宅基地

文章浏览阅读240次。public class nonRecursiveMergeSort {public static void main(String[] args) { int[] list = {8,4,3,6,9}; MergeSort(list); for(int num:list) System.out.print(num);}public sta..._java 归并排序非递归

OpenWrt常见问题汇总_openwrt没有路由器的型号-程序员宅基地

文章浏览阅读4.8k次。目录1.***error:'OpenWrtunknown'istoolong,maxfirwareversionlengthis132.DownLoad failed3.OpenWrt的Target Profile找不到自己的路由器型号 4.编译错误you should not run configure as root (set FORCE_..._openwrt没有路由器的型号

source insight 工程项目路径替换_source insight 4.x 替换文件-程序员宅基地

文章浏览阅读1.1k次。然后切换到sourceinsight,刚才的弹窗还在,点击Addfromlist按钮,选择刚才完成路径替换的.txt文件,等待导入完成,至此,完成路径替换,又不用再添加一遍文件。Project->AddandRemoveProjectFiles->RemoveAll,移除原来所有文件,完毕后,当前弹窗别关。Project->ExportProjectFileList,点保存.txt文件。找到刚才保存的.txt文件位置,打开并批量替换路径。..._source insight 4.x 替换文件

java做一个文件存储回滚的实现_java中文件可以回滚吗-程序员宅基地

文章浏览阅读798次。思路:操作任意表之前,先对表做备份,如果出了异常,那么还原备份。采用kv存储 nosql存储main函数:public class TestDemo { public static void main(String[] args) { // 数据初始化 Car car = new Car(); car.setCarAge("0"); car.setCarBirth("2022-01-30"); car.setC_java中文件可以回滚吗

display: weston: opaque region笔记_pixman_region32_intersect-程序员宅基地

文章浏览阅读1.1k次。client的设置方法:region = wl_compositor_create_region(window->display->compositor);wl_region_add(region, 0, 0, window->geometry.width, window->geometry.height);wl_surface_set_opaque_region(window->surface, region);wl_region_de_pixman_region32_intersect

matlab double to int,如何将matlab中的sym数据类型转换为double型-程序员宅基地

文章浏览阅读7.7k次。满意答案a19850429推荐于 2016.11.30采纳率:47%等级:8已帮助:862人一般来说用dec2hex及hex2dec就可以。ss='010600001388849C';ssDec = hex2dec(ss);ssHex = dec2hex(ssDec);format hex;disp(ssHex);不过前提是你的机器能处理这么大的数。我用小一点的数比如说ss='01600..._matlab将sym类型变为double类型

随便推点

猪猪猫.CN-WINXP-SP2 2008.04[安装旗舰DVD版]_winxp dvd.iso-程序员宅基地

文章浏览阅读1.1k次。http://bt.jujumao.cn/view.php?id=1111HTTP/://WWW.JUJUMAO.COM荣誉出品======================================================================================================================================_winxp dvd.iso

opencv关于图像前景提取-程序员宅基地

文章浏览阅读9.1k次,点赞10次,收藏53次。在图像处理的过程中,会经常碰到需要提取选定区域的前景,以下就使用grabCut算法进行图像前景的提取(我是用的opencv版本为opencv2.4.3.)。这是我的第一篇博客,有什么不足的地方欢迎大家指导。关于grabCut算法的原理,我也是参照大牛写的文章,[这里]就是原文地址。(https://blog.csdn.net/zouxy09/article/details/8534954)在进..._图像前景提取

【Mac使用技巧】Mac中文输入法打不出来句号_mac打不出来中文句号-程序员宅基地

文章浏览阅读2.2w次,点赞17次,收藏3次。解决办法:点击导航栏里的输入法, 找到use halfwidth punctuation ,如果有勾, 取消选中就行了。此文章本人原创,如有问题底部留言,如有转载请标明出处。--HurryChen Happy Coding! *\( ^ v ^ )/*..._mac打不出来中文句号

腾讯信鸽推送(七):魅族推送通道配置_信鸽 魅族推送-程序员宅基地

文章浏览阅读328次。后台不知道填写的魅族appid是哪个? 腾讯信鸽后台填写的是6位数的appid,如下图所示 需要腾讯信鸽插件请联系企业即时通讯-触点通QQ:1417249367..._信鸽 魅族推送

python-pcl文档_python-pcl 学习教程-程序员宅基地

文章浏览阅读765次。https://python-pcl-fork.readthedocs.io/en/latest/tutorial/_python-pcl 学习教程

Taro/TraoUI框架使用过程中的一些坑-程序员宅基地

文章浏览阅读7.8k次。使用TaroUI组件 @import样式不好使 / 不能够覆盖样式问题Textarea 组件层级穿透问题_traoui