POJ 3673

Cow Multiplication

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12181   Accepted: 8485

Description

Bessie is tired of multiplying pairs of numbers the usual way, so she invented her own style of multiplication. In her style, A*B is equal to the sum of all possible pairwise products between the digits of A and B. For example, the product 123*45 is equal to 1*4 + 1*5 + 2*4 + 2*5 + 3*4 + 3*5 = 54. Given two integers A and B (1 ≤ A, B ≤ 1,000,000,000), determine A*B in Bessie's style of multiplication.

Input

* Line 1: Two space-separated integers: A and B.

Output

* Line 1: A single line that is the A*B in Bessie's style of multiplication.

Sample Input

123 45

Sample Output

54

CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#define REP(i, s, n) for(int i = s; i <= n; i ++)
#define REP_(i, s, n) for(int i = n; i >= s; i --)
#define MAX_N 10

using namespace std;

char a[MAX_N], b[MAX_N];
int ans = 0;

int main(){
    scanf("%s%s", a + 1, b + 1);
    int la = strlen(a + 1), lb = strlen(b + 1);
    REP(i, 1, la) REP(j, 1, lb){
        ans += (int)(a[i] - '0') * (b[j] - '0');
    }
    printf("%d\n", ans);
    return 0;
}
            

 

 
posted @ 2015-05-21 11:47  ALXPCUN  阅读(133)  评论(0编辑  收藏  举报