九度 题目1083:特殊乘法

来源:互联网 发布:thunder for mac 编辑:程序博客网 时间:2024/04/30 16:58

题目来源:http://ac.jobdu.com/problem.php?pid=1083

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:2589

解决:1794

题目描述:

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

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

输入:

 两个小于1000000000的数

输出:

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

样例输入:
123 45
样例输出:
54
来源:
2010年清华大学计算机研究生机试真题
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 10;int Multi_Res(char* str1, char *str2){    int iLen1 = strlen(str1);    int iLen2 = strlen(str2);    int i, j, iNum, a, b;    if((iLen1 == 1 && str1[0] == '0') || (iLen2 == 1 && str2[0] == '0'))        return 0;    iNum = 0;    for(i = 0; i < iLen1; ++i)    {        a = str1[i] - '0';        for(j = 0; j < iLen2; ++j)        {            b = str2[j] - '0';            iNum += a*b;        }    }    return iNum;}int main(){    char str1[MAXN], str2[MAXN];    int iNum;    while(~scanf("%s %s", str1, str2))    {        iNum = Multi_Res(str1, str2);        printf("%d\n", iNum);    }    return 0;}


0 0
原创粉丝点击