咋处理0啊

P1303 A*B Problem

littlewhite_ @ 2023-09-24 11:25:50

80

#include <bits/stdc++.h>
using namespace std;

int la, lb, lans, a[10100], b[10100], c[10100], ans[10100];

void Read(int *a, int &len)
{
    string cur;
    cin >> cur;
    len = cur.length(); 
    for (int i = 0; i < len; i++)
    {
        a[len - i - 1] = cur[i] - '0'; 
    }
    return;
}

int main()
{
    Read(a, la);
    Read(b, lb);
    lans = la + lb - 1;
    for (int i = 0; i < la; i++)
    {
        for (int j = 0; j < lb; j++)
        {
            ans[i + j] += a[i] * b[j];
        }
    } 
    for (int i = 0; i <= lans; i++)
    {
        if (ans[i] > 9)
        {
            ans[i + 1] = ans[i + 1] + ans[i] / 10;
            ans[i] = ans[i] % 10;
        }
    } 
    if (ans[lans])
    {
        lans++;
    }
    for (int i = lans - 1; i >= 0; i--)
    {
        cout << ans[i]; 
    }
    return 0;
} 

by zxjn @ 2023-09-24 11:35:33

高精用py


by LIUZHIYU1 @ 2023-10-02 18:54:50

打表


by Humour_Fz @ 2023-10-03 10:26:21

我的模版,自己看

