当前位置:网站首页>C language introduction practice (11): enter a group of positive integers and sum the numbers in reverse order
C language introduction practice (11): enter a group of positive integers and sum the numbers in reverse order
2022-07-23 14:39:00 【liberg】
This is a 《C Language introduction practice 》 Series No 11 piece .
Last one :C Language introduction practice (10): Mold taking problem
List of articles
subject
Any set of positive integers entered on the keyboard ( Any number ), Find the sum of all numbers in reverse order , The number in reverse order here refers to the number read from right to left , for example 123 The number in reverse order is 321,1200 The number in reverse order is 21. End the program when entering a non number . Please program to solve this problem .
requirement
Example of input / output format 1:
Input :170< enter >
x< enter >
Output :71
Example of input / output format 2:
Input :21< enter >
130< enter >
89< enter >
125< enter >
90< enter >
a< enter >
Output :671
Reference code
#include <stdio.h>
#include <stdlib.h>
// Define a function , Calculate input integer n In reverse order
int reverseNum(int n) {
int a = 0;
while(n>0) {
a = a*10 + n%10;
n /= 10;// namely n=n/10
}
return a;
}
int main(int argc, char *argv[]) {
int result = 0;
int n;
char ch;
int flag = 1;
while(flag) {
n = 0;
ch = getchar();
while(ch!='\n' && ch!=EOF) {
if (ch>='0' && ch <='9') {
n = n*10 + ch-'0';
} else {
// Read characters that are not positive integers , Set exit flag
flag = 0;
break;
}
ch = getchar();
}
// Corresponding to the legal positive integer value in the input group , Get its inverse ordinal number , Add to the answer
result += reverseNum(n);
}
printf("%d", result);
return 0;
}
Code duplicate
We define a function ( Functions are used to encapsulate reusable code units ),int reverseNum(int n), According to the entered integer value n Calculation return n In reverse order .
&& yes Logic and Symbol , yes “ also ” It means .
getchar() In the standard library header file stdio.h In a statement , Used to input from standard ( It's usually a keyboard ) Read a character .
EOF yes stdio.h Constants defined in ( Constant value is -1), Indicates that you have read the file / End of standard input . Because normally , Read a byte from a file or standard input , This byte must be >=0 The characters of , So it can be stipulated -1 Constant expression reading is over .
边栏推荐
- CSDN写文方法(二)
- Dynamic programming -- knapsack problem
- 第三章 复杂一点的查询
- Some libraries that can perform 2D or 3D triangulation
- 关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论
- Chapter 3 complex query
- Consensys Quorum Benchmark Test
- c语言实现strcmp、strstr、strcat、strcpy
- Optimize Huawei ECs to use key login
- Due to resource constraints, the namenode fails to start with an error unable to create new native thread
猜你喜欢

First acquaintance and search set

Tensor, numpy, PIL format conversion and image display

C language implementation of classroom random roll call system

How can manual testing turn to automated testing? Byte 5 years of automation experience talk about

Which is a good fixed asset management system? What are the fixed asset management platforms?
![Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)](/img/f0/9491ccc2a86d95bb30066397fb9fb6.png)
Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)

Wacom firmware update error 123, digital board driver cannot be updated

uni-app知识点和项目上遇到的问题和解决办法的记录

About flex layout justify content: the last solution to the misalignment of space around and why it is solved like this is a discussion

手工测试如何转向自动化测试?字节5年自动化经验浅谈一下...
随机推荐
利用js自动解析执行xss
Work notes: one time bag grabbing
右键新建txt,新建文本文件不见了,通过添加注册表就可以解决,找来找去办法解决不了的终极办法
[WinForm] desktop program implementation scheme for screenshot recognition and calculation
【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
FFmpeg 2 - ffplay、ffprobe、ffmpeg 命令使用
Spotlight light box JS plug-in full screen enlarged picture
优化华为云服务器采用Key登陆
(重链剖分)魔法树
Chapitre 2 requête de base et tri
Cool code rain dynamic background registration page
炫酷代码雨动态背景注册页面
Offline data interoperability
【附下载】值得收藏的几款渗透测试常用的脚本
剑指 Offer 46. 把数字翻译成字符串
【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
C语言实现课堂随机点名系统
JS calendar style pie chart statistics plug-in
Authing supports Zadig! Unified authentication and rapid docking of cloud native users
After using vscode to format the code, save and find that the code is messy again. What should I do? Vs remove formatting