【慢慢学算法】:特殊乘法

   题目描述:   

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

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

输入:

 两个小于1000000000的数

输出:

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

样例输入:
123 45
样例输出:
54
代码:
#include<stdio.h>
int main()
{
    long long x, y, temp1, temp2, sum;
    int index1, index2;
    char a[11],b[11];
    while(scanf("%lld%lld",&x,&y) != EOF)
    {
	temp1 = x;
	temp2 = y;
	index1 = 0;
	index2 = 0;
	sum = 0;
	while(temp1 != 0)
	{ a[index1++] = temp1%10 ; temp1 = temp1/10;}
	while(temp2 != 0)
	{ b[index2++] = temp2%10 ; temp2 = temp2/10;}
	for(int i = 0; i < index1; i++)
	    for(int j = 0; j < index2; j++)
		sum += (int)a[i]*(int)b[j];
	printf("%lld\n",sum);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/VortexPiggy/p/2514804.html