|  | 
 
 
 楼主|
发表于 2020-6-4 21:51:23
|
显示全部楼层 
| 复制代码#include<iostream>
#include<cstdlib>
#include<ctime>
#include<windows.h>
#include<Mmsystem.h>
#pragma comment(lib,"winmm.lib")
#include<dsound.h>
using namespace std;
int main()
{
        system("color 2f");
        system ("title 星海风驰");
        cout<<"欢迎来到猜数字的游戏,系统会随机给出四位整数,您有十次机会来猜对此数字"<<endl;
    cout<<"每次猜测后,系统将会提示您,其中位置和数字均正确将以A表示,数字正确而位置错误将以B表示"<<endl;
    cout<<"希望您能顺利过关!!!!"<<endl;
        int i,j;                                                                                        //计数器
        int num,guess;                                                                                //用来存放随机数和用户数
        int arr1[4],arr2[4];                                                                //分别存放各位随机数和各位猜测数字
        int a=0,b=0;                                                                                //用来统计A,B
        int count=1;                                                                                //用来统计用户的猜测此数
        srand(unsigned(time(NULL)));                                                //产生随机数
        num=(rand()%5000+5000)*17/19;
        while(count<=10)
        {
        arr1[3]=num%10;
        arr1[2]=num/10%10;
        arr1[1]=num/100%10;
        arr1[0]=num/1000;
        a=0;b=0;
        cout<<"请输入第"<<count<<"个四位数:";
    PlaySound("强化失败.wav", NULL, SND_ASYNC | SND_LOOP | SND_NODEFAULT);
        cin>>guess;
        if(guess>9999 or guess<1000 or guess%1 !=0) {cout<<"输入不合法,请输入一个四位数"<<endl;continue;}                //判断输入是否合法
        arr2[3]=guess%10;
        arr2[2]=guess/10%10;
        arr2[1]=guess/100%10;
        arr2[0]=guess/1000;
        for(i=0;i<=3;i++)                                                                //这里用两个循环来校验用户数和猜测数
        {
                for(j=0;j<=3;j++)
                        if(arr1[i]==arr2[j]&&i==j)
                        {a++;arr1[i]=10;arr2[j]=11;}
        }
        for(i=0;i<=3;i++)
        {
                for(j=0;j<=3;j++)
                        if(arr1[i]==arr2[j])
                        {b++;arr1[i]=10;arr2[j]=11;}
        }
        if(a==4) break;
        cout<<"  "<<a<<"A"<<b<<"B"<<endl;
        count++;
        }
        if(count==11)
                cout<<"您好,正确的数为"<<num<<endl<<"由于你猜测错误,程序即将爆炸....."<<endl;
        else
                cout<<"恭喜您猜对了,此数为"<<num<<endl<<"由于您智商过高,程序即将爆炸"<<endl;
        system("pause=NULL");
        return 0;
}
 | 
 |