当前位置:网站首页>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
边栏推荐
- Set classic topics
- 【测绘程序设计】坐标方位角推算神器(C#版)
- Redis 命令大全
- Hard core chip removal
- 国外LEAD,联盟经理常见问答
- As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
- Matlab minimalist configuration of vscode configuration
- [AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
- 阿里云IoT流转到postgresql数据库方案
- RC低通滤波器的逆系统
猜你喜欢
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
Thought of reducing Governance -- detailed summary of binary search
Express middleware ② (classification of Middleware)
Jetpack 之 LifeCycle 组件使用详解
洛谷P1858 【多人背包】 (背包求前k优解)
Installation du compilateur croisé de la plateforme zynq
AWS EKS 部署要点以及控制台与eksctl创建的差异
Effects of antibiotics on microbiome and human health
【Echart】echart 入门
单片机串口数据处理(2)——uCOSIII+循环队列接收数据
随机推荐
Alibaba cloud IOT transfer to PostgreSQL database scheme
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
Redis 命令大全
【BIM+GIS】ArcGIS Pro2.8如何打开Revit模型,BIM和GIS融合?
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
Chlamydia infection -- causes, symptoms, treatment and Prevention
Mysql---数据读写分离、多实例
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism
php导出Excel表格
Bacterial infection and antibiotic use
Go 语言中的 logger 和 zap 日志库
单片机串口数据处理(2)——uCOSIII+循环队列接收数据
thymeleaf th:value 为null时报错问题
matlab讀取多張fig圖然後合並為一張圖(子圖的形式)
三十六计是什么
The difference between lists, tuples, dictionaries and collections
Nel ASA: her ø Ya facility in Norway officially opened
A lifetime of needs, team collaboration can play this way on cloud nailing applet
Why recommend you to study embedded
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection