2017.11.15

特殊乘法

题目描述

写个算法,对2个小于1000000000的输入,求结果。 特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5

输入描述:

两个小于1000000000的数

输出描述:

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

输入

123 45

输出

54

 

#include<iostream>
#include<string>
#include<math.h>
#include<stack> //出入栈头文件
using namespace std;
int main()
{
long a=0; //输入a
long b=0; //输入b
int counta=0; //计数a
int countb=0; //计数b
int arraya[10]={0}; //存储分离后的a
int arrayb[10]={0}; //存储分离后的a
int sum=0; //和
while(cin>>a&&cin>>b)
{
long tempa=a;
long tempb=b;
while(tempa!=0) //数据a分离
{
arraya[counta++]=tempa%10;
tempa=tempa/10;
}
while(tempb!=0) //数据b分离
{
arrayb[countb++]=tempb%10;
tempb=tempb/10;
}
for(int i=counta-1;i>=0;i--) //运算规则
{
for(int j=countb-1;j>=0;j--)
{
sum+=arraya[i]*arrayb[j];
}
}
cout<<sum<<endl; //输出
sum=0; //清零
counta=0;
countb=0;
}
return 0;
}

 




posted @ 2017-11-15 22:54  P_langen  阅读(149)  评论(0编辑  收藏  举报