天天看點

九度OJ-1083-特殊乘法

題目位址:點選打開連結 題目描述:

寫個算法,對2個小于1000000000的輸入,求結果。

特殊乘法舉例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5

輸入:
 兩個小于1000000000的數
輸出:
 輸入可能有多組資料,對于每一組資料,輸出Input中的兩個數按照題目要求的方法進行運算後得到的結果。
樣例輸入:
123 45      
樣例輸出:
54      
來源:
2010年清華大學計算機研究所學生機試真題
答疑:
解題遇到問題?分享解題心得?讨論本題請通路: http://t.jobdu.com/thread-7806-1-1.html
#include <iostream>
using namespace std;
 
int main(){
    int a,b;
    int anum[10],bnum[10];
    int acount,bcount;
    int temp,sum;
    while (cin>>a>>b){
        //initiate
        sum=0;
        //process
        for (acount=0,temp=a;temp>0;acount++){
            anum[acount]=temp%10;
            temp/=10;
        }
        for (bcount=0,temp=b;temp>0;bcount++){
            bnum[bcount]=temp%10;
            temp/=10;
        }
        //sum up 
        for (int i=0;i<acount;i++){
            for (int j=0;j<bcount;j++){
                sum+=anum[i]*bnum[j];
            }
        }
        //output
        cout<<sum<<endl; 
    }
    return true;
}