大数系列——高精度乘法
来源:互联网 发布:小蜜软件怎么样 编辑:程序博客网 时间:2024/05/19 05:39
Product
The Problem
The problem is to multiply two integers X, Y. (0<=X,Y<10250)
The Input
The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.
The Output
For each input pair of lines the output line should consist one integer the product.
Sample Input
12122222222222222222222222222
Sample Output
144444444444444444444444444
本来是很水的一题的,速度写出来后没想到拖了这么久老AC不了,怎么修改都A不了。。。
本来的思路是这样的:倒序输入数组里面,然后再二重循环相乘,个位数累加给result的第i+j个数,其他位累加给i+j+1,虽然没有和网上大部分做法一样用到leap来作为进位数,但结果据我测试多次效果是一样的。
但不知道怎么回事,老是WA。。。
注意到乘数为0时的情况,于是我判断输入的string第一个字符是否为0,是就输出0并进行下一轮,还是WA。
后来我考虑到如果遇到012*01这种情况,就没有进行计算了,于是我吧判断放在后面,判断result是不是空的。。。还是WA了。。
我太失望了,我已经没办法了,难道非得用通用的那种方法吗。。。
先贴出WA代码吧。。。
#include<iostream>#include<string>using namespace std;int main(){string str1, str2;while (cin >> str1 >> str2){int a[250] = {0}, b[250] = {0}, res[300] = {0};for (int i = 0; i < (int)str1.size(); i++)a[i] = str1[(int)str1.size() - i - 1] - '0';for (int i = 0; i < (int)str2.size(); i++)b[i] = str2[(int)str2.size() - i - 1] - '0';for (int i = 0; i < (int)str1.size(); i++)for (int j = 0; j < (int)str2.size(); j++){int temp = a[i] * b[j] + res[i +j];res[i + j] = temp % 10;res[i + j + 1] += temp / 10;}int i;for (i = 299; i >= 1; i--)if (res[i])break;for (;i >= 0; i--)cout << res[i];cout << endl;}return 0;}
不知道到底是为什么。。。
改用进位方法了。。。
然后就AC了。。。
#include <stdio.h>#include <string.h>int main(){ int u, i, j, k, n, m, temp, leap; char str2[10000], str1[10000], res[10000]; while(gets(str1) != NULL) {gets(str2); memset(res, 0, sizeof(res)); n = strlen(str1); m = strlen(str2);for(i = 0; i < n; i++) str1[i] -= '0'; for(i = 0; i < m; i++) str2[i] -= '0'; for(k = n - 1; k >= 0; k--) { leap = 0; u = n - 1 - k; for(j = m - 1; j >= 0; j--) { temp = str2[j] * str1[k] + leap + res[u]; res[u] = temp % 10; u++; leap = temp / 10; } if(leap){ res[u] = leap;u++;} } for(i = u - 1; i > 0; i--) if(res[i]) break; for(; i >= 0; i--) printf("%d", res[i]); printf("\n"); } return 0;}
- 大数系列——高精度乘法
- 算法提高 高精度乘法 ————大数乘法
- 大数系列——大数乘法
- 高精度计算 大数乘法
- 大数_高精度乘法
- 高精度--大数乘法 luogu
- 高精度乘法,大数相乘
- 大数,高精度计算---大数乘法
- POJ-1001 求高精度幂-大数乘法系列
- 高精度——乘法
- 【高精度】-DLUTOJ-1176-大数乘法
- 6、大数,高精度计算---大数乘法
- POJ2389 —— 高精度乘法
- 大数运算(4)——大数乘法
- 大数加法 减法 乘法 除法 高精度四则运算
- URAL 1108. Heritage 高精度大数乘法
- 大数加法 减法 乘法 除法 高精度四则运算
- 高精度 大数加法 乘法 除法 幂 模板
- 运用Jconsole监控JVM
- android.graphics.Path
- 使用JCONSOLE监控远程LINUX运行的JAVA进程,总是在报连接失败的错误。
- 【★更新★】高性能 Windows Socket 服务端与客户端组件(HP-Socket v2.0.1 源代码及测试用例下载)
- 4_8
- 大数系列——高精度乘法
- Unity3d开发之实现在Unity3d层C#脚本和在Xcode层Objective-c语言的通信以及NGUI的屏幕适配问题
- spring基础开发框架搭建简记
- MFC一些问题的解决方法
- leetcode 122: Palindrome Partitioning
- DVR硬盘存储
- 实例化Spring Ioc文件的三种方式(硬编码)
- 图像编程学习笔记7——图像缩放
- 理解 Proc 文件系统