A*B problem 个人错误分析
A*B问题
比赛题目 题目统计 时间限制:C/C++ 1000MS,其他语言 2000MS
内存限制:C/C++ 256MB,其他语言 512MB
分数:100
比赛题目 题目统计 时间限制:C/C++ 1000MS,其他语言 2000MS
内存限制:C/C++ 256MB,其他语言 512MB
分数:100
描述
计算两个非负整数A,B 的乘积,A,B 可能会很大。
#include#include <string.h> using namespace std; int temp[1002] = {0}; string a, b; //数字过大,用string先装起来,之后取出 int main() { cin >> a >> b; if (a == "0" || b == "0") cout << "0"; //判断a,b是否为"0"(直接就这样判断吧,试了几种了) else { int lena = a.length(); int lenb = b.length(); //取出a,b长度 int as[502] = {0}; int bs[502] = {0}; int i, j; for (i = lena - 1, j = 0; i >= 0; i--, j++) { as[j] = a[i] - '0'; //倒序取出,方便使用,减'0'是为了转换类型 } for (i = lenb - 1, j = 0; i >= 0; i--, j++) { bs[j] = b[i] - '0'; } for (i = 0; i < lena; i++) for (j = 0; j < lenb; j++) { temp[i + j] += as[i] * bs[j]; } //各位相乘,先把结果存在相应的位置 for (i = 0; i < (lena + lenb); i++) { if (temp[i] > 9) { temp[i + 1] += temp[i] / 10; temp[i] %= 10; //单独执行进位操作 } } int ret; //数组开得比较大,输出时先去掉前置0 for (ret = lena + lenb; ret >= 0; ret--) { if (temp[ret] == 0) continue; else break; } for (; ret >= 0; ret--) cout << temp[ret]; //之前进行了倒序操作,所以倒序输出结果 } return 0; } /*虽然很早就见过这类题,但从来没有认真思考过,这次虽然没能自己做出来,但搞懂了许多,自己大概也能写出来了*/
输入描述
第一行输入一个非负整数 A。
第二行输入一个非负整数 B。
A,B 的长度不大于 500。
输出描述
输出 A×B 的值。
用例输入 1
4321 1234
用例输出 1
5332114