2 solutions

  • 1
    @ 2024-1-20 11:50:07
    #include<bits/stdc++.h>
    using namespace std;
    int l1,l2;
    int chengshu1[1005],chengshu2[1005],ji[1005]={0};//三个数组,分别存储两个乘数与积,要开到1005,本人亲测205会寄 
    void input(string s,int a[]){//输入函数,具体功能不多赘述 
    	int len=s.length();
    	for(int i=0;i<len;i++){
    		a[len-i-1]=s[i]-'0';
    	}
    }
    void APAmul(int a[],int b[],int c[]){//高精度乘法函数 
    	for(int i=0;i<1004;++i){//这里i<1004是因为数组长度是1005
    		for(int j=0;j<=i;++j){
    			c[i]+=a[j]*b[i-j];//分别叠加 
    		}
    		if(c[i]>=10){//进位 
    			c[i+1]+=c[i]/10;
    			c[i]%=10; 
    		}
    	} 
    }
    void output(int d[]){//输出函数 
    	int i;
    	for(i=1004;i>=1;--i){//此处同理 
    		if(d[i]!=0)break;//寻找前导0结束的位置 
    	}
    	for(;i>=0;--i){
    		cout<<d[i];
    	}
    	cout<<"\n";
    }
    int main(){
    	string n1,n2;
    	cin>>n1>>n2;
    	input(n1,chengshu1);//分别存储两个乘数 
    	input(n2,chengshu2);
    	APAmul(chengshu1,chengshu2,ji);//进行高精度乘法 
    	output(ji);
    	return 0;
    } 
    //与A1174同理
    
    • 0
      @ 2024-1-26 9:45:09
      #include<bits/stdc++.h>
      #include <string>
      using namespace std;
      
      int fn[1005],sn[1005],ans[1010];
      
      void read(int a[])
      {
      	char s[110];
      	cin >> s;
      	int lenth = strlen(s); 
      	for(int i = 0; i < lenth; i++)
      		a[lenth - i - 1] =s[i]-'0';
      } 
      
      void print(int b[])
      {
      	int i;
      	for(i = 1004; i >= 1; i--)
      		if(b[i] != 0) break;
      	for(; i >= 0; i--)
      	    cout << b[i];
      	cout << endl;
      } 
      
      void mul (int a[], int b[], int c[])
      {
      	for(int i = 0; i < 1004; ++i)
      	{
      		for(int j = 0; j <= i; ++j)
      			c[i]+=a[j]*b[i-j];
      		if(c[i] >= 10)
      		{
      			c[i+1] += c[i] / 10;
      			c[i] %= 10;
      		}
      	}
      }
      
      int main()
      {
      	read(fn);
      	read(sn);
      	mul(fn, sn, ans);
      	print(ans);
      	return 0;
      }
      
      • 1

      Information

      ID
      792
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      4
      Tags
      # Submissions
      75
      Accepted
      37
      Uploaded By