Project Euler task 52
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
-----------------------------------------------------------------
#include < iostream >
#include < vector >
#include < algorithm >
using namespace std;
bool hasSameDigits(int _iNum) {
int iTemp = _iNum;
vector vNums;
while (iTemp) {
vNums.push_back(iTemp % 10);
iTemp /= 10;
}
sort(vNums.begin(), vNums.end());
for (int i = 2; i <= 6; i++) {
iTemp = _iNum * i;
vector vNumsTemp;
while (iTemp) {
vNumsTemp.push_back(iTemp % 10);
iTemp /= 10;
}
sort(vNumsTemp.begin(), vNumsTemp.end());
if (vNums != vNumsTemp)
return false;
}
return true;
}
int main() {
int iTens = 1;
for (int i = 0; i < 6; i++) {
for (int j = iTens; j <= (int) ((((double) iTens * 10) / 6) + 1); j++)
if (hasSameDigits(j))
cout << j << endl;
iTens *= 10;
}
return 0;
}
Comments