当前位置:网站首页>华为机试--高精度整数加法
华为机试--高精度整数加法
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
边栏推荐
- LabVIEW 小端序和大端序区别
- RuntimeError: output with shape [4, 1, 512, 512] doesn‘t match the broadcast shape[4, 4, 512, 512]
- RuntimeError: output with shape [4, 1, 512, 512] doesn‘t match the broadcast shape[4, 4, 512, 512]
- MATLAB lit plusieurs diagrammes fig et les combine en un seul diagramme (sous forme de sous - Diagramme)
- Matlab minimalist configuration of vscode configuration
- 中国移动日赚2.85亿很高?其实是5G难带来更多利润,那么钱去哪里了?
- Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
- Set classic topics
- 小红书被曝整体裁员20%,大厂之间内卷也很严重
- VHDL语言实现32位二进制数转BCD码
猜你喜欢
STM32上μC/Shell移植与应用
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
智能电子秤全国产化电子元件推荐方案
VSCode配置之Matlab极简配置
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
减治思想——二分查找详细总结
Cortex-M3寄存器组、汇编语言与C语言的接口介绍
Why recommend you to study embedded
[AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022
随机推荐
A new method for evaluating the quality of metagenome assembly - magista
阿里云IoT流转到postgresql数据库方案
Operating skills of spot gold_ Wave estimation curve
Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!
Photoshop installation under win10
C语言: 指针的进阶
【论文阅读】【3d目标检测】point transformer
Shopping mall for transportation tools based on PHP
What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
Xshell、Xftp连接新创建的Unbutu系统虚拟机全流程
Who will answer the question?
HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
Machine translation baseline
matlab读取多张fig图然后合并为一张图(子图的形式)
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
Does China Mobile earn 285 million a day? In fact, 5g is difficult to bring more profits, so where is the money?
UDP protocol and TCP protocol
【BIM入门实战】Revit中的墙体层次以及常见问题解答
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism