问题 C: 特殊乘法

389 篇文章 1 订阅

问题 C: 特殊乘法
时间限制: 1 Sec 内存限制: 32 MB
献花: 155 解决: 127
[献花][花圈][TK题库]
题目描述
写个算法,对2个小于1000000000的输入,求结果。特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5
输入
两个小于1000000000的数
输出
输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。
样例输入
24 65
42 66666
3 67
样例输出
66
180
39

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

const int maxSize = 128;

int main()
{
#ifdef _DEBUG
    ifstream cin("data.txt");
#endif // _DEBUG
    char array[maxSize] = {'\0'};
    bool fristP = true;
    while (cin.getline(array, maxSize))
    {
        if (array[0] == '\0') { break; }
        long long Sum = 0;
        int Afrist = 0, Bfrist = 0,Aend; 
        while (array[Bfrist++] != ' '); Aend = Bfrist - 1;
        while (array[Bfrist] == ' ') { ++Bfrist; };//空格折腾得我蛋疼
        for (int i = Afrist; i < Aend; ++i)
        {
            for (int j = Bfrist; array[j] != '\0' && array[j] != ' '; ++j)
            {
                Sum += (array[i] - '0') * (array[j] - '0');
            }
        }
        if (!fristP) { cout << endl; }
        else { fristP = false; }
        cout << Sum;

    }

#ifdef _DEBUG
    cin.close();
    system("pause");
#endif // _DEBUG

    return 0;
}

代码二:(别人写的)

#include <iostream>
#include <string>

using namespace std;


int main()
{
    string a,b;
    while (cin>>a>>b)
    {
        int n1=(int)a.length();
        int n2=(int)b.length();
        long long sum=0;

        for(int i=0;i<n1;i++)
        {
            for(int j=0;j<n2;j++)
            {
                sum+=(a[i]-'0')*(b[j]-'0');
            }
        }
        cout<<sum<<endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值