当前位置:网站首页>Question 1000: Input two integers a and b, calculate the sum of a+b, this question is multiple sets of test data
Question 1000: Input two integers a and b, calculate the sum of a+b, this question is multiple sets of test data
2022-08-04 06:29:00 【Ka Ka Lu】
C language learning website:https://www.docpp.com/course/5
C language:
#include /*Introduce header files*/int main(void) /* a simple C program */{int number; /*define a variable named number*/number=2014; /*Assign a value to number*/printf("Hello ! I am dotcpp.com\n"); /*call printf() function*/printf("This year is %d\n",number);return 0;}
Question 1: Input two integers a and b, calculate the sum of a+b
Note that this question is multiple sets of test data, that is, you need to continuously receive the test input of the system, you can calculate the result and output it
Method 1:
#include int main(){int a,b; //Define two variables a,b, the type is integerwhile(~scanf("%d%d",&a,&b)) //Continue to loop if non-zeroprintf("%d\n",a+b); //Enter the loop statement, if a and b are given a value, output a+b; %d replaces the unknown integerreturn 0; //return value}
Display of running results:
Solution ideas:
Function name: scanf
Function: Execute formatted input
Usage: int scanf(char *format[...,argurment,....]);
The scanf() function uses a generic terminal formatted input function to read input information from the standard input device (keyboard).Can read in any inherent type of data and automatically convert the value to the appropriate on-board format.
The calling format is: scanf("
Analysis of the role of ~:
1. Under Windows, if the user presses CTRL+Z (you will see a ^Z character), the input stream will be stopped, and scanf will return -1.
The complement of 2 and -1 is 11111111 11111111 11111111 11111111 a total of 4 bytes.
3, ~ is the bitwise inversion in C language, so the result of ~(-1) is 00000000 00000000 00000000 00000000 is exactly the complement of integer 0.
4. Therefore, when Ctrl+Z is input, scanf will return -1, while(~-1)==while(0), 0 is false, and exit the while loop.
Method 2:
#includeint main(){int a,b;while(scanf("%d%d", &a, &b)==2)//When ==2, that is, the case where the scanf() function returns a value of 1 (see below)printf("%d\n",a+b);return 0;}
[Note] The return value of the scanf function is the key point and must be understood.
The return value of the scanf() function is divided into three types:
(1) returns a positive integer.Indicates the number of correct input parameters.
(2) returns the integer 0.Indicates that the user's input did not match and no value could be entered correctly.
(3) returns -1.Indicates that the input stream has ended.Under Windows, the user presses CTRL+Z (you will see a ^Z character) and then presses Enter (which may need to be repeated several times), which means the end of the input; under Linux/Unix, use CTRL+D to indicate the end of the input.
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);}}}
边栏推荐
- 数据库的简述与常用操作指南
- 【论文阅读】SPANET: SPATIAL PYRAMID ATTENTION NETWORK FOR ENHANCED IMAGE RECOGNITION
- sbl_init.asm-适合在编辑模式下看
- No matching function for call to 'RCTBridgeModuleNameForClass'
- Thunderbolt turns off automatic updates
- [开发杂项][编辑器][代码阅读]ctags&vim
- LeetCode_Nov_3rd_Week
- MFC 打开与保存点云PCD文件
- (导航页)OpenStack-M版-双节点手工搭建-附B站视频
- 典型CCN网络——efficientNet(2019-Google-已开源)
猜你喜欢
LeetCode_Nov_5th_Week
lstm pipeline 过程理解(输入输出)
安装pyspider后运行pyspider all后遇到的问题
YOLOV5 V6.1 详细训练方法
Deep learning, "grain and grass" first--On the way to obtain data sets
【论文阅读】SPANET: SPATIAL PYRAMID ATTENTION NETWORK FOR ENHANCED IMAGE RECOGNITION
Lee‘s way of Deep Learning 深度学习笔记
度量学习(Metric learning)—— 基于分类损失函数(softmax、交叉熵、cosface、arcface)
管道重定向
target has libraries with conflicting names: libcrypto.a and libssl.a.
随机推荐
计算某像素点法线
迅雷关闭自动更新
arm交叉编译
空洞卷积
Pytorch问题总结
pytorch学习-没掌握的点
Copy Siege Lion 5-minute online experience MindIR format model generation
浅谈游戏音效测试点
LeetCode_Dec_2nd_Week
MNIST手写数字识别 —— 基于Mindspore快速构建感知机实现十分类
(Navigation page) OpenStack-M version - manual construction of two nodes - with video from station B
光条提取中的连通域筛除
度量学习(Metric learning、损失函数、triplet、三元组损失、fastreid)
The usefulness of bind() system call
【Copy攻城狮日志】飞浆学院强化学习7日打卡营-学习笔记
如何用Pygame制作简单的贪吃蛇游戏
【论文阅读】Anchor-Free Person Search
浅谈外挂常识和如何防御
Postgresql snapshot
详解近端策略优化