文曲星上猜数字游戏

// guessNumber.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <time.h>
using namespace std;
void guess( int randomNum[4]);
int n=0;
void main()
{
    cout<<"*****************猜数字小游戏***************"<<endl;
    cout<<"***输入四个不同的0~9间整数且首位不要为0***"<<endl;
    cout<<"**************Such as: 9876 Enter***********"<<endl;

    int randomNum[4],temp[10],randomSum,n=10;

    srand((unsigned)time( NULL ));

    for(int i=0;i<=3;i++)
    {
        randomNum[i]=rand() % 10;//得到四个0-9的随机数,不过可能相同或首位为0
    }
    for(int i=0;i<=9;i++)
    {
        temp[i]=i;
    }

    for(int i=0;i<=3;i++)//在0-9中找出与四个随机数不相等的,并赋给temp数组
        for(int j=0;j<9;j++)
        {
            if(randomNum[i]==temp[j])
            {
                for(int k=j;k<9;k++)
                    temp[k]=temp[k+1];
            }
        }

        for(int i=0;i<=3;i++)
        {
            randomSum=0;
            randomSum+=randomNum[i];
        }

        for(int i=1;i<=3;i++)//通过%得出temp下标,并赋给randomNum,保证randomNum不相等
            for(int j=0;j<i;j++)
            {
                if(randomNum[i]==randomNum[j])
                {
                    randomNum[i]=temp[(randomSum++)%6];
                }
            }



            if(randomNum[0]==0)  //保证首位不为0
            {
                int temp;
                temp=randomNum[0];
                randomNum[0]=randomNum[1];
                randomNum[1]=temp;
            }
            guess(randomNum);

}


void guess( int randomNum[])
{

    int input,a=0,b=0;
    int inputArray[4];
    cin>>input;

    for(int i=3;i>=0;i--)
    {
        inputArray[i]=input%10;
        input/=10;
    }

    for(int i=0;i<=3;i++)
    {
        if(inputArray[i]==randomNum[i])
            a++;
    }
    for(int i=0;i<=3;i++)
        for(int j=0;j<=3;j++)
        {
            if(inputArray[i]==randomNum[j])
                b++;
        }

        if(a==4)
        {
            cout<<"succeed!"<<endl;
            exit(0);
        }

        else
        {
            cout<<a<<"A"<<b-a<<"B"<<endl;

            n++;

            if(n==10)
            {

                cout<<"还是看答案吧,呵呵~~"<<endl;

                for(int i=0;i<=3;i++)
                    cout<<randomNum[i];
                cout<<endl;

                exit(0);
            }
            else
            {
                guess(randomNum);
            }

        }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值