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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int size = 100;
// Prototypes:
void bulgarian_move(unsigned int stcks[], const int max_size);
//other declared fuctions
int main()
{
do
{
unsigned int stacks[size] = {0};
int n;
cout << "Enter a positive integer: ";
cin >> n;
srand((unsigned)time(0));
build_stacks(stacks, size, n);
show_stacks(stacks, size);
cout << "\nThere are " << count_stacks(stacks, size) << " stacks." << endl;
bubble_sort(stacks, size);
cout << "\nAfter sorting, the stacks are: " << endl;
show_stacks(stacks, size); //show sequence of stacks
cout << "\nBulgarian Move: " << endl;
bulgarian_move(stacks, size);
cout << endl;
cin.clear();
}
while(cin.get()!= 'A');
return 0;
}
void build_stacks(unsigned int stck[], const int size, int n)
{
int i = 0;
while(n>0)
{
stck[i] = rand()%n + 1;
n -= stck[i];
++i;
}
}
void show_stacks(unsigned int stck[], const int size)
{
int i = 0;
//cout << endl;
while(stck[i] != 0)
{
cout << stck[i] << " ";
++i;
if(i%80==0) cout << endl;
}
}
unsigned int count_stacks(unsigned int a[], const int size)
{
unsigned int sum = 0;
for(int i = 0; i < size; ++i)
if(a[i] != 0) ++sum;
return sum;
}
void bubble_sort(unsigned int a[], const int size)
{
for(int i = 1; i < size; ++i)
for(int j = 1; j <= size-i; ++j)
if(a[j-1]<a[j])
{
swp(a[j-1],a[j]);
//show_stacks(a, size); //debug
}
}
void swp(unsigned int & x, unsigned int & y)
{
unsigned int temp = x;
x = y;
y = temp;
}
void bulgarian_move(unsigned int stcks[], const int max_size)
{
int k = 0;
int i = 0; //This is where I want the second array
int a[i] = {0};
do
{
int i = 0;
int sum = 0;
while(stcks[i] != 0)
{
stcks[i] -= 1;
sum += 1;
++i;
}
stcks[sum] = 0;
while(stcks[i] >= 0)
{
if(stcks[i] == 0)
{
stcks[i] = sum;
break;
}
}
bubble_sort(stcks, max_size);
int j = 0;
while(stcks[j] != 0)
{
cout << stcks[j] << " ";
++j;
if(j%80==0) cout << endl;
}
cout << endl;
++k;
}
while(k != 10);
}
|