C++ 随机数
2026-02-08 11:19:26
发布于:四川
C++随机数
伪随机
rand(填一个数字);
纯随机
srand(time(0));
rand();
实际应用
#include <bits/stdc++.h>
using namespace std;
int main(){
//生成随机数
srand(time(0));
int l,r;
l=1,r=100;
int mid;
int x=rand()%100+1;
//一直循环,直到mid==随机数
while(1){
mid=(l+r)/2;
if(mid<x){
printf("%d小了\n",mid);
l=mid+1;
}else if(mid>x){
printf("%d大了\n",mid);
r=mid-1;
}else{
cout<<mid;
break;
}
}
return 0;
}
这里空空如也



















有帮助,赞一个