A+B and A*B problem 大数相加 相乘 模拟
A+B and A*B problem 大数相加 相乘 模拟
题意
给你两个数a和b,这两个数很大,然后输出这两个数相加的和,相乘的积。
解题思路
模拟,但是还是搜了搜代码实现,发现这个大佬写的是真的简洁高效。
下面第一个代码转自博客(修改了一些) 》》》https://blog.csdn.net/hacker00011000/article/details/51298294
第二个代码是CZH同学的代码,代码更是简单易懂!
别的一些思路
大数相加相乘 https://blog.csdn.net/weixin_41162823/article/details/80044079
高精度快速幂 https://www.luogu.org/problemnew/solution/P1045
代码实现
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
//C++大数相加
string BigNumAdd(const string& strNum1, const string& strNum2)
{
string strSum;
int len1 = strNum1.size()-1;
int len2 = strNum2.size()-1;
int bit = 0; //保存进位
//从结尾开始每位相加
while (len1>=0 && len2>=0)
{
//求每位的和(要把进位也加上)
int tmpSum = strNum1[len1]-'0' + strNum2[len2]-'0' + bit;
//保存进结果
strSum += tmpSum % 10 + '0';
//求进位
bit = tmpSum / 10;
--len1;
--len2;
}
//两个整数长度不相等(肯定有一个已经加完了,不需要再额外加if来判断,因为while就可以判断)
while (len1 >= 0)
{
//和上个while循环一样
int tmpSum = strNum1[len1]-'0' + bit;
strSum += tmpSum % 10 + '0';
bit = tmpSum / 10;
--len1;
}
while (len2 >= 0)
{
//和上个while循环一样
int tmpSum = strNum2[len2]-'0' + bit;
strSum += tmpSum % 10 + '0';
bit = tmpSum / 10;
--len2;
}
//最高位有进位
if (bit != 0)
strSum += bit + '0';
//反转
reverse(strSum.begin(), strSum.end());
return strSum;
}
//C++大数相乘
string BigNumMultiply(const string& strNum1, const string& strNum2)
{
string strMultiply;
//两数相乘最大有m+n位
int bit = 0;
int len1 = strNum1.size()-1;
int len2 = strNum2.size()-1;
//计算每一位
for (int i=0; i<len1+len2+2; ++i)
{
//计算结果的第i位(权值肯定为i,第1位也就是个位权值为0(pow(10, 0)))
//等于乘数的第(i~0)位分别与被乘数的第(0~i)位相乘,因为这样每位相乘之后权值仍为i
//然后相加再加上前一位的进位,就是结果的第i位
//然后%10得出第i位,/10得到进位
int tmp = 0;
for (int j=i; j>=0; --j)
{
//如果下标超出字符串的范围 j为num1的下标, i-j为num2的下标,然后两数相乘
if (j>len1 || (i-j)>len2)
continue;
//还要注意字符串数字的最高位在字符串的最低位所以得用len减去
tmp += (strNum1[len1-j]-'0') * (strNum2[len2-(i-j)]-'0');
}
//加上进位
tmp += bit;
//为了防止最后一位是0,但是却加上了
if (tmp == 0 && i == len1+len2+1)
break;
//求余得到结果的第i位
strMultiply += tmp % 10 + '0';
//计算新的进位
bit = tmp / 10;
}
//判断结果的最后一个字符如果是0的话说明可以删去
//if (strMultiply[strMultiply.size()-1] == '0')
// strMultiply[strMultiply.size()-1] = '\0';
//反转
reverse(strMultiply.begin(), strMultiply.end());
return strMultiply;
}
int main()
{
string str1;
string str2;
cin >> str1 >> str2;
//相加和相乘
cout << BigNumAdd(str1, str2) << endl;
cout << BigNumMultiply(str1, str2) << endl;
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 10010;
int a[maxn],b[maxn];
int ans[maxn];
int ans2[maxn];
int main()
{
string s1,s2;
cin>>s1>>s2;
int len1 = s1.length();
int len2 = s2.length();
int lenans = 0, lenans2 = 0;
int n = 0;
for(int i=0;i<len1;i++)
a[i] = s1[i]-'0';
reverse(a, a+len1);
for(int i=0;i<len2;i++)
{
b[i] = s2[i]-'0';
n = n*10 + s2[i]-'0';
}
reverse(b, b+len2);
for(int i=0,up=0;i<maxn;i++)
{
ans[i] = a[i]+b[i]+up;
up = ans[i]/10;
ans[i] %= 10;
}
for(int i=maxn-1;i!=-1;i--)
{
if(ans[i])
{
lenans = i;
break;
}
}
for(int i=lenans;i!=-1;i--)
cout<<ans[i];
cout<<endl;
for(int i=0;i<maxn;i++)
{
ans2[i] = a[i]*n+ans2[i];
ans2[i+1] = ans2[i]/10;
ans2[i] %= 10;
}
for(int i=maxn-1;i!=-1;i--)
{
if(ans2[i])
{
lenans2 = i;
break;
}
}
for(int i=lenans2;i!=-1;i--)
cout<<ans2[i];
cout<<endl;
return 0;
}
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100;
char a[maxn], b[maxn];
int len, len1, len2;
int anum[maxn], bnum[maxn], sum[maxn];
int main()
{
cin>>a>>b;
len1=strlen(a);
len2=strlen(b);
len=max(len1, len2);
for(int i=len1-1, j=0; i>=0; i--, j++)
anum[j]=a[i]-'0';
for(int i=len2-1, j=0; i>=0; i--, j++)
bnum[j]=b[i]-'0';
for(int i=0; i<=len; i++)
{
sum[i]+=anum[i]+bnum[i];
if(sum[i]>9)
{
sum[i+1]+=sum[i]/10;
sum[i]%=10;
}
}
int begin=0;
for(int i=len; i>=0; i--)
{
if(sum[i]!=0)
{
begin=i;
break;
}
}
for(int i=begin; i>=0; i--)
cout<<sum[i];
cout<<endl;
memset(sum, 0, sizeof(sum));
for(int i=0; i<len1; i++)
{
for(int j=0; j<len2; j++)
sum[i+j]+=anum[i]*bnum[j];
}
for(int i=0; i<=len1+len2; i++)
{
if(sum[i]>9)
{
sum[i+1]+=sum[i]/10;
sum[i]%=10;
}
}
begin=0;
for(int i=len1+len2; i>=0; i--)
if(sum[i]!=0)
{
begin=i;
break;
}
for(int i=begin; i>=0; i--)
cout<<sum[i];
cout<<endl;
return 0;
}
A+B and A*B problem 大数相加 相乘 模拟的更多相关文章
- HDU-1002.大数相加(字符串模拟)
本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值. 本题思路:字符串模拟就能过,会Java的大佬应该不会点进来...... 参考代码: #include <cstdio&g ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- hdu acm-1047 Integer Inquiry(大数相加)
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu1002大数相加
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1250 Hat's Fibonacci(大数相加)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 1297 Children’s Queue (递推、大数相加)
Children’s Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 用字符串模拟两个大数相加——java实现
问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...
- 随机数组&大数相加
随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中 一, 设计思路: 先生成随机数数组,再将数组保存在一个字符串中,然后将数组各数字加和, ...
- java-两个大数相加
题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a=&qu ...
随机推荐
- jvm——内存模型
这是我理解的jvm内存模型,这一整块可以简单理解为虚拟内存空间: jvm代码.jvm数据:是运行jvm所用到的代码和数据,并不是我们自己编程得到的代码和数据 direct memory:主要是NIO在 ...
- 【leetcode】1224. Maximum Equal Frequency
题目如下: Given an array nums of positive integers, return the longest possible length of an array prefi ...
- 【知识】location.search获取中文时候会被编码成一串字符
[转码] 例如:case.html?id='这个是页面的标题' 当想要使用location.search获取?id='这个是页面的标题'的时候,包含的中文会被编码成一串字符串. 所以我们需要进行解码, ...
- 中南林业大学校赛 I 背包问题 ( 折半枚举 || 01背包递归写法 )
题目链接 题意 : 中文题 分析 : 价值和重量都太过于大,所以采用折半枚举的方法,详细可以看挑战的超大背包问题 由于 n <= 30 那么可以不必直接记录状态来优化,面对每个用例 直接采用递 ...
- UI编辑
UI编辑 基本部件介绍 (1)Layout(布局) (2)Space(空间) (3)Button (4)ItemView (5)ItemWidget Widget继承自View,即ListWidget ...
- Devexpress MVC DateEdit 设置默认的Time
当用户没有选择日期的时候, 默认显示当前的时间给TimeEdit. 只有当用户选了日期后, 才会把时间带进去. 效果图: 实现 C# Helper Code public static Action& ...
- DFS-全排列
void dfs(int x) { if(x>n) { ;i<=n;++i) cout<<a[i]; cout<<endl; return; } ;i<=n; ...
- Android图片缩放 指定尺寸
//使用Bitmap加Matrix来缩放 public static Drawable resizeImage(Bitmap bitmap, int w, int h) { ...
- [BZOJ1697][USACO2007 FEB]Cow Sorting牛排序:贪心+置换
分析 一个月前做的一道题补一下题解,就简单写一写吧. 单独考虑每一个循环节,如果只进行内部的调整,最优方案显然是把最小的绕这个循环交换一圈. 但是借助全局最小值可能使答案更优,两种情况取个\(\max ...
- python中类的设计问题(一些高级问题探讨,函数重载,伪私有,工厂模式,类方法等)
从这里再次体现了python语言强大的灵活性.某些在高级语言中看似不严谨需要尽量避免的地方在python中都是允许的. 比如: (1),异常可以用来处理错误 (2),方法,类也都可以视为对象. (3) ...