-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0_equation.c
87 lines (75 loc) · 1.94 KB
/
0_equation.c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
// soliton 1:
void equation(int n) {
for (int a = 0; a < 10; a++)
for (int b = 0; b < 10; b++)
for (int c = 0; c < 10; c++)
if ((a * 10 + b) + (c * 10 + a) == n)
printf("A = %d, B = %d, C = %d\n", a, b, c);
}
// soliton 2:
#define TRUE(a, b, c, n) ((a * 10 + b) + (c * 10 + a) == n)
void equation(int n)
{
int a = 0;
int b, c;
while (a <= 9)
{
b = 0;
while (b <= 9)
{
c = 0;
while (c <= 9)
{
if (TRUE(a, b, c, n))
printf("A = %d, B = %d, C = %d\n", a, b, c);
c++;
}
b++;
}
a++;
}
}
/*
Assignment name : equation
Expected files : equation.c
Allowed functions: printf
-------------------------------------------------------------------------------
The goal of this exercise is to find all possible answers to the following
equation :
AB + CA = n
where A, B, and C are individual digits [0-9] and n is an integer.
Note that here AB is not the product of A and B. It is merely a two digit number
with A being the first digit (tens place) and B the second digit (ones place).
Implement a function that, given an integer n, prints on the standard
output all the possible values of A, B, C for which the equation is true.
Your function must be declared as follows:
void equation(int n);
If a solution could not be found, nothing is printed.
Examples:
For the value n = 42, the output would be :
$> ./equation 42
A = 0, B = 2, C = 4
A = 1, B = 1, C = 3
A = 2, B = 0, C = 2
A = 3, B = 9, C = 0
$>
For the value n = 111, the output would be :
$> ./equation 111
A = 2, B = 9, C = 8
A = 3, B = 8, C = 7
A = 4, B = 7, C = 6
A = 5, B = 6, C = 5
A = 6, B = 5, C = 4
A = 7, B = 4, C = 3
A = 8, B = 3, C = 2
A = 9, B = 2, C = 1
$>
For the value n = 0, the output would be :
$> ./equation 0 | cat -e
A = 0, B = 0, C = 0$
$>
Note:
- The displayed output will always be sorted in ascending order beginning with A, then B and then C.
(as shown in the examples above)
*/