#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int main (int argc, const char * argv[]) {
/*
4 digits small to large no repetition 1-9
A indicates the number of existing and positional pairs
B means it exists, but the position is wrong
1 2 3 4
1 4 6 8
1a1b
*/
int array[4] = {};
BOOL Isexist = false;
Set the seed of a random number
Srand ((unsigned int) time (NULL));
for (int i = 0; i < 4; i++) {
int temp = rand ()% 9 + 1;//1-9
Determine if there is
for (int j = 0; J < i; J + +) {
if (array[j] = = temp) {
Already exist.
Give me back this chance.
i--;
Isexist = true;
}
}
Determine if this number should be saved
if (isexist = = False) {
Save
Array[i] = temp;
} else{
There was
Isexist = false;
}
}
Bubble sort
int temp;
for (int i = 0; i < 4; i++) {
for (int j = 4-1-1; J >= i; j--) {
if (Array[j] > array[j+1]) {
temp = Array[j];
ARRAY[J] = array[j+1];
ARRAY[J+1] = temp;
}
}
}
for (int i = 0; i < 4; i++) {
printf ("%d", array[i]);
}
printf ("\ n");
Receiving input from a user
int totalwrongtime = 10;
int wrongtime = 0;
int inputedarray[4] = {};
int CountA = 0;
int COUNTB = 0;
do {
printf ("Please enter numbers:");
for (int i = 0; i < 4; i++) {
scanf ("%d", &inputedarray[i]);
}
Judging the user's input results
for (int i = 0; i < 4; i++) {
for (int j = 0; J < 4; J + +) {
if (array[i] = = Inputedarray[j]) {
Numbers exist.
Determine if the location is the same
if (i = = j) {
The same is the location
CountA + +;
} else{
Different locations
COUNTB + +;
}
}
}
}
Process the results
if (CountA = = 4) {
printf ("Congratulation to You!\n");
Break
} else{
printf ("%da%db\n", CountA, COUNTB);
Wrongtime + +;
Zeroing the results
CountA = 0;
COUNTB = 0;
}
} while (Wrongtime < totalwrongtime);
return 0;
}
1208.1--Guess number Games