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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
#include "cpp_headers/fcpp.hh"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
print("Please enter 10 natural numbers, enter 0 to show the sorted array, -1 to quit");
|
||||||
|
// initialize the array with 0s
|
||||||
|
int numbers[10] = {0,0,0,0,0,0,0,0,0,0};
|
||||||
|
// ask 11 times for input
|
||||||
|
for (int i = 0; i < 11; i++) {
|
||||||
|
int input = enter_int("> ");
|
||||||
|
// quit on -1
|
||||||
|
if (input == -1) {
|
||||||
|
break;
|
||||||
|
// print the array on 0
|
||||||
|
} else if (input == 0) {
|
||||||
|
printf("Array = [");
|
||||||
|
for (int n = 0; n < 10; n++) {
|
||||||
|
// last number is printed with trailing bracket and newline
|
||||||
|
if (n == 9) {
|
||||||
|
printf("%d]\n", numbers[n]);
|
||||||
|
// print out a single number with trailing space
|
||||||
|
} else {
|
||||||
|
printf("%d ", numbers[n]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// if already full, quit
|
||||||
|
} else if (i >= 10) {
|
||||||
|
print("array already full, terminating");
|
||||||
|
break;
|
||||||
|
// quit on invalid input
|
||||||
|
} else if (input < -1) {
|
||||||
|
print("input not allowed, terminating");
|
||||||
|
break;
|
||||||
|
// otherwise insert new input
|
||||||
|
} else {
|
||||||
|
// set last number to new input
|
||||||
|
numbers[i] = input;
|
||||||
|
// sort numbers from 0 to i
|
||||||
|
for (int k = 0; k < i; k++) {
|
||||||
|
// whenever we see a wrong ordered number
|
||||||
|
if (numbers[k] > numbers[k+1]) {
|
||||||
|
int smaller = numbers[k+1];
|
||||||
|
int bigger = numbers[k];
|
||||||
|
// flip the numbers
|
||||||
|
numbers[k] = smaller;
|
||||||
|
numbers[k+1] = bigger;
|
||||||
|
// and step back to compare with previous numbers
|
||||||
|
if (k <= 1) {
|
||||||
|
// there is no number before the first one
|
||||||
|
k = 0;
|
||||||
|
} else {
|
||||||
|
// step back 2, because we are adding 1 each time
|
||||||
|
k = k-2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user