【算法筆記】特殊的乘法
http://codeup.hustoj.com/problem.php?cid=100000575&pid=2
題目描述
寫個(gè)算法,對(duì)2個(gè)小于1000000000的輸入,求結(jié)果。特殊乘法舉例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5
輸入
?兩個(gè)小于1000000000的數(shù)
輸出
?輸入可能有多組數(shù)據(jù),對(duì)于每一組數(shù)據(jù),輸出Input中的兩個(gè)數(shù)按照題目要求的方法進(jìn)行運(yùn)算后得到的結(jié)果。
樣例輸入?
24 65?
42 66666?
3 67
樣例輸出?
66?
180
39
*********************************************************************************************************
/*思路:沒啥思路,不想用拆分的話,就直接寫成數(shù)組形式,然后參考上一篇A+B的char型數(shù)字轉(zhuǎn)換成int型。
或者選擇拆分整數(shù)的話,用取余,取整,存到整型數(shù)組里,然后再計(jì)算,可以參考如下答案(來自網(wǎng)絡(luò))(60條消息) 題目1083:特殊乘法[數(shù)位拆解]_ivolcano的博客-CSDN博客
*/
#include<stdio.h>
#include<string.h>
#define maxn 100010
int main(){
char A[maxn];
char B[maxn];
while(scanf("%s %s",A,B)!=EOF){
int a=strlen(A);
? ? int b=strlen(B);
int sum=0;
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
sum=sum+(A[i]-'0')*(B[j]-'0');
}
}
printf("%d\n",sum);
}
return 0;
}