当前位置:网站首页>题目1000:输入两个整数a和b,计算a+b的和,此题是多组测试数据
题目1000:输入两个整数a和b,计算a+b的和,此题是多组测试数据
2022-08-04 05:30:00 【咔咔鹿】
C语言学习网站:https://www.dotcpp.com/course/5
C语言:
#include<stdio.h> /*引入头文件*/
int main(void) /*一个简单的C程序*/
{
int number; /*定义个名字叫做number的变量*/
number=2014; /*给number赋一个值*/
printf("Hello ! I am dotcpp.com\n"); /*调用printf()函数*/
printf("This year is %d\n",number);
return 0;
}题目一:输入两个整数a和b,计算a+b的和
注意此题是多组测试数据,即需要不停的接收系统的测试输入,你都可以计算结果并输出
方法一:
#include <stdio.h>
int main()
{
int a,b; //定义两个变量a,b,类型为整数
while(~scanf("%d%d",&a,&b)) //非零则继续循环
printf("%d\n",a+b); //进入循环语句,若给a,b一个值,则输出a+b;%d代替不知道整数
return 0; //返回值
}
运行结果展示:

解题思路:
函数名:scanf
功能:执行格式化输入
用法:int scanf(char * format[...,argurment,....]);
scanf()函数用通用终端格式化输入函数,从标准输入设备(键盘)读取输入信息。可以读入任何固有类型的数据并自动把数值变换成适当的机内格式。
其调用格式为:scanf("<格式化字符串>",<地址表>);
关于~的作用解析:
1、在Windows下,用户按下CTRL+Z(会看到一个^Z字符),会停止输入流,scanf会返回-1。
2、-1的补码为11111111 11111111 11111111 11111111 一共4个字节。
3、~是C语言中的按位取反,因此~(-1)结果为00000000 00000000 00000000 00000000刚好为整数0的补码。
4、因此当输入Ctrl+Z时,scanf会返回-1,while(~-1)==while(0),0为假,退出while循环。
方法二:
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d%d", &a, &b)==2)//当==2,即scanf()函数返回值为1的情况(看下面)
printf("%d\n",a+b);
return 0;
}
【注意】scanf函数的返回值是重点,必须理解。
scanf()函数返回值分为3种:
(1)返回正整数。表示正确输入参数的个数。
(2)返回整数0。表示用户的输入不匹配,无法正确输入任何值。
(3)返回-1。表示输入流已经结束。在Windows下,用户按下CTRL+Z(会看到一个^Z字符)再按下回车(可能需要重复多次),就表示输入结束;Linux/Unix下使用CTRL+D表示输入结束。
Java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = a + b;
System.out.println(c);
}
}
}边栏推荐
猜你喜欢

The second official example analysis of the MOOSE platform - about creating a Kernel and solving the convection-diffusion equation
![[Copy Siege Lion Log] Flying Pulp Academy Intensive Learning 7-Day Punch Camp-Study Notes](/img/af/05caea638de8d75f6d3b42b3d8e28f.png)
[Copy Siege Lion Log] Flying Pulp Academy Intensive Learning 7-Day Punch Camp-Study Notes

MNIST手写数字识别 —— Lenet-5首个商用级别卷积神经网络

arm学习-1-开发板
![[CV-Learning] Linear Classifier (SVM Basics)](/img/94/b48e34b2c215ca47f8ca25ce97547e.png)
[CV-Learning] Linear Classifier (SVM Basics)

Tencent and NetEase have taken action one after another. What is the metaverse that is so popular that it is out of the circle?

Amazon Cloud Technology Build On 2022 - AIot Season 2 IoT Special Experiment Experience

剪映专业版字幕导出随笔

MFC读取点云,只能正常显示第一个,显示后面时报错

【深度学习日记】第一天:Hello world,Hello CNN MNIST
随机推荐
图像resize
【Copy攻城狮日志】飞浆学院强化学习7日打卡营-学习笔记
PCL窗口操作
迅雷关闭自动更新
LeetCode_Dec_1st_Week
PP-LiteSeg
Attention Is All You Need(Transformer)
A code example of the PCL method in the domain of DG (Domain Generalization)
DeblurGAN-v2: Deblurring (Orders-of-Magnitude) Faster and Better 图像去模糊
yoloV5 使用——训练速度慢,加速训练
第三章 标准单元库(下)
(导航页)OpenStack-M版-双节点手工搭建-附B站视频
MOOSE平台使用入门攻略——如何运行官方教程的例子
Copy Siege Lions "sticky" to AI couplets
Rules.make-适合在编辑模式下看
MNIST手写数字识别 —— 从感知机到卷积神经网络
【论文阅读】Multi-View Spectral Clustering with Optimal Neighborhood Laplacian Matrix
动手学深度学习__数据操作
审稿意见回复
深度学习,“粮草”先行--浅谈数据集获取之道