POJ3673Cow Multiplication(水)

来源:互联网 发布:mac 安装sass 编辑:程序博客网 时间:2024/06/15 20:07
Cow Multiplication
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13399 Accepted: 9371

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 andB. 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

Source

USACO 2008 February Bronze

题目大意:要求把两个1e9以内的数,每一位互相乘然后输出

解题思路:不断的除和模

#include<iostream>    #include<cstdio>  #include<stdio.h>  #include<cstring>    #include<cstdio>    #include<climits>    #include<cmath>   #include<vector>  #include <bitset>  #include<algorithm>    #include <queue>  #include<map>  using namespace std;int a[50], b[50];int i, j, k;long long int n, m, ans;int main(){cin >> n >> m;k = 10;ans = 0;memset(a, -1, sizeof(a));memset(b, -1, sizeof(b));while (n!=0){a[++ans] = n%k;n /= k;}k = 10;ans = 0;while (m!=0){b[++ans] = m%k;m /= k;}for (i=1;;i++){if (a[i] == -1){n = i - 1;break;}}for (i = 1;; i++){if (b[i] == -1){m = i - 1;break;}}ans = 0;for (i = 1; i <= n; i++){for (j = 1; j <= m; j++){ans += a[i] * b[j];}}cout << ans << endl;}


0 0