【算法】大数乘法

POJ:1001

http://poj.org/problem?id=1001

===============我是分割线=================

  1 /*
  2 *Copyright: CheerM
  3 *Author: CheerM
  4 *Date: 2016-11-14
  5 *Description: 实现 底数B为6位宽浮点数 指数E为(0,25】的数,求值
  6 */
  7
  8 #include <iostream>
  9 #include <string>
 10 #include <vector>
 11 #include <iomanip>
 12
 13 using namespace std;
 14
 15 const int max_ = 10000;//保留四位数
 16
 17 /*
 18 *Function:       multiply
 19 *Description:    被乘数任意长vector<int>& multiplicand, 乘数为定长,不超过6位的int multiplier,相乘的值保存在vector<int>& multiplicand中
 20 *parameter:         vector<int>& multiplicand 表示任意长的被乘数
 21 *                 int multiplier 表示定长不超过6位数的乘数
 22 *Return:         void
 23 */
 24 void multiply(vector<int>& multiplicand, int multiplier) {
 25     int carry = 0;
 26     for (int i = 0; i < multiplicand.size(); i++) {
 27         int t1 = multiplicand[i] * multiplier + carry;
 28         multiplicand[i] = t1 % max_;
 29         carry = t1 / max_;
 30     }
 31     if (carry != 0) {
 32         while (carry) {
 33             multiplicand.push_back(carry % max_);
 34             carry /= max_;
 35         }
 36     }
 37 }
 38
 39 int main() {
 40     string base;//底数
 41     int exponentiation, point, mark;//指数,小数点后有效位数,‘.’标识符
 42     while (cin >> base >> exponentiation) {
 43         point = mark = 0;
 44
 45         //记录小数点后数字位数,忽略最右的连续0
 46         for (int i = base.size() - 1; i >= 0; i--) {
 47             if (base[i] == ‘0‘ && point == 0)
 48                 continue;
 49             else if (base[i] == ‘.‘) {
 50                 mark = 1;
 51                 break;
 52             }
 53             else
 54                 point++;
 55         }
 56         if (!mark)point = 0;
 57
 58         //把底数base从字符串转化为整型,e.g. 12.345 -> int:12345 point=3, 1.0100 -> int:101 point=2
 59         int tempBase = 0, tmp = point;//tempBase是底数的int形式
 60         mark = 0;
 61         for (int i = 0; i < base.size() && tmp >= 0; i++) {
 62             if (base[i] >= ‘0‘ && base[i] <= ‘9‘) {
 63                 tempBase = tempBase * 10 + base[i] - ‘0‘;
 64             }
 65             else if (base[i] == ‘.‘) {
 66                 mark = 1;
 67             }
 68
 69             if (mark == 1) {
 70                 tmp--;
 71             }
 72         }
 73
 74         //初始化被乘数为1,循环相乘
 75         vector<int> result;//vector来存储超长int型整数,因为乘数有可能是6位数,而int最大值可以10位,所以result的每一个单位用来存储4位结果,从低到高
 76         result.push_back(1);
 77         point *= exponentiation;
 78         while (exponentiation--) {
 79             multiply(result, tempBase);
 80         }
 81
 82         //把vector转为string
 83         string ss;
 84         for (int i = 0; i < result.size(); i++) {
 85             int tt = result[i];
 86             for (int j = 0; j < 4; j++) {
 87                 ss.push_back(tt % 10 + ‘0‘);
 88                 tt /= 10;
 89                 if (ss.size() == point) ss.push_back(‘.‘);
 90                 if (i == result.size() - 1 && tt == 0) break;
 91             }
 92         }
 93         while (ss.size() < point) {//补0
 94             ss.push_back(‘0‘);
 95             if (ss.size() == point) ss.push_back(‘.‘);
 96         }
 97
 98         //输出
 99         for (int i = ss.size() - 1; i >= 0; i--)
100             cout << ss[i];
101         cout << endl;
102     }
103
104     system("pause");
105     return 0;
106 }

时间: 2024-08-24 21:10:53

【算法】大数乘法的相关文章

ACM学习历程—51NOD1028 大数乘法V2(FFT)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这个长度的大数肯定不行. 大数可以表示点值表示法,然后卷积乘法就能用FFT加速运算了. 这道题是来存模板的. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath>

大数乘法的几种算法分析及比较(2014腾讯南京笔试题)

转自:http://blog.csdn.net/chhuach2005/article/details/21168179 1.题目 编写两个任意位数的大数相乘的程序,给出计算结果. 2.题目分析 该题相继被ACM.华为.腾讯等选作笔试.面试题,若无准备要写出这种程序,还是要花一定的时间的.故,觉得有必要深入研究一下.搜索了网上的大多数该类程序和算法,发现,大数乘法主要有模拟手工计算的普通大数乘法,分治算法和FFT算法.其中普通大数乘法占据了90%以上,其优点是空间复杂度低,实现简单,时间复杂度为

[POJ] #1002# Exponentiation : 大数乘法

一. 题目 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 156373   Accepted: 38086 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of

51 Nod 1028 大数乘法 V2【Java大数乱搞】

1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 100000,A,B >= 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemI

uva 10106 Product(高精度大数乘法)

昨天刚写了个大数加法,今天又来了个大数乘法,其实解法差不多,只不过换成了好多个大数的相加而 已,看别人的算法其实跟我的也差不多,都是这个姿势.wa了一次,竟然忘了考虑0的情况,以后交题之前,都要判 断一下边缘数据,大数据和小数据,要不就是白白被扣时间啊 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; char a[

最短的计算大数乘法的c程序

#include <stdio.h> char s[99],t[99]; int m,n; void r(int i,int c) { int j=0,k=i; while(k)c+=s[j++]*t[k---1]; if(i)r(i-1,c/10); printf("%d",c%10); } void main() { gets(s);gets(t); while(s[n])s[n++]-=48; while(t[m])t[m++]-=48; r(m+n-1,0); }

大数乘法

1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 1000,A,B >= 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 相关问题 大数加法 0 大数开平方 640 大数进制转换 320 大数除法 320 大数乘法 V2 80 代码: 1 #inclu

【大数乘法】

1 #include<cstdio> 2 #include<cstring> 3 const int Len = 100; 4 void Mul(char a[],char b[],char c[])//大数乘法 5 { 6 int i,j; 7 int alen = strlen(a),blen = strlen(b); 8 memset(c,0,Len); 9 for(i = 0; i < alen; i++) 10 for(j = 0; j < blen; j++

HDOJ-1042 N!(大数乘法)

http://acm.hdu.edu.cn/showproblem.php?pid=1042 题意清晰..简单明了开门见山的大数乘法.. 10000的阶乘有35000多位 数组有36000够了 # include <stdio.h> # include <string.h> # define MAX 36000 int BigNum[MAX], NowLen; void Multi(int number) { int Temp[MAX]={0}, Tlen = 0, t;//Tem

大数乘法 (poj2389)

模板 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; void cheng( char *a, char *b, char *sum ) { int temp[2500]; int lena,lenb,l; lena=strlen(a); lenb=strlen(b); int len = lena + lenb;