文章标题 POJ 3673 : Cow Multiplication (水)

来源:互联网 发布:linux elinks命令 编辑:程序博客网 时间:2024/04/30 22:18

Cow Multiplication

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
题意:重新定义*这个符号,A*B表示A中每个位上的数乘以B中每个数字,然后相加得到答案。
分析:可以直接输入两个数字,用字符串输入,然后枚举每个字符,然后转化成相应的数字相乘。
代码:

#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<vector>#include<math.h>#include<map>#include<queue> #include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;string a,b; int main (){    while (cin>>a>>b){        int len_a=a.length();        int len_b=b.length();        int ans=0;        for (int i=0;i<len_a;i++){            for (int j=0;j<len_b;j++){                ans+=((a[i]-'0')*(b[j]-'0'));            }        }        cout<<ans<<endl;    }       return 0;}
0 0
原创粉丝点击