题目1083:特殊乘法

题目1083:特殊乘法

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:5152

解决:3501

题目描述:

写个算法,对2个小于1000000000的输入,求结果。

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

输入:

 两个小于1000000000的数

输出:

 输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。

样例输入:
123 45
样例输出:
54

方法1比较复杂
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;

int main()
{
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        int buf1[20],buf2[20],size1=0,size2=0;
        while(a!=0)
        {
            buf1[size1++]=a%10;
            a/=10;
        }
        while(b!=0)
        {
            buf2[size2++]=b%10;
            b/=10;
        }
        int ans=0;
        for(int i=0; i<size1; i++)
            for(int j=0; j<size2; j++)
            {
                ans+=buf1[i]*buf2[j];
            }
            cout<<ans<<endl;
    }
    return 0;
}

方法2比较灵巧

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;

int main()
{
    char a[11],b[11];
    while(scanf("%s %s",a,b)!=EOF)
    {

        int ans=0;
        for(int i=0; a[i]!=0; i++)
            for(int j=0; b[j]!=0; j++)
            {
                ans+=(a[i]-'0')*(b[j]-'0');
            }
            cout<<ans<<endl;
    }
    return 0;
}

 

posted @ 2016-07-31 17:10  多思考&&多动手  阅读(218)  评论(0编辑  收藏  举报