坚持使用我对欧拉项目#4的解决方案
Stuck with my solution to #4 of 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(字符串 first_half,字符串 second_half) 函数,并确定两个字符串匹配(根据各种来源应该返回 0),然后应该在 check() 中启动 if 语句并将实现的布尔值设置为 true 在主语句中结束程序并在退出程序之前打印结果。但是,它不会这样做,这是我的问题。感谢您的任何帮助。
撇开我在这段代码中看到的其他一些问题不谈,我认为您的终止问题是因为您仅在 main()
中的外部循环完成后才检查achieved
。 因此,在实际检查终止条件之前,内循环(iterate()
)将继续operator_one
下降到100以下,然后外循环将继续,直到operator_two
下降到100以下。
相关文章:
- 运行同一解决方案的另一个项目的项目
- Dll在同一解决方案中从 c# 项目导入到 c++ 项目
- MS Visual Studio 解决方案结合了非托管C++项目和 C# 项目
- 在 Visual Studio 解决方案中设置所有项目的运行时库
- 如何在解决方案中生成多个项目之前执行流程
- C++不正确,不需要重新声明类成员变量 MFC 手工解决方案/项目 MS VS 2015
- 如何在不创建新配置的情况下对两个不同解决方案使用的一个项目使用不同的 #defines
- 如何使用 CMake 获得包含所有项目的 VS 解决方案和单个项目的解决方案
- 单元测试类无法在我要测试的同一解决方案中的另一个项目中找到类
- C++/VS 2012-多项目解决方案:一个项目中存在链接器错误,但另一个项目没有
- 项目解决方案结构
- 如何在C++中设置多项目解决方案
- 了解 VC++ 项目/解决方案资源管理器文件层次结构
- 在VS2015社区版(C++)中从项目/解决方案中创建可执行文件
- Visual C++IDE中的项目/解决方案设置
- 错误LNK2019:在多个项目解决方案中无法解析的外部符号
- Visual Studio c++多项目解决方案
- 在多项目解决方案中使用非托管DLL的vc++项目在构建时导致链接错误
- C++Visual Studio-多项目解决方案-“;静态lib”;vs“;包括目录”;
- Coderloop不接受欧拉项目解决方案