0分求助

P1303 A*B Problem

xujunjieAC @ 2021-12-21 13:08:30

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

bool compare(string& s1, string& s2)
{
    if (s1.length() > s2.length())return 1;
    else if (s1.length() == s2.length())return -1;
    else return 0;
}
string add(string& s1, string& s2)
{
    int a = abs(int(s1.length() - s2.length()));
    //补0
    if (compare(s1, s2))
    {
        for (int i = 0; i < a; i++) {
            s2 = '0' + s2;
        }
    }
    else if (!compare(s1, s2))
    {
        for (int i = 0; i < a; i++) {
            s1 = '0' + s1;
        }
    }
    string s = "";
    int carr = 0;    //进位
    int temp = 0;
    for (int i = s1.length() - 1; i >= 0; i--)
    {
        temp = s1[i] - '0' + s2[i] - '0' + carr;
        carr = 0;
        if (temp >= 10)
        {
            carr = temp / 10;
            temp = temp % 10;
        }
        s = char(temp + '0') + s;
    }
    if (carr != 0)
    {
        s = char(carr + '0') + s;
    }

    return s;
}

string muti(string& s1, string& s2)
{
    string s = "0";
    if (s1 == "0" || s2 == "0")
    {
        return s;
    }
    for (int i = s2.length()-1; i >=0; i--)
    {
        string stemp = "";
        int temp;
        int carr = 0;
        for (int i1 = s1.length()-1; i1 >=0; i1--)
        {
            temp = (s1[i1] - '0') * (s2[i] - '0')+carr;
            carr = 0;
            if (temp >= 10)
            {
                carr = temp / 10;
                temp = temp % 10;
            }
            stemp = char(temp + '0') + stemp;
        }
        for  (int i2=s2.length()-i-1; i2>0; i2--)
        {
            stemp = stemp + '0';
        }

        if ( carr != 0)
        {
            stemp = char(carr + '0') + stemp;
        }

        s = add(stemp, s);
    }
    return s;
}
int main()
{
    string s1, s2, s;
    getline(cin, s1);
    getline(cin, s2);

    s = muti(s1, s2);
    cout << s;
    return 0;
}

by xujunjieAC @ 2021-12-21 13:09:06

下载了测试点数据也一样的但就是不过


by reginakxl @ 2021-12-21 13:13:46

c++14不能用getline


by reginakxl @ 2021-12-21 13:14:16

改成cin试试,我昨天也死在了一样的地方


by B1ade_ @ 2021-12-21 13:14:34

getline->cin

by ud2_ @ 2021-12-21 13:18:35

C++14 没有不能用 getline。按行输入都有这样的问题。

总之没空格的就不要按行读入,有空格的按行读入后 erase 掉末尾的多余字符。


by rxjdasiwzl @ 2021-12-21 13:26:32

@reginakxl C++14 可以用 std::getline


by EC75 @ 2021-12-27 11:29:24

@xujunjieAC 用Python

a=int(input())
b=int(input())
print(a*b)

完事,100code


|