#include<bits/stdc++.h>
using namespace std;
const int maxlen=/*自己填,就是数字的最大长度(结果也算)*/;
struct bignum{//大整数结构体 
    string s;
    int len,num[maxlen];//len是长度,num存数字的每一位(第0位是个位,第一位是十位……) 
};
bignum reverse(string x){//初始化大整数结构体 
    bignum xx;
    xx.len=x.size(),xx.s=x;
    memset(xx.num,0,sizeof(xx.num));
    for(int i=0;i<xx.len;i++) xx.num[i]=xx.s[xx.len-i-1]-'0';//倒序存放 
    return xx;
}
bool operator < (string x,string y){//对大整数的比较(下同) 
    if(x.size()<y.size()) return true;
    if(x.size()>y.size()) return false;
    if(x.size()==y.size()){
        for(int i=0;i<x.size();i++){//比较大整数的每一位(下同) 
            if(x[i]<y[i]) return true;
            if(x[i]>y[i]) return false;
        }
        return false;
    }
}
bool operator > (string x,string y){
    if(x.size()>y.size()) return true;
    if(x.size()<y.size()) return false;
    if(x.size()==y.size()){
        for(int i=0;i<x.size();i++){
            if(x[i]>y[i]) return true;
            if(x[i]<y[i]) return false;
        }
        return false;
    }
}
bool operator == (string x,string y){
    if(x.size()!=y.size()) return false;
    for(int i=0;i<x.size();i++) if(x[i]!=y[i]) return false;
    return true;
}
bool operator <=(string x,string y){
    return !(x>y);
}
bool operator >=(string x,string y){
    return !(x<y);
}
bool operator !=(string x,string y){
    return !(x==y);
}
string operator + (string x,string y){//计算两个大整数相加 
    bignum xx=reverse(x),yy=reverse(y),result;
    memset(result.num,0,sizeof(result.num));
    result.len=max(xx.len,yy.len);//计算结果的长度(下同) 
    for(int i=0;i<result.len;i++) result.num[i]+=xx.num[i]+yy.num[i],result.num[i+1]+=result.num[i]/10,result.num[i]%=10;//相加每一位并进位 
    if(result.num[result.len]!=0) result.len++;//去前缀0(下同) 
    for(int i=0;i<result.len;i++) result.s+=result.num[result.len-i-1]+'0';//倒序取出结果 
    return result.s;
}
string operator - (string x,string y){//计算两个大整数相减
    bignum xx=reverse(x),yy=reverse(y),result;
    memset(result.num,0,sizeof(result.num));
    if(x<y) swap(x,y),swap(xx,yy),result.s+="-";//处理被减数小于减数的情况 
    result.len=max(xx.len,yy.len);
    for(int i=0;i<result.len;i++){
        result.num[i]+=xx.num[i]-yy.num[i];
        if(result.num[i]<0) result.num[i]+=10,result.num[i+1]--;//相减并退位 
    }
    while(result.num[result.len-1]==0&&result.len>1) result.len--;
    for(int i=0;i<result.len;i++) result.s+=result.num[result.len-i-1]+'0';
    return result.s;
}
string operator * (string x,string y){//计算两个大整数相乘
    bignum xx=reverse(x),yy=reverse(y),result;
    memset(result.num,0,sizeof(result.num));
    result.len=xx.len+yy.len;
    for(int i=0;i<xx.len;i++) for(int j=0;j<yy.len;j++) result.num[i+j]+=xx.num[i]*yy.num[j];//相乘 
    for(int i=0;i<result.len;i++) result.num[i+1]+=result.num[i]/10,result.num[i]%=10;//进位 
    while(result.num[result.len-1]==0&&result.len>1) result.len--;
    for(int i=0;i<result.len;i++) result.s+=result.num[result.len-i-1]+'0';
    return result.s;
}
string operator * (string x,int y){//计算大整数乘整数 
    bignum xx=reverse(x),result;
    memset(result.num,0,sizeof(result.num));
    result.len=xx.len+to_string(y).size();
    for(int i=0;i<xx.len;i++) result.num[i]=xx.num[i]*y;//类似乘法竖式的原理 
    for(int i=0;i<result.len;i++) result.num[i+1]+=result.num[i]/10,result.num[i]%=10;//进位 
    while(result.num[result.len-1]==0&&result.len>1) result.len--;
    for(int i=0;i<result.len;i++) result.s+=result.num[result.len-i-1]+'0';
    return result.s;
}
string operator * (int y,string x){
    return x*y;
}
string operator / (string x,string y){//计算大整数除以大整数 
    bignum result;
    memset(result.num,0,sizeof(result.num));
    int l=0;
    while(y.size()<x.size()) y+="0",l++;//预先初始化y 
    for(int i=0;i<=l;i++){
        int k=0;
        while(x>=y) x=x-y,k++;//用减法模拟除法 
        y=y.substr(0,y.size()-1);//将y去掉一个末尾0 
        result.num[i]=k;//存结果 
    }
    for(int i=0;i<=l;i++) if(!(result.s.empty()&&result.num[i]==0)) result.s+=result.num[i]+'0';//防前缀0并存结果 
    if(result.s.empty()) result.s="0";
    return result.s;
}
string operator / (string x,int y){//计算大整数除以整数 
    int k=0;//k是余数
    bignum xx=reverse(x),result;
    memset(result.num,0,sizeof(result.num));
    result.len=xx.len;
    for(int i=xx.len-1;i>=0;i--) result.num[i]=(k*10+xx.num[i])/y,k=(k*10+xx.num[i])%y;//并且计算每一位的结果 
    while(result.num[result.len-1]==0&&result.len>1) result.len--;
    for(int i=0;i<result.len;i++) result.s+=result.num[result.len-i-1]+'0';
    return result.s;
}
string operator % (string x,string y){//计算大整数取模于大整数(与除法仅有返回的变量不同) 
    bignum result;
    memset(result.num,0,sizeof(result.num));
    int l=0;
    while(y.size()<x.size()) y+="0",l++;
    for(int i=0;i<=l;i++){
        while(x>y||x==y) x=x-y;
        y=y.substr(0,y.size()-1);
    }
    return x;
}
int operator % (string x,int y){//计算大整数取模于整数(与除法仅有返回的变量不同) 
    int k=0;
    bignum xx=reverse(x),result;
    memset(result.num,0,sizeof(result.num));
    result.len=xx.len;
    for(int i=xx.len-1;i>=0;i--) result.num[i]=(k*10+xx.num[i])/y,k=(k*10+xx.num[i])%y;
    return k;
}
string fast_pow(string x,int y){//快速幂 
    string sum="1";
    while(y){
        if(y%2) sum=sum*x;
        sum=sum*sum;
        y/=2;
    }
    return sum;
}
string A(int x,int y){//排列 
    string sum="1";
    for(int i=x;i>x-y;i--) sum=sum*i;
    return sum; 
}
string fact(int x){//阶乘 
    return A(x,x);//x!=A(x,x)
}
string C(int x,int y){//组合 
    y=min(x-y,y);//C(x,y)=c(x,x-y)
    return A(x,y)/fact(y);
}
int main(){

    return 0;
}

by Oscar20100301 @ 2023-10-10 17:14:08

自己看

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define max 5000
string s1,s2; 
int main(){
    cin>>s1>>s2;

    int l1=s1.length(),l2=s2.length();//s1,s2的长度 

    int a[max],b[max];//s1,s2整型数组 
    for(int i=0;i<l1;i++) a[i]=s1[l1-i-1]-'0';//倒序 
    for(int i=0;i<l2;i++) b[i]=s2[l2-i-1]-'0';

    int c[max];//结果 
    for(int i=0;i<l1;i++){
        for(int j=0;j<l2;j++){
            c[i+j]+=a[i]*b[j];
            c[i+j+1]+=c[i+j]/10;
            c[i+j]%=10;
        }
    }

    int l3=l1+l2;
    while(l3>1&&c[l3-1]==0) l3--;//去掉前导0 
    for(int i=l3-1;i>=0;i--) cout<<c[i];

    return 0;
}


|