Мой код компилируется, но кажется, что он никогда не найдет ответ.Это странно, так как я посмотрел на почти идентичный код, который заканчивается за секунды.
Вот мой код:
#include <iostream>
#include <sstream>
int main()
{
for(int i = 999; i >=100; i--)
{
for(int j=999; j>=100;j--)
{
int num = (i*j);
std::string number;
std::string temp;
std::string reversed;
std::stringstream out;
out << num;
number = out.str();
temp = number;
std::reverse(temp.begin(),temp.end());
if( temp == number)
{
std::cout << number << std::endl;
}
}
}
std::cin.get();
return 0;
}
Теперь вот код, который я знаю, работает и работает невероятно быстро,Я не вижу, что мы делаем по-другому.
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
// Count down from largest to smallest, so first palindrome found is the largest
unsigned biggestProduct = 0;
for(unsigned n1=999; n1>=100; --n1) {
for(unsigned n2=999; n2>=100; --n2) {
unsigned thisProduct = n1 * n2;
if(thisProduct > biggestProduct) {
stringstream strmProduct;
string strProductReverse;
strmProduct << n1 * n2;
strProductReverse = strmProduct.str();
reverse(strProductReverse.begin(), strProductReverse.end());
if(strmProduct.str() == strProductReverse)
biggestProduct = thisProduct;
}
}
}
cout << biggestProduct << endl;
cin.get();
return 0;
}