Я работаю над проблемой для веб-сайта практики программирования, я уже решил это в рубине и легко возвратил правильные значения, проблема заключается в том, чтобы найти самое большое палиндромное число, которое может быть получено из умножение трехзначный чисел, мой код выглядит следующим образомвозвращает неправильные значения для простого проекта
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int h = 0; //this will hold the largest palindrome
for (int a = 101; a < 1000; a++) //will cycle through all three digit numbers
{
for (int b = 101; b < 1000; b++) //will cycle through all three digit numbers
{
if (a*b%10 != 0) //checks to make sure last digit is not zero
{
int c = a*b; //temporary int to hold value when it is read into an array
int d[8] = {0}; //array to hold individual place values
int e = 0; //length of number
int f = 0; //will be used to trace through array
bool g = true; //bool to decide whether or not a number is a palindrome
while (c > 0) //until all digits have been read
{
d[e] = c%10; //reads last digit
c/=10; //chops off last digit
e++; //grows by on for every exponent of ten
}
for (e; e >= f; f++)
if (d[f] != d[e-f]) //compares array symetrically
g = false; //if a difference is found the bool is set to false
if (g==true)
h = a*b; //if g remains true then a new value is saved to h.
}
}
}
cout << h;
return 0;
}
Я прокомментировал это, чтобы сделать его более удобным для чтения, с моей проверкой ошибок я определил с почти абсолютной уверенностью, что проблема заключается в этих строках
for (e; e >= f; f++)
if (d[f] != d[e-f]) //compares array symetrically
g = false; //if a difference is found the bool is set to false
каким-то образом мой палиндромный тест не работает, как следует, значение, возвращаемое этой программой, равно 0, должно быть 906609
спасибо, что зафиксировал мой палиндромный улов после того, как я изменил if (g == true) на if (g == true && h