当前位置:网站首页>Huawei machine test -- high precision integer addition
Huawei machine test -- high precision integer addition
2022-04-23 04:32:00 【It's never too old to learn】
describe :
Enter two strings str The whole number represented , Find the sum of the numbers they represent .
Data range : 1≤len(str)≤10000
Input description :
Enter two strings . Ensure that the string contains only '0'~'9' character
Output description :
Output the sum result
Example 1:
Input :9876543210
1234567890
Output :11111111100
solution :
This logic , I look like I have bug
#include<stdio.h>
#include<string.h>
#define MAX 10000
int main() {
char str1[MAX] = {};
char str2[MAX] = {};
int result[MAX] = {0}; // Store the calculation results
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; // Represent the result and carry value of each round of calculation respectively
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) { // Secondary high if carry , The highest bit is 1
result[len] = 1;
}
// Output in reverse order
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;
}
版权声明
本文为[It's never too old to learn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230415001831.html
边栏推荐
- IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
- Understand the gut organ axis, good gut and good health
- matlab讀取多張fig圖然後合並為一張圖(子圖的形式)
- 基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
- A lifetime of needs, team collaboration can play this way on cloud nailing applet
- C语言: 指针的进阶
- 从MySQL数据库迁移到AWS DynamoDB
- PHP export excel table
- 【BIM入门实战】Revit中的墙体层次以及常见问题解答
- Coinbase:关于跨链桥的基础知识、事实和统计数据
猜你喜欢

单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真

Chapter 4 - understanding standard equipment documents, filters and pipelines

Set classic topics

OpenCV----YOLACT实例分割模型推理
![[mapping program design] coordinate azimuth calculation artifact (version C)](/img/2b/e640f3e2702f80d003fa3dd5c4158d.png)
[mapping program design] coordinate azimuth calculation artifact (version C)

AWS EKS 部署要点以及控制台与eksctl创建的差异
![[echart] démarrer avec echart](/img/40/e057f4ac07754fe6f3500f3dc72293.jpg)
[echart] démarrer avec echart

229. 求众数 II

QtSpim手册-中文翻译

Installation and use of Apache bench (AB pressure test tool)
随机推荐
【Pytorch基础】torch.split()用法
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
IEEE Transactions on Industrial Informatics(TII)投稿须知
顺序表的基本操作
Shopping mall for transportation tools based on PHP
Bacterial infection and antibiotic use
单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真
单片机串口数据处理(2)——uCOSIII+循环队列接收数据
Set经典小题目
What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
指纹Key全国产化电子元件推荐方案
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
[BIM introduction practice] wall hierarchy and FAQ in Revit
QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
php导出Excel表格
C language character constant
电钻、电锤、电镐的区别
thymeleaf th:value 为null时报错问题
STM32单片机ADC规则组多通道转换-DMA模式
Mysql---数据读写分离、多实例