当前位置:网站首页>华为机试--高精度整数加法
华为机试--高精度整数加法
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
边栏推荐
- UDP protocol and TCP protocol
- The latest price trend chart and trading points of London Silver
- 【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
- [string] ranking of country names ----- problem solving notes
- What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
- VSCode配置之Matlab极简配置
- 【论文阅读】【3d目标检测】point transformer
- Hard core chip removal
- 无线充电全国产化电子元件推荐方案
- [BIM introduction practice] wall hierarchy and FAQ in Revit
猜你喜欢

Network principle | connection management mechanism in TCP / IP important protocol and core mechanism

STM32F4单片机ADC采样及ARM-DSP库的FFT

Express middleware ② (classification of Middleware)

Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data

Cortex-M3寄存器组、汇编语言与C语言的接口介绍
![[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022](/img/48/0e95841743bada4faf3edfee31cb6a.png)
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022

无线充电全国产化电子元件推荐方案

Why recommend you to study embedded

Thought of reducing Governance -- detailed summary of binary search

【Echart】echart 入门
随机推荐
秒杀所有区间相关问题
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
无线键盘全国产化电子元件推荐方案
matlab读取多张fig图然后合并为一张图(子图的形式)
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
Express middleware ① (use of Middleware)
【Echart】echart 入門
Basic introduction to spot gold
[AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021
智能电子秤全国产化电子元件推荐方案
IEEE Transactions on Industrial Informatics(TII)投稿须知
【Pytorch基础】torch.split()用法
记录一下盲注脚本
CRF based medical entity recognition baseline
列表、元组、字典和集合的区别
[BIM introduction practice] wall hierarchy and FAQ in Revit
AI CC 2019 installation tutorial under win10 (super detailed - small white version)
Common string processing functions in C language
单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真