add uebung5 programs
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
#include "cpp_headers/fcpp.hh"
|
||||
|
||||
bool deck_check(int deck[], int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
// compare deck with number at this position
|
||||
// because initial deck is [0,1,2,3,..., n-1]
|
||||
if (deck[i] != i) {
|
||||
// if a difference is found, return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// if no problem was found, deck is the original deck
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// query the amount of cards that should be shuffled
|
||||
int size = enter_int("How many cards do you want to be shuffled? ");
|
||||
// only shuffle even number of cards
|
||||
if (size <= 0, size % 2 != 0) {
|
||||
print("Invalid input, please enter an even number >= 2");
|
||||
return 0;
|
||||
}
|
||||
// initialize the deck of cards
|
||||
int deck[size];
|
||||
for (int i = 0; i<size; i++) {
|
||||
deck[i] = i;
|
||||
}
|
||||
|
||||
// initialize a working array
|
||||
int newdeck[size];
|
||||
|
||||
// perfect out shuffle
|
||||
for (int times = 1; ;times++) {
|
||||
// shuffle with perfect out
|
||||
for (int i = 0; i<size/2; i++) {
|
||||
newdeck[i*2] = deck[i];
|
||||
newdeck[i*2+1] = deck[i+size/2];
|
||||
}
|
||||
// check if newdeck is the original deck
|
||||
if (deck_check(newdeck, size)) {
|
||||
// print out the amount of iterations needed
|
||||
printf("We needed %d perfect out shuffle iterations to get back to the original deck\n", times);
|
||||
break;
|
||||
} else {
|
||||
// otherwise set deck to newdeck
|
||||
for (int i = 0; i <size; i++) {
|
||||
deck[i] = newdeck[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// reset the deck
|
||||
for (int i = 0; i<size; i++) {
|
||||
deck[i] = i;
|
||||
}
|
||||
|
||||
// perfect in shuffle
|
||||
for (int times = 1; ;times++) {
|
||||
// shuffle with perfect in
|
||||
for (int i = 0; i<size/2; i++) {
|
||||
newdeck[i*2] = deck[i+size/2];
|
||||
newdeck[i*2+1] = deck[i];
|
||||
}
|
||||
// check if newdeck is the original deck
|
||||
if (deck_check(newdeck, size)) {
|
||||
// print out the amount of iterations needed
|
||||
printf("We needed %d perfect in shuffle iterations to get back to the original deck\n", times);
|
||||
break;
|
||||
} else {
|
||||
// otherwise set deck to newdeck
|
||||
for (int i = 0; i <size; i++) {
|
||||
deck[i] = newdeck[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user