当前位置:网站首页>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);}}}边栏推荐
- CAS无锁队列的实现
- fuser 使用—— YOLOV5内存溢出——kill nvidai-smi 无pid 的 GPU 进程
- YOLOV4流程图(方便理解)
- MNIST手写数字识别 —— 基于Mindspore快速构建感知机实现十分类
- sbl_init.asm-适合在编辑模式下看
- 【深度学习日记】第一天:Hello world,Hello CNN MNIST
- ConnectionRefusedError: [Errno 111] Connection refused问题解决
- 光条中心提取方法总结(一)
- [开发杂项][VS Code]remote-ssd retry failed
- target has libraries with conflicting names: libcrypto.a and libssl.a.
猜你喜欢

Lee‘s way of Deep Learning 深度学习笔记

Copy攻城狮的年度之“战”|回顾2020

tensorRT5.15 使用中的注意点

语音驱动嘴型与面部动画生成的现状和趋势

MOOSE平台官方第二个例子分析——关于创建Kernel,求解对流扩散方程

基于asp.net的法律援助平台的设计与实现(附项目链接)

集合---ArrayList的底层

lstm pipeline 过程理解(输入输出)

MOOSE平台使用入门攻略——如何运行官方教程的例子

The second official example analysis of the MOOSE platform - about creating a Kernel and solving the convection-diffusion equation
随机推荐
【论文阅读】Further Non-local and Channel Attention Networks for Vehicle Re-identification
fuser 使用—— YOLOV5内存溢出——kill nvidai-smi 无pid 的 GPU 进程
第三章 标准单元库(上)
学习资料re-id
Windows10重置MySQL用户密码
[日常办公][杂项][vscode]tab space
Postgresql snapshot
Copy Siege Lion's Annual "Battle" | Review 2020
target has libraries with conflicting names: libcrypto.a and libssl.a.
IEEE802.X协议族
arm学习-1-开发板
LeetCode_Nov_2nd_Week
LeetCode_Dec_1st_Week
MNIST Handwritten Digit Recognition - Lenet-5's First Commercial Grade Convolutional Neural Network
2020-10-19
亚马逊云科技Build On-Amazon Neptune基于知识图谱的推荐模型构建心得
tensorRT教程——tensor RT OP理解(实现自定义层,搭建网络)
MNIST手写数字识别 —— Lenet-5首个商用级别卷积神经网络
审稿意见回复
tmux概念和使用