codevs 3740_奶牛式乘法_模拟

发布于 2019-05-13  990 次阅读


题目描述

做厌了乘法计算题的贝茜,自创了一种新的乘法运算法则。在这套法则里,A*B等于一个取自A、一个取自B的所有数字对的乘积的和。比方说,123*45等于1*4 + 1*5 + 2*4 + 2*5 + 3*4 + 3*5 = 54。对于2个给定的数A、B (1 <= A, B <= 长整型最大数),你的任务是,用新的乘法法则计算A*B的值。


思路

暴力枚举字符串乘起来就可以了,然后因为全部数都是拆为一位,所以乘起来不会太大,不需要高精度之类的
O(nm)

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s,st;
    cin>>s>>st;
    int ans=0;
    for (int i=0;i<=s.length()-1;i++)
        for (int j=0;j<=st.length()-1;j++)
            ans+=(s[i]-'0')*(st[j]-'0');
    cout<<ans;
}