#include
#include
#include
using namespace std;
void main()
{
int a[4],b[4];
int i,j,k;
bool flag=false;
srand( (unsigned)time( NULL ) ); //初始化rand
//产生随机数
a[0]=rand()%10;
for(i=1;i<=3;i++)
{
a[i]=rand()%10;
for(j=0;j while(a[i]==a[j])
a[i]=rand()%10;
}
//输出提示信息
cout<<"程序已经产生了一个四位互不重复的随机数字,"
<<"请在下方每行分别没简茄键入一个一位数,注意也应当互不相同枯察,"咐此
<<"以增加获胜的概率\n请输入:"<
//输入
k=0;
while(k<8)
{
i=0;
while(i<4)
{
cin>>b[i];
i++;
}
//比较判断
int count=0,correct=0;
int c[4];//用来保存数值正确的数字
int d[4];//用来保存位置和数值都正确的数字
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(a[i]==b[j])
{
c[count++]=b[j];
if(i==j)
d[correct++]=b[j];
}
}
if(correct==4)
{
cout<<"Congratulations!!!您猜对了!正确的数字是:";
for(i=0;i<4;i++)
cout< cout<
flag=true;//游戏胜利标志
break;
}
//结论输出
cout<<"猜对了"<
if(correct!=0)
{
cout<
cout<<"其中,有"<
else
cout<<",位置并不正确";
cout<
k++;
}
//游戏失败
if(flag==false)
{
cout<<"很遗憾,您没有猜中,正确的数字是:";
for(i=0;i<4;i++)
cout<cout<
}
}
#include
#include
using namespace std;
#define Random (rand() % 10)
void makenumbers(int *num)
{
int count = 1;
num[0] = Random;
while(count != 4)
{
LOOP:
num[count] = Random;
for(int i = 0; i < count; i++)
{
if(num[count] == num[i])
goto LOOP;
}
count++;
}
}
void guessnumbers(int *num)
{
char inputnum[5];
int A, B;
cout << "薯埋input: "宽运 << endl;
for(int i = 0; i < 8; i++)
{
A = 0, B = 0;
cin >> inputnum;
for(int j = 0; j < 4; j++)
{
if(num[j] == inputnum[j] - 48)
A++;
}
for(int m = 0; m < 4; m++)
{
for(int n = 0; n < 4; n++)
{
if(inputnum[m]-48 == num[n] && m != n)
B++;
}
}
if(A == 4)
{
cout << "Congratulations!" << endl;
break;
}
else
cout << A << "A" <<慎手梁 B << "B" << endl;
}
}
int main()
{
srand((unsigned)time(NULL));
int *num = new int[4];
makenumbers(num);
guessnumbers(num);
delete []num;
return 0;
}