当前位置:网站首页>题目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);
}
}
}
边栏推荐
- PP-LiteSeg
- Lee‘s way of Deep Learning 深度学习笔记
- TensorRT 5 初步认识
- 深度学习理论 —— 初始化、参数调节
- YOLOV4流程图(方便理解)
- 【Copy攻城狮日志】“一分钟”跑通MindSpore的LeNet模型
- 图像线性融合
- 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
猜你喜欢
Brief description of database and common operation guide
LeetCode_Dec_1st_Week
Golang环境变量设置(二)--GOMODULE&GOPROXY
LeetCode_Dec_3rd_Week
No matching function for call to ‘RCTBridgeModuleNameForClass‘
【论文阅读】TransReID: Transformer-based Object Re-Identification
【深度学习日记】第一天:Hello world,Hello CNN MNIST
Copy攻城狮5分钟在线体验 MindIR 格式模型生成
剪映专业版字幕导出随笔
Golang environment variable settings (2)--GOMODULE & GOPROXY
随机推荐
Pytest常用插件
关于DG(域泛化)领域的PCL方法的代码实例
JPEG2jpg
中国联通、欧莱雅和钉钉都在争相打造的秘密武器?虚拟IP未来还有怎样的可能
No matching function for call to ‘RCTBridgeModuleNameForClass‘
Copy Siege Lion 5-minute online experience MindIR format model generation
AWS uses EC2 to reduce the training cost of DeepRacer: DeepRacer-for-cloud practical operation
PostgreSQL schema (Schema)
审稿意见回复
Deep Adversarial Decomposition: A Unified Framework for Separating Superimposed Images
Qt日常学习
CSDN大礼包--高校圆桌派大礼包
DeblurGAN-v2: Deblurring (Orders-of-Magnitude) Faster and Better 图像去模糊
【论文阅读】SPANET: SPATIAL PYRAMID ATTENTION NETWORK FOR ENHANCED IMAGE RECOGNITION
Copy Siege Lions "sticky" to AI couplets
MNIST手写数字识别 —— ResNet-经典卷积神经网络
Attention Is All You Need(Transformer)
数据库的简述与常用操作指南
Tencent and NetEase have taken action one after another. What is the metaverse that is so popular that it is out of the circle?
【论文阅读】Multi-View Spectral Clustering with Optimal Neighborhood Laplacian Matrix