当前位置:网站首页>华为机试--高精度整数加法
华为机试--高精度整数加法
2022-04-23 04:15:00 【学到老才能活到老】
描述:
输入两个用字符串 str 表示的整数,求它们所表示的数之和。
数据范围: 1≤len(str)≤10000
输入描述:
输入两个字符串。保证字符串只含有'0'~'9'字符
输出描述:
输出求和后的结果
示例1:
输入:9876543210
1234567890
输出:11111111100
解法:
这逻辑,我自己看着都像有bug
#include<stdio.h>
#include<string.h>
#define MAX 10000
int main() {
char str1[MAX] = {};
char str2[MAX] = {};
int result[MAX] = {0}; // 存储计算结果
scanf("%[^\n]\n", str1);
scanf("%[^\n]\n", str2);
int len1 = strlen(str1), len2 = strlen(str2);
int len = (len1 > len2) ? len1 : len2;
int count, flag = 0; // 分别表示每一轮计算的结果和进位值
for (int i = 0; i < len; i++, len1--, len2--) {
if (len1 > 0 && len2 > 0) {
count = str1[len1 - 1] + str2[len2 - 1] - '0' - '0' + flag;
result[i] = count % 10;
if (count > 9) {
flag = 1;
} else {
flag = 0;
}
} else {
count = (len1 > len2 ? str1[len1 - 1] : str2[len2 - 1]) - '0' + flag;
result[i] = count % 10;
if (count > 9) {
flag = 1;
} else {
flag = 0;
}
}
}
if (count > 9) { // 次高位如果进位,最高位则为1
result[len] = 1;
}
// 逆序输出
if (result[len] != 0) printf("%d", result[len]);
for (int i = len - 1; i >= 0; i--) {
if (result[i] >= 0) printf("%d", result[i]);
}
printf("\n");
return 0;
}
版权声明
本文为[学到老才能活到老]所创,转载请带上原文链接,感谢
https://blog.csdn.net/MARS_098/article/details/124352125
边栏推荐
- 网络原理 | TCP/IP中的连接管理机制 重要协议与核心机制
- 列表、元组、字典和集合的区别
- VHDL implementation of 32-bit binary to BCD code
- [AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022
- [echart] Introduction to echart
- Express中间件①(中间件的使用)
- Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!
- Set经典小题目
- Xshell、Xftp连接新创建的Unbutu系统虚拟机全流程
- 创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
猜你喜欢
[AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
[mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)
[AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
Machine translation baseline
[BIM introduction practice] wall hierarchy and FAQ in Revit
【Echart】echart 入门
Express middleware ② (classification of Middleware)
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022
随机推荐
Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
Qtspim manual - Chinese Translation
Thought of reducing Governance -- detailed summary of binary search
The latest price trend chart and trading points of London Silver
记录一下盲注脚本
Cortex-M3寄存器组、汇编语言与C语言的接口介绍
Express middleware ② (classification of Middleware)
A new method for evaluating the quality of metagenome assembly - magista
【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
Common string processing functions in C language
IEEE Transactions on Industrial Informatics(TII)投稿须知
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
matlab讀取多張fig圖然後合並為一張圖(子圖的形式)
列表、元组、字典和集合的区别
VSCode配置之Matlab极简配置
【BIM+GIS】ArcGIS Pro2.8如何打开Revit模型,BIM和GIS融合?
洛谷P1858 【多人背包】 (背包求前k优解)
UDP protocol and TCP protocol
C语言常用字符串处理函数