POJ2389 —— 高精度乘法
直接上代码了:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<cmath>
- #include<queue>
- #include<vector>
- #include<map>
- #include<string>
- #include<set>
- #define LL long long
- #define MAX(a,b) (a>b?a:b)
- #define MIN(a,b) (a<b?a:b)
- using namespace std;
- char s[50];
- int a[50],b[50],c[100];
- int main()
- {
- int lena = 0, lenb = 0;
- memset(a,0,sizeof(a));
- memset(b,0,sizeof(b));
- memset(c,0,sizeof(c));
- gets(s);
- for(int i = strlen(s)-1; i>=0; i--)
- a[lena++] = s[i]-'0';
- gets(s);
- for(int i = strlen(s)-1; i>=0; i--)
- b[lenb++] = s[i] - '0';
- for(int i = 0; i<50; i++)
- {
- int last = 0;
- for(int j = 0; j<50; j++)
- {
- c[i+j] += a[i]*b[j];
- c[i+j] += last;
- last = c[i+j]/10;
- c[i+j] = c[i+j]%10;
- }
- }
- int t = 99;
- while(!c[t]) t--;
- while(t>=0) printf("%d",c[t--]);
- putchar('\n');
- return 0;
- }
顺便附上高精度加法的函数:
- //高精度加法
- void highplus(char a[],char b[],char c[])//调用前必须memset
- {//将a+b存到c中,直接输出,但c是字符型,要整形则简修改即可
- char ch;
- int la = strlen(a),lb = strlen(b);
- for(int i = 0;i<=(la-1)/2;i++)//将a和b位数倒转,以便逐位相加;同时-‘0’转成整形
- {
- ch = a[i]-'0';
- a[i] = a[la-1-i]-'0';
- a[la-1-i] = ch;
- }
- for(int i = 0;i<=(lb-1)/2;i++)
- {
- ch = b[i]-'0';
- b[i] = b[lb-1-i]-'0';
- b[lb-1-i] = ch;
- }
- for(int i = 0;i<100;i++)//这里的100根据调用函数而修改
- { //逐位计算,依照加法竖式
- c[i] += b[i]+a[i];
- c[i+1] += c[i]/10;
- c[i] %= 10;
- }
- int i = 99;//若精度不同记得修改
- while(!c[i]) i--;//跳过无用的0;
- for(int j = 0;j<=i/2;j++)//将c再换成字符型,并再倒叙,使其成为正常数
- { //记住要经过中间数,否则中间一个将没有转成字符型,所以用<=(len-1)/2
- ch = c[j]+'0';
- c[j] = c[i-j]+'0';
- c[i-j] = ch;
- }
- }
POJ2389 —— 高精度乘法的更多相关文章
- [vijos P1040] 高精度乘法
如果这次noip没考好,完全是因为从7月29日之后就没有再写过程序了.说起来,真是一个泪流满面的事实… 那这样一个弱智题练手恢复代码能力,竟然还花了我两个晚上(当然不是两整个晚上…) 第一天TLE了, ...
- 【PKU1001】Exponentiation(高精度乘法)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 145642 Accepted: 35529 ...
- hdu 1042 N!(高精度乘法 + 缩进)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 题目大意:求n!, n 的上限是10000. 解题思路:高精度乘法 , 因为数据量比较大, 所以 ...
- hdu 1042 N!(高精度乘法)
Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in ...
- Vijos 1040 高精度乘法
描述 高精度乘法 输入:两行,每行表示一个非负整数(不超过10000位) 输出:两数的乘积. 样例1 样例输入1 99 101 样例输出1 9999 题解 这道题和之前的Vijos 1010 清帝之惑 ...
- 【POJ 1001】Exponentiation (高精度乘法+快速幂)
BUPT2017 wintertraining(15) #6A 题意 求\(R^n\) ( 0.0 < R < 99.999 )(0 < n <= 25) 题解 将R用字符串读 ...
- [leetcode]43. Multiply Strings高精度乘法
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- H. GSS and Simple Math Problem 高精度乘法模板
链接:https://www.nowcoder.com/acm/contest/104/G来源:牛客网 题目描述 Given n positive integers , your task is to ...
- 高精度乘法--C++
高精度乘法--C++ 模仿竖式乘法,在第一步计算的时候将进位保留,第一步计算完再处理进位.(见代码注释) 若要处理正负情况,可在数据输入后加以判断,处理比较简单. 小数计算也可参照该方法,不过对齐方式 ...
随机推荐
- 分享Kali Linux 2017年第11周镜像文件
分享Kali Linux 2017年第11周镜像文件 Kali?Linux官方于3月12日发布2017年的第11周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KDE ...
- 济南day4
啥也不会,做了不对,对了没分.T1 50 + 30 + 0想了想,有思路,写了,半个小时写完,算错复杂度,复杂度最差(n*m),想成了(n+m)被卡没了50分,gg.....T2自己写了个单向并查集, ...
- Spring配置项之<aop:aspectj-autoproxy />
通过配置织入@Aspectj切面 虽然可以通过编程的方式织入切面,但是一般情况下,我们还是使用spring的配置自动完成创建代理织入切面的工作. 通过aop命名空间的<aop:aspectj-a ...
- Access自定义函数(人民币大写)
人民币大写函数:整数不超过13位. Public Function 人民币大写(A) As String Dim aa As String Dim bb As String Dim cc As Str ...
- AngularJS的简单表单验证
代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsCheckSimpleForm.rar 代码: <!DOCTYPE HTM ...
- 关于mysql engine(引擎)的疑问
http://bbs.chinaunix.net/thread-989698-1-1.html
- Android通过JNI实现与C语言的串口通讯操作蓝牙硬件模块
一直想写一份技术文档,但因为自感能力有限而无从下笔,近期做了个关于Android平台下实现与C语言的通讯来操作蓝牙模块的项目,中间碰到了很多问题,也在网上查了很多资料,在完毕主要功能后.也有一些人在网 ...
- 杭电 HDU 1279 验证角谷猜想
验证角谷猜想 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 函数指针使用演示样例(參考Linux-内核代码)
本文有xhz1234(徐洪志)编写,转载请注明出处. http://blog.csdn.net/xhz1234/article/details/36635083 作者:徐洪志 近期阅读Linux-内核 ...
- caffe2--ubuntu16.04--14.04--install
Install Welcome to Caffe2! Get started with deep learning today by following the step by step guide ...