A × B problem (高精度)

A × B problem

输入格式

数据的第一行是整数 T(1 \leq T \leq 20)T(1≤T≤20),代表测试数据的组数。接着有 TT 组数据,每组数据只有一行,包括两个正整数 AA 和 BB。但 AA 和 BB 非常大,Redraiment 能保证这些数用 long 来保存一定会溢出。但 AA 和 BB 的位数最大不会超过 100100 位。

输出格式

对应每组测试数据,你都要输出两行:

第一行为:Case #:, # 代表这是第几组测试数据。

第二行是一个等式:A * B = Sum, Sum 代表 A \times BA×B 的结果。

你要注意这个等式里包含了几个空格。要求每组数据之间都需要保留一个空行。

样例输入

2
1 2
123456789 987654321

样例输出

Case 1:
1 * 2 = 2

Case 2:
123456789 * 987654321 = 121932631112635269

两种解该题的方法:

1.高精度乘法:

#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#define mod 998244353
#define Max 0x3f3f3f3f
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
const int maxn=100005;
ll a[405],b[405],c[815];
void High_Pre_Multi(string s1,string s2){       //高精度乘法模板(可收藏)
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    a[0]=s1.length();
    b[0]=s2.length();
    int len=b[0]+a[0]+1;
    for(int i=1;i<=a[0];i++){   
        a[i]=s1[a[0]-i]-'0';    //从低位开始存储s1
    }
    for(int i=1;i<=b[0];i++){
        b[i]=s2[b[0]-i]-'0';    //从低位开始存储s1
    }
    for(int i=1;i<=a[0];i++){
        for(int j=1;j<=b[0];j++){
            c[i+j-1]+=a[i]*b[j];    //乘法运算
            c[i+j]+=c[i+j-1]/10;    //进位
            c[i+j-1]%=10;           //低位取余数
        }
    }
    while(len>1 && c[len]==0){      //去除高位的空0
        len--;
    }
    for(int i=len;i>=1;i--){        //逆序输出(因为存储时为低位开始)
        cout<<c[i];
    }
    cout<<endl;                 //非模板内容,根据题意补充
}
int main(){
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    int index=1;
    while(T--){
        string str1,str2;
        cin>>str1>>str2;
        cout<<"Case "<<index++<<":"<<endl;
        cout<<str1<<" * "<<str2<<" = ";
        High_Pre_Multi(str1,str2);      //高精度乘法开始
        cout<<endl;
    }
    return 0;
}

2.C++string转换为数值+数值转换为string标准函数:

#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#define mod 998244353
#define Max 0x3f3f3f3f
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
template<class Type>
const int maxn=100005;
template<class Type>
Type StringtoNum(const string & str){   //C++stirng转换为数值函数模板
    istringstream iss(str);
    Type num;
    iss>>num;
    return num;
}
template<typename T>string ToString(const T&t){ //C++数值转换为string函数模板
    ostringstream oss;                          //创建一个格式化输出流
    oss<<t;                                         //把值传递入流中
    return oss.str();               
}
int main(){
    ios::sync_with_stdio(false);
    string str1,str2;
    int T;
    cin>>T;
    int index=1;
    while(T--){
        string str1,str2;
        cin>>str1>>str2;
        cout<<"Case "<<index++<<":"<<endl;
        cout<<str1<<" * "<<str2<<" = ";
        ll num1=StringtoNum<ll>(str1);
        ll num2=StringtoNum<ll>(str2);
        ll num=num1*num2;
        cout<<ToString(num);
        cout<<endl<<endl;;
    }
    return 0;
}

高精度除法待更...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值