时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:6858
解决:4618
-
题目描述:
-
写个算法,对2个小于1000000000的输入,求结果。
特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5
-
输入:
-
两个小于1000000000的数
-
输出:
-
输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。
-
样例输入:
-
123 45
-
样例输出:
-
54
-
题目地址:http://ac.jobdu.com/problem.php?pid=1083
-
问题分析:先获得整数每一位,然后进行其乘法。关键是对整数进行拆分的操作,利用求余运算可以轻松的实现。
-
代码实现:
-
#include <iostream>
#include<stdio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int A,B;
int Mutilple(int a,int b){
int ans=0;
while(a !=0){
//对a进行按位拆分
int t1=a%10;
a/=10;
int tmp_b=b;
while(tmp_b !=0 ){
int t2=tmp_b%10;
tmp_b/=10;
ans+=t1*t2; //a的一位和b的 所有位累乘相加
}
}
return ans;
}
int main(int argc, char *argv[]) {
while(cin>>A>>B){
int ans=Mutilple(A,B);
cout<<ans<<endl;
}
return 0;
}