原题链接:蓝桥杯算法提高VIP-高精度乘法
解题思路:
哎,我太笨啦。
逆序字符串 → 乘积 → 进位 → 反转存在字符串里 → 返回字符串。
大佬教教我简单的方法
参考代码:
#include<bits/stdc++.h>
using namespace std;
void change(char num[], int length) {
for (int i = 0; i < length / 2; i++) {
char temp = num[i];
num[i] = num[length - i - 1];
num[length - i - 1] = temp;
}
}
char *multiply(char str1[], char str2[]) {
int length1 = strlen(str1), length2 = strlen(str2);
change(str1, length1); change(str2, length2);
int *product = new int[length1 + length2]();
char *poi = new char[length1 + length2 + 1]();
for (int i1 = 0; i1 < length1; i1++)
for (int i2 = 0; i2 < length2; i2++)
product[i1 + i2] += (str1[i1] - '0')*(str2[i2] - '0');
for (int i = 0; i < length1 + length2; i++)
if (product[i] > 9) {
product[i + 1] += product[i] / 10;
product[i] = product[i] % 10;
}
int pos = length1 + length2 - 1;
while (pos >= 0 && product[pos] == 0) pos--;
if (pos < 0) *poi = '0';
else
for (int i = 0; i <= pos; i++)
poi[pos - i] = product[i] + '0';
delete[] product;
return poi;
}
int main() {
char num1[10001], num2[10001];
cin >> num1 >> num2;
char *poi = multiply(num1, num2);
puts(poi);
delete[] poi;
return 0;
}0.0分
4 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复