-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy path8 coins algorithm
102 lines (92 loc) · 1.92 KB
/
8 coins algorithm
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
using namespace std;
void eightcoin(int arr[]);
void compare(int a, int b, int real, int index1, int index2);
void print(int fake, int real, int i);
int main()//Get the weight of coins
{
int arr[8];
//Enter the weight of coins
cout << "Please enter eight coins:" << endl;
for (int i = 0; i < 8; i++)
{
cin >> arr[i];
}
eightcoin(arr);
return 0;
}
void eightcoin(int arr[])
{
int abc = arr[0] + arr[1] + arr[2];
int def = arr[3] + arr[4] + arr[5];
int a = arr[0];
int b = arr[1];
int c = arr[2];
int d = arr[3];
int e = arr[4];
int f = arr[5];
int g = arr[6];
int h = arr[7];
if (abc > def)
{
if ((a + e) > (d + b))
{
compare(a, d, g, 0, 3);
}
else if ((a + e) == (d + b))
{
compare(c, f, g, 2, 5);
}
else
{
compare(b, e, g, 1, 4);
}
}
else if (abc == def)
{
if (g == a)
{
print(h, g, 7);
}
else
{
print(g, h, 6);
}
}
else
{
if ((a + e) > (d + b))
{
compare(b, e, g, 1, 4);
}
else if ((a + e) == (d + b))
{
compare(c, f, g, 2, 5);
}
else
{
compare(a, d, g, 0, 3);
}
}
}
void compare(int a, int b, int real, int index1, int index2)//compare coins and find real coins and fake coin.
{
if (a == real)
{
print(b, real, index2);
}
else
{
print(a, real, index1);
}
}
void print(int fake, int real, int i)//print the result
{
if (fake > real)
{
cout << "The fake money is located at " << (i + 1)<<" and it is heavier than real one" ;
}
else {
cout << "The fake money is located at" << (i + 1)<<" and it is lighter than real one" ;
}
}