坚持使用我对欧拉项目#4的解决方案

Stuck with my solution to #4 of Project Euler

本文关键字:项目 解决方案 坚持      更新时间:2023-10-16

我坚持使用欧拉项目问题 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(字符串 first_half,字符串 second_half) 函数,并确定两个字符串匹配(根据各种来源应该返回 0),然后应该在 check() 中启动 if 语句并将实现的布尔值设置为 true 在主语句中结束程序并在退出程序之前打印结果。但是,它不会这样做,这是我的问题。感谢您的任何帮助。

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