1208.1--Guess number Games

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.