In: Computer Science
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value.
First count from the first number to the second, but stop one short.
Then, count from that number back towards the first, again stopping one short.
Continue until you reach a single number.
Enter 3 and 9
solution:
3456789
987654
45678
8765
567
76
6
#include <iostream> using namespace std; int main() { int start, end; cin >> start >> end; if (start < end) { while (start < end) { for (int i = start; i < end; ++i) { cout << i << " "; } cout << endl; for (int i = end-1; i > start; --i) { cout << i << " "; } cout << endl; start++; end--; } } else { while (start > end) { for (int i = start; i > end; --i) { cout << i << " "; } cout << endl; for (int i = end+1; i < start; ++i) { cout << i << " "; } cout << endl; start--; end++; } } return 0; }