zoukankan      html  css  js  c++  java
  • 1208.1——猜数字小游戏

    #include <stdio.h>

    #include <stdlib.h>

    #include <time.h>

    #include <stdbool.h>

    int main(int argc, const char * argv[]) {

        /*

         4位数 从小到大  没有重复 1-9

         A 表示存在而且位置也对的个数 

         B 表示存在但是位置不对

         1 2 3 4

         1 4 6 8

         1A1B

         */

        int array[4] = {};

        bool isExist = false;

        

        //设置随机数的种子

        srand((unsigned int)time(NULL));

        for (int i = 0; i < 4; i++) {

            int temp = rand() % 9 + 1;//1-9

            //判断是否存在

            for (int j = 0; j < i; j++) {

                if (array[j] == temp) {

                    //已经存在了

                    //把这次机会还给我

                    i--;

                    

                    isExist = true;

                }

            }

            

            //判断是否应该保存这个数字

            if (isExist == false){

                //保存

                array[i] = temp;

            } else{

                //存在了

                isExist = false;

            }

            

        }

        

        //冒泡排序

        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(" ");

        

        //接收用户的输入

        int totalWrongTime = 10;

        int wrongTime = 0;

        int inputedArray[4] = {};

        int countA = 0;

        int countB = 0;

        

        do {

            printf("请输入数字:");

            for (int i = 0; i < 4; i++) {

                scanf("%d", &inputedArray[i]);

            }

            

            //判断用户的输入结果

            for (int i = 0; i < 4; i++){

                for (int j = 0; j < 4; j++) {

                    if (array[i] == inputedArray[j]) {

                        //数字存在了

                        //判断位置是否相同

                        if (i == j){

                            //位置也相同

                            countA ++;

                        } else{

                            //位置不同

                            countB ++;

                        }

                    }

                }

            }

            

            //对结果进行处理

            if (countA == 4){

                printf("Congratulation to you ! ");

                break;

            } else{

                printf("%dA%dB ", countA, countB);

                

                wrongTime ++;

                

                //对结果清零

                countA = 0;

                countB = 0;

            }

        } while (wrongTime < totalWrongTime);

        

        return 0;

    }

  • 相关阅读:
    自己写CPU第五级(5)——测试逻辑、实现移动和空指令
    ERROR: The partition with /var/lib/mysql is too full! failed!
    关于精益创业理念随想
    android使用ffmpeg
    如何让格斗游戏的横版过关(2) Cocos2d-x 2.0.4
    java两个音频进入巩固期 玩的同时类似的伴奏
    Java 反射 想
    SharePoint 2013 如何使用Silverlight
    如何才能连接到你的网站访客
    写作---英语中常见的写作错误有哪些
  • 原文地址:https://www.cnblogs.com/damonWq/p/5030977.html
Copyright © 2011-2022 走看看