HzuWHF


私信TA

用户名:I7I08I9047

访问量:76379

签 名:

我RUN了

等  级
排  名 18
经  验 20448
参赛次数 13
文章发表 127
年  龄 3
在职情况 学生
学  校 贺州学院
专  业

  自我简介:

解题思路:

        哎,我太笨啦。

                逆序字符串 → 乘积 → 进位 → 反转存在字符串里 → 返回字符串。

        大佬教教我简单的方法QQ图片20180609223812.jpg
参考代码:

#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分

5 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

这个已经够简单了
2021-08-17 16:05:56
  • «
  • 1
  • »