0

我坚持使用我对 Project Euler 问题 4 的解决方案,我有以下代码应该可以工作,并且它确实遍历了我研究和发现的问题的解决方案(993 * 913):

// Michael Clover
// Project Euler
// Problem 4

/* A palindromic number reads the same both ways. The largest palindrome made from the    product of two 2-digit numbers is 9009 = 91  99.

Find the largest palindrome made from the product of two 3-digit numbers. */

#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>

using namespace std;

bool achieved = false; // change to true when the palindrome is found
string string_conversion = "";
string first_half = ""; // first half of string
string second_half = ""; // second half of string
stringstream conversion; // use this to convert integers to strings

int check(string first_half, string second_half) {
    if (first_half.compare(second_half) == 0) {
        achieved = true;
        return 0;
    }
    else {
        return 0;
    }
    return 0;
}

int convert(int result) {
    char temp;
    conversion << result;
    conversion >> string_conversion;
    if (string_conversion.size() == 6) {
        temp = string_conversion.at(0);
        //cout << "temp = " << temp << endl;
        first_half+=(temp);
        temp = string_conversion.at(1);
        //cout << "temp = " << temp << endl;
        first_half+=(temp);
        temp = string_conversion.at(2);
        //cout << "temp = " << temp << endl;
        first_half+=(temp);
        temp = string_conversion.at(5);
        //cout << "temp = " << temp << endl;
        second_half+=(temp);
        temp = string_conversion.at(4);
        //cout << "temp = " << temp << endl;
        second_half+=(temp);
        temp = string_conversion.at(3);
        //cout << "temp = " << temp << endl;
        second_half+=(temp);
        //cout << first_half << second_half << endl;
        check(first_half, second_half);
    }
    //if (string_conversion.size() == 5) {
        //cout << "The size of the string is 5" << endl;
        //exit(1);
    //}
    string_conversion = "";
    cout << first_half << endl;
    cout << second_half << endl;
    first_half.clear();
    second_half.clear();
    conversion.clear();
    conversion.str("");
    //cout << "conversion: " << conversion << endl;
    return 0;
}

int iterate(int operator_one, int operator_two) { // takes two numbers and iterates     through them, each time it is iterated, the result is passed to the convert    function to convert to string
    int two = operator_two;
        for (int i = operator_one; i > 100; i--) {
            int result = i * two;
            cout << i << "x" << two << endl;
            convert(result);
        }
    return 0;
}

int main() { // Use the stringstream to convert the numerical values into strings     which you can then use to check if they are palindromes
    int operator_one = 999;
    int operator_two = 999;
    while (achieved == false) {
        for (int i = operator_two; i > 100; i--) {
            iterate(999, i);
        }
    }
    cout << "The largest palindrome made from the product of two 3-digit numbers is: "     << string_conversion << endl;
    return 0;
}

该程序向下遍历 999x999 的所有数字,然后将 6 位数字拆分为两个字符串,结果的后半部分从后向前排列。如控制台在运行时使用 cout << 所示,程序尝试 993*913 并且 second_half 字符串和 first_half 字符串都包含 906。我认为程序应该做的是执行 check(string first_half, string second_half) 函数在迭代此并确定两个字符串匹配(应根据各种来源返回 0)之后,这应该在 check() 中启动 if 语句并将实现的布尔值设置为 true 在主语句中结束程序并打印结果在退出程序之前。然而它并没有这样做,这是我的问题。感谢您的任何帮助。

4

1 回答 1

0

撇开我在这段代码中看到的其他一些问题不谈,我认为你的终止问题是因为你只在外循环完成achieved 后才main()检查。因此,在您实际检查终止条件之前,内循环 ( iterate()) 将继续直到operator_one低于 100,然后外循环将继续直到operator_two低于 100。

于 2012-07-03T01:41:56.313 回答