天天看點

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;
}
           

高精度除法待更...