// Main.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "math.h"
#include "ostream.h"
// Get the minor number between "a" and "b".
#define min(a,b) (((a) < (b)) ? (a) : (b))
// Eliminate some special impossible number.
bool IsImpossibleNum(int iNumByPos0, int iNumByPos1, int iNumByPos2)
{
if (0 == iNumByPos0 || 1 == iNumByPos0 || 5 == iNumByPos0 || 6 == iNumByPos0 || 0 == iNumByPos1 || 0 == iNumByPos2)
{ // Because these number's square include themselves, e.g. "5 * 5 = 25"
return true;
}
// Get the "iNumByPos0"'s square's lowest position's number
int i = (iNumByPos0 * iNumByPos0) % 10;
if (i == iNumByPos0 || i == iNumByPos1 || i == iNumByPos2)
{ // Then it's impossible.
return true;
}
return false;
}
void NumCheck(int Num, int NumEnd)
{
bool bIsImpossible = false;
int NumByPos[9] = { 0 }, j = 0, k = 0, NumSquare = 0, divNum = 0;
for (; Num < NumEnd; Num++)
{
// Fill the number array by position.
j = 0;
NumByPos[j++] = Num / 100;
NumByPos[j++] = (Num % 100) / 10;
NumByPos[j++] = Num % 10;
bIsImpossible = IsImpossibleNum(NumByPos[j-1], NumByPos[j-2], NumByPos[j-3]);
if (!bIsImpossible)
{
NumSquare = Num * Num; // get it's square.
divNum = 100000; // the divider
// Fill the number array by position.
for (; (!bIsImpossible) && (j < 9); j++)
{
NumByPos[j] = NumSquare / divNum; // every position's number
if (0 == NumByPos[j])
{ // If it has "0", then it's impossible.
bIsImpossible = true;
break;
}
NumSquare %= divNum;
divNum /= 10;
}
// Check the number array by position.
for (j = 0; (!bIsImpossible) && (j < 9 - 1); j++)
{ // check "x != y"
for (k = j + 1; k < 9; k++)
{
if (NumByPos[j] == NumByPos[k])
{ // If it has two equal number, then it's impossible.
bIsImpossible = true;
break;
}
}
}
if (!bIsImpossible)
{ // It's the needed number.
cout << Num << "\'s square is " << Num * Num << endl;
}
}
}
}
int main(int argc, char* argv[])
{
int Num = (int)sqrt(100000L); // the minimum
int NumEnd = min((int)sqrt(1000000L), 999); // the maximum
// Check the needed number between "Num" and "NumEnd".
NumCheck(Num, NumEnd);
// Remind the user to quit.
cout << "Press anykey to quit." << flush;
// Pause the program
getchar();
return 0;
}