-1

每当我将单个数字输入输入以下代码时,答案都是 4 位数字。

#include <iostream>
#include <stdio.h>

using namespace std;

int makeEqual(string &s1, string &s2)
{
    int len1 = s1.length();
    int len2 = s2.length();

    //cout<<"Lenght 1 :: "<<len1<<endl;
    //cout<<"Lenght 2 :: "<<len2<<endl;

    if(len1>len2)
    {
        for(int i=0; i<(len1-len2); i++)
            s2='0'+s2;  
        //cout<<"Return value is :: "<<len1<<endl;
        return len1;
    }
    else if(len2>len1)
    {
        for(int i=0; i<len2-len1; i++)
            s1='0'+s1;
        //cout<<"Return value is :: "<<len2<<endl;
        return len2;
    }
    else
        return len1;
}
int singleMultiply(string s1, string s2)
{
    return (s1[0]-'0')*(s2[0]-'0');
}
long int multiply(string a, string b)
{
    int n=makeEqual(a,b);
    if(n==0) return 0;
    if(n==1) return (singleMultiply(a,b));
    return 0;
}
int main()
{
    cout<<singleMultiply("9","9");
    return 0;
}

输出是 0020 而不是 20。谁能解释一下这背后的逻辑?

编辑:我包括了我写的所有代码。实际上是编程新手,我正在尝试以 10 为基础编写 Karatsuba 算法的代码。

4

2 回答 2

0

我怀疑你cout有一个 setw(4) << setfill('0')我们看不到的地方。

正如0020你所描述的那样。

我建议尝试:

cout << setw(0) << setfill(' ') << singleMultiply("9","9");

以这种方式开始寻找问题:

int singleMultiply(string s1, string s2)
{
    cout << "Input 1 is " << s1[0] << endl;
    cout << "Input 2 is " << s2[0] << endl;

    cout << "Oper 1 is " << s1[0]-'0' << endl;
    cout << "Oper 2 is " << s2[0]-'0' << endl;

    cout << "Answer is " << (s1[0]-'0')*(s2[0]-'0') << endl;
    printf("Answer (via printf) is %d\n", (s1[0]-'0')*(s2[0]-'0'));

    return (s1[0]-'0')*(s2[0]-'0');
}
于 2013-08-20T14:31:02.070 回答
0

您的函数实现仅限于单个数字,但您正在接受字符串(可能是多个数字。

如果您只想乘以个位数,请将您的签名更改为

int singleMultiply(char s1, char s2)
{
  return (s1-'0')*(s2-'0');
}

int main()
{
    cout << singleMultiply('5','4');
    return 0;
}

如果你想更通用:

int singleMultiply(string s1, string s2)
{
    // deprecated method, but still the easiest to understand
    //return atoi(s1.c_str()) * atoi(s2.c_str());
    // updated method
    long l1 = strtol(s1.c_str(), NULL, 10);
    long l2 = strtol(s2.c_str(), NULL, 10);
    return l1 * l2;
}

int main()
{
    cout << singleMultiply("50", "40");
    return 0;
}

编辑:在你的编辑之后,很明显你做的工作比你需要的要多得多。 atoi/strtol将为您进行整数转换(并且比您尝试做的更好)。

我有一些时间可以消磨,所以我认为这就是你想要做的(虽然,我不知道你为什么想要):

#include <algorithm>
#include <iterator>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

std::string karatsuba(std::string lhs, std::string rhs)
{
    if (lhs.length() != rhs.length())
    {
        if (lhs.length() < rhs.length())
        {
            int diff = rhs.length() - lhs.length();
            lhs.insert(0, diff, '0');
        }
        else
        {
            int diff = lhs.length() - rhs.length();
            rhs.insert(0, diff, '0');
        }
    }

    if (lhs.length() == 1 && rhs.length() == 1)
    {
        long l = std::strtol(lhs.c_str(), NULL, 10);
        long r = std::strtol(rhs.c_str(), NULL, 10);
        long ret = l * r;
        std::stringstream ss;
        ss << ret;
        return ss.str();
    }

    int m = lhs.length();
    int midpoint = m / 2;
    std::string lowlhs(lhs.begin() + midpoint, lhs.end());
    std::string lowrhs(rhs.begin() + midpoint, rhs.end());
    std::string highlhs(lhs.begin(), lhs.begin()  + midpoint);
    std::string highrhs(rhs.begin(), rhs.begin()  + midpoint);

    long addLhs = std::strtol(lowlhs.c_str(), NULL, 10) + std::strtol(highlhs.c_str(), NULL, 10);
    long addRhs = std::strtol(lowrhs.c_str(), NULL, 10) + std::strtol(highrhs.c_str(), NULL, 10);
    std::stringstream ssL;
    std::stringstream ssR;
    ssL << addLhs;
    ssR << addRhs;

    std::string sZ0 = karatsuba(lowlhs, lowrhs);
    std::string sZ1 = karatsuba(ssL.str(), ssR.str());
    std::string sZ2 = karatsuba(highlhs, highrhs);

    long z0 = std::strtol(sZ0.c_str(), NULL, 10);
    long z1 = std::strtol(sZ1.c_str(), NULL, 10);
    long z2 = std::strtol(sZ2.c_str(), NULL, 10);
    long highOrder = static_cast<long>(std::pow(10.0, static_cast<double>(m)));
    long lowOrder = static_cast<long>(std::pow(10.0, static_cast<double>(m / 2)));
    long result = (z2 * highOrder) + ((z1 - z2 - z0) * lowOrder) + z0;
    std::stringstream ss;
    ss << result;
    return ss.str();
}

int main()
{
    std::string lhs = "20";
    std::string rhs = "45";
    std::string result = karatsuba(lhs, rhs);

    std::cout << "Multiplied:  " << lhs << " x " << rhs << " = " << result << std::endl;
    return 0;
}
于 2013-08-20T14:17:13.470 回答