UVA 10106 Product (大數相乘)

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   for   2014   ar   

Product 

The Problem

The problem is to multiply two integers X, Y. (0<=X,Y<10250)

The Input

The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.

The Output

For each input pair of lines the output line should consist one integer the product.

Sample Input

12122222222222222222222222222

Sample Output

144444444444444444444444444


大數相乘,網上有很多模板,今下午練練手,自己寫一遍,剛開始忽略了前置0,導致0*12=00,0*123=000,後來改了輸出就ac了。

#include <iostream>#include <cstring>#define maxn 260using namespace std;int main(){    char str1[maxn],str2[maxn];    while(cin>>str1>>str2){int len1,len2;len1=strlen(str1);len2=strlen(str2);int s1[maxn],s2[maxn];memset(s1,0,sizeof(s1));memset(s2,0,sizeof(s2));int i;for(i=0;i<len1;i++)s1[i]=str1[i]-'0';for(i=0;i<len2;i++)s2[i]=str2[i]-'0';int sum[maxn+maxn];memset(sum,0,sizeof(sum));int j,k;for(i=len2-1;i>=0;i--){for(j=len1-1;j>=0;j--){sum[i+j+1]=sum[i+j+1]+s2[i]*s1[j];}}for(k=len1+len2-1;k>=0;k--)if(sum[k]>=10){int temp;temp=sum[k]%10;sum[k-1]=sum[k-1]+sum[k]/10;sum[k]=temp;}//if(sum[0]!=0)//cout<<sum[0];bool flag=0;for(i=0;i<len1+len2-1;i++){if(sum[i]!=0)flag=1;if(flag==1)cout<<sum[i];}cout<<sum[len1+len2-1];cout<<endl;}    return 0;}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.