跳到正文

个人记录

c++猜数字

chengxihang888约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年2月8日
更新
2026年2月8日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

#include<bits/stdc++.h>

using namespace std;

int main ()

{

srand((int)time(0)); long long c[5],m[5],x[10],ans=0,sum=1;

C++
memset(x,-1,sizeof(x));

while (sum<=4)

{
	ans=rand()%10;

	if (x[ans]==-1)

	{

		x[ans]=1;

		m[sum]=ans;

		sum++;

	}

}
cout<<"输入密码"<<endl;

for (long long i=1;i<=10;i++)

{

	long long a=0,b=0;

	for (long long j=1;j<=4;j++)

	{

		cin>>c[j];

		if (c[j]==m[j])

		{

			a++;

		}else

		{
			for (long long l=1;l<=4;l++)

			{
				if (j!=l && c[j]==m[l])

				{

                			b++;
				}
			}
		}
	}
	cout<<a<<'A'<<"    "<<b<<'B'<<endl;
	if (a==4)
	{
		cout<<"看来你并没有我想象中的那么傻";
		return 0;
	}
}
cout<<"你输了,你个傻瓜!"<<endl;
cout<<"正确答案是:";
for (long long i=1;i<=4;i++)
{
	cout<<m[i]<<" ";
}
return 0;

}

原文