2012-07-03 2 views
0

Project Euler 문제 4에 대한 해결책을 고수했습니다. 작동해야하는 다음 코드를 가지고 있으며, 조사하고 발견 한 문제에 대한 해결책을 반복합니다. (993 * 913) : 다음 999x999 아래쪽과까지의 숫자를 모두 관통Project Euler # 4에 대한 내 해결책으로 고민

// 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; 
} 

이 프로그램 반복은 뒤에서 앞으로 배치되는 결과 후반 두 문자열로 6 자리 수를 나눈다. 실행 중에 console<을 사용하는 것처럼 프로그램은 993 * 913을 시도하고 second_half 문자열과 first_half 문자열은 모두 906을 포함합니다. 프로그램이 수행해야한다고 생각한 것은 check (string first_half, string second_half) 함수를 수행하는 것입니다 이를 반복하고 두 문자열이 일치하는지 (다양한 소스에 따라 0을 반환해야 함) 결정하면 check() 내에서 if 문을 시작하고 true로 설정된 부울을 기본 문에서 프로그램을 종료하고 결과를 인쇄해야합니다 프로그램을 종료하기 전에. 그것은 그러나 이것을하지 않으며 이것은 나의 문제입니다. 모든 도움을 주셔서 감사합니다. 이 코드에서 볼 다른 문제를 차치

답변

0

, 당신은 main()의 외부 루프가 완료 후 achieved을 확인 때문에 종료 문제라고 생각합니다. 따라서 내부 루프 (iterate())는 operator_one이 100 미만으로 떨어질 때까지 계속 진행되며 종료 조건을 실제로 확인하기 전에 operator_two이 100 미만으로 떨어질 때까지 외부 루프가 계속됩니다.

+0

맞아요, 제 솔루션은 매우 엉망입니다. 문제를 해결하고 더 잘 이해하기 위해이 코드를 많이 추가했습니다. check() 함수에 종료 코드를 삽입하여 코드를 간단히 수정했습니다. 대답을 출력하고 일부 루프를 조정합니다. 고맙습니다. –

관련 문제