当前位置:网站首页>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);}}}
边栏推荐
- 剪映专业版字幕导出随笔
- MFC读取点云,只能正常显示第一个,显示后面时报错
- Pytest常用插件
- latex-写论文时一些常用设置
- 详解近端策略优化
- The second official example analysis of the MOOSE platform - about creating a Kernel and solving the convection-diffusion equation
- [日常办公][shell]常用代码段
- 迅雷关闭自动更新
- PyTorch
- Copy Siege Lion 5-minute online experience MindIR format model generation
猜你喜欢
LeetCode_Nov_4th_Week
Amazon Cloud Technology Build On-Amazon Neptune's Knowledge Graph-Based Recommendation Model Building Experience
【论文阅读】Mining Cross-Image Semantics for Weakly Supervised Semantic Segmentation
Lee‘s way of Deep Learning 深度学习笔记
安装MySQL的详细步骤
安装pyspider后运行pyspider all后遇到的问题
arm-2-基础阶段
target has libraries with conflicting names: libcrypto.a and libssl.a.
Tencent and NetEase have taken action one after another. What is the metaverse that is so popular that it is out of the circle?
亚马逊云科技Build On-Amazon Neptune基于知识图谱的推荐模型构建心得
随机推荐
理想的生活
管道重定向
LeetCode_22_Apr_2nd_Week
度量学习(Metric learning、损失函数、triplet、三元组损失、fastreid)
Copy Siege Lions "sticky" to AI couplets
数据库的简述与常用操作指南
No matching function for call to ‘RCTBridgeModuleNameForClass‘
光条中心提取方法总结(二)
【论文阅读】Exploring Spatial Significance via Hybrid Pyramidal Graph Network for Vehicle Re-identificatio
Copy攻城狮的年度之“战”|回顾2020
安装pyspider后运行pyspider all后遇到的问题
第三章 标准单元库(上)
LeetCode_Nov_3rd_Week
空洞卷积
target has libraries with conflicting names: libcrypto.a and libssl.a.
CSDN spree -- college round table spree
AWS使用EC2降低DeepRacer的训练成本:DeepRacer-for-cloud的实践操作
arm学习-1-开发板
PyTorch
【论文阅读】Further Non-local and Channel Attention Networks for Vehicle Re-identification