-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgebraicabcd.cpp
62 lines (50 loc) · 1.6 KB
/
algebraicabcd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*this updated solution checks the uniqueness of digits (a,b,c,d,e) and then finds if abcd * e == dcba
Problem:
Find all 4 digit numbers 'abcd' with distinct digits such that abcd * e == dcba (reversed)
1<=e<10 and e should be a distinct digit from abcd
a,b,c,d,e are distinct digits
*/
#include<iostream>
using namespace std;
int rev(int num)
{
int digit,res=0;
while(num!=0)
{
digit=num%10;
res=(res*10)+digit;
num=num/10;
}
return res;
}
int main()
{
int abcd,e,product,reversed;
for(abcd=1001;abcd<=9999;abcd++)
{
int num=abcd; // abcd should be 4-digit number with distinct digits
bool visited[10]={false};// visited digits are set to true
while(num!=0)
{
if(visited[num%10])
{ // if a digit occurs more than once -> break
break;
}
visited[num%10]=true;
num=num/10;
if(num==0)
{ // if number is unique -> check for abcd *e == dcba
for(e=0;e<10;e++)
{
if(!visited[e]) // e should be distinct from digits in abcd
{
product=abcd*e;
reversed=rev(product);
if(reversed==abcd)
cout<<abcd<<" * "<<e<<" = "<<product<<endl;
}
}
}
}
}
}