我坚持使用我对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&lt;&lt;在运行时,程序尝试993 * 913,second_half字符串和first_half字符串都包含906.我认为程序应该做的是在迭代后执行检查(字符串first_half,字符串second_half)函数并确定两者都是字符串匹配(应根据各种来源返回0)然后应该在check()中启动if语句并将boolean achie设置为true,结束主语句中的程序并在退出程序之前打印结果。但它不会这样做,这是我的问题。感谢您的帮助。
答案 0 :(得分:0)
除了我在此代码中看到的其他一些问题之外,我认为您的终止问题是因为您只在 achieved
中的外部循环完成后检查main()
。因此内循环(iterate()
)将一直持续到operator_one
低于100,然后外循环将持续到operator_two
低于100,然后才能实际检查终止条件。