Given any four positive integers a, b, c, d, you can find four more integers as:
| a - b |, | b - c |, | c - d |, | d - a |
Find the absolute value of the difference of pairs to create a new group of a, b, c, d. If you repeat this, the numbers will eventually converge to one common value.
For example, given 1, 3, 5, 9:
1 3 5 9
2 2 4 8 (1 step)
0 2 4 6 (2 steps)
2 2 2 6 (3 steps)
0 0 4 4 (4 steps)
0 4 0 4 (5 steps)
4 4 4 4 (6 steps)
This set converges to the value 4 in 6 steps.
Write a program that allows the user to enter a group of four numbers, then compute and display the converged value and number of steps.
Enter four numbers: 1 3 5 9
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
|
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int a, b, c, d, x = 0;
bool flag = true;
flag == (a == b && a == c && a == d);
cout << "Enter four numbers: ";
cin >> a >> b >> c >> d;
cout << endl;
while(flag ){
a -= b;
b -= c;
c -= d;
d -= a;
x++;
if(a < 0)
a *= -1;
if(b < 0)
b *= -1;
if(c < 0)
c *= -1;
if(d < 0)
d *= -1;
}
cout << "This group converges to value " << a << " after " << x << " steps.";
}
|
i also tried this
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
|
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int a, b, c, d, x = 0;
cout << "Enter four numbers: ";
cin >> a >> b >> c >> d;
cout << endl;
while(a != b && a != c && a != d ){
a -= b;
b -= c;
c -= d;
d -= a;
x++;
if(a < 0)
a *= -1;
if(b < 0)
b *= -1;
if(c < 0)
c *= -1;
if(d < 0)
d *= -1;
}
cout << "This group converges to value " << a << " after " << x << " steps.";
}
|