- 题解
devfvbfg
- @ 2026-6-12 17:17:40
#include <bits/stdc++.h>
using namespace std;
int main(){
cout << endl << "=====猜数游戏=====" << endl;
cout << "请输入一个正整数n,我将会从1-n中随机选择一个数字作为所猜目标数字" << endl;
int n;
int minn=100;
cin >> n;
for(int i=1;i<=n;i++){
int wy=pow(2,i);
if(wy==n){
minn=min(minn,i);
minn-=1;
}else if(wy>n){
minn=min(minn,i);
}
}
int cs=minn;
static mt19937 rng(random_device{}());
uniform_int_distribution<int> dist(1, n);
int x = dist(rng);
cout << "我已选择完毕,游戏正式开始" << endl;
cout << "你有" << cs << "次猜测机会,如果在这几次之内猜中,你赢,否则算我赢" << endl;
cout << "你每次猜完后,如果不正确,我将会告诉你正确答案比这个数大,还是小" << endl << endl;
for(int j=1;j<=cs;j++){
cout << "这是第" << j << "次" << endl;
cout << "请输入你猜的数" << endl;
int cd;
cin >> cd;
if(cd==x){
cout << "太厉害了,你赢了!" << endl;
return 0;
}else if(cd>x){
cout << "比这个数小" << endl << endl;
}else if(cd<x){
cout << "比这个数大" << endl << endl;
}
}
cout << "非常遗憾,都没有猜中,我赢了!" << endl;
return 0;
}
0 条评论
目前还没有评论...