// Answer on Question#38176 - Programming - C++
#include <iostream>
using namespace std;
int main()
{
int number = 0;
int digits[3]; // read a three digit number from console
while (number < 100 || number > 999)
{
cout << "Enter a three digit number:\n";
cin >> number;
} // get 3 digits of the number
digits[0] = number % 10;
digits[1] = (number / 10) % 10;
digits[2] = number / 100; // generate all different permutations
cout << "Permutations:\n"; // all digits are the same
if (digits[0] == digits[1] && digits[1] == digits[2])
{
cout << number << endl;
}
else
{ // all digits are different
if (digits[0] != digits[1] && digits[1] != digits[2] && digits[2] != digits[0])
{
cout << digits[2] * 100 + digits[1] * 10 + digits[0] << endl;
cout << digits[2] * 100 + digits[0] * 10 + digits[1] << endl;
cout << digits[1] * 100 + digits[0] * 10 + digits[2] << endl;
cout << digits[1] * 100 + digits[2] * 10 + digits[0] << endl;
cout << digits[0] * 100 + digits[1] * 10 + digits[2] << endl;
cout << digits[0] * 100 + digits[2] * 10 + digits[1] << endl;
}
else
{ // 2 digits are equal
if (digits[0] == digits[1])
{
cout << digits[2] * 100 + digits[1] * 10 + digits[0] << endl;
cout << digits[1] * 100 + digits[0] * 10 + digits[2] << endl;
cout << digits[1] * 100 + digits[2] * 10 + digits[0] << endl;
}
else
{
if (digits[0] == digits[2])
{
cout << digits[2] * 100 + digits[1] * 10 + digits[0] << endl;
cout << digits[2] * 100 + digits[0] * 10 + digits[1] << endl;
cout << digits[1] * 100 + digits[0] * 10 + digits[2] << endl;
}
else
{ // digits[1] == digits[2]
cout << digits[2] * 100 + digits[1] * 10 + digits[0] << endl;
cout << digits[2] * 100 + digits[0] * 10 + digits[1] << endl;
cout << digits[0] * 100 + digits[1] * 10 + digits[2] << endl;
}
}
}
}
return 0;
}
No comments:
Post a Comment