第一部分 题目
第二部分 分析
首先,让你求猜对多少次,次数一定是个整数,所以输出格式是把你往弯路上引的。
其次,我们有几个做法:
第 个做法
直接模拟解决。
每次随机选一张牌,然后与小海的决策进行比较。
第 种做法
既然随机数可以解决,我们可以使用一个固定的策略解决,这样不用使用随机数了。
我们可以采用先全抽“,再全抽 ”的策略,简单、明了。
第 种做法
根据第 种做法,我们可以做出以下讨论:
- 小海一次猜 一次猜 ,最终答案为 。
- 小海会一直猜 直到 ,共猜对 次。
- 小海会一直猜 ,会猜错前 次,接下来猜对 次。
综上,我们可以得出答案为 。
第三部分 代码
第 种
#include <bits/stdc++.h>
using namespace std;
double a,b,c;
int main(){
srand(time(0));
cin>>a>>b;
while(a>1e-6||b>1e-6){
int rnd=rand()%2,k;
if(rnd==0&&!a)k=1;
if(rnd==1&&!b)k=0;
if(k==0){
if(a>b){
c++;
}
a--;
}else{
if(b>a){
c++;
}
b--;
}
}
printf("%.8f",c);
return 0;
}第 种
#include <bits/stdc++.h>
using namespace std;
double a,b,c;
int main(){
//srand(time(0));
cin>>a>>b;
while(a>1e-6||b>1e-6){
int rnd,k;
rnd=0;
if(rnd==0&&!a)k=1;
if(rnd==1&&!b)k=0;
if(k==0){
if(a>b){
c++;
}
a--;
}else{
if(b>a){
c++;
}
b--;
}
}
printf("%.8f",c);
return 0;
}第 种做法
#include <bits/stdc++.h>
using namespace std;
int a,b,c;
int main(){
cin>>a>>b;
cout<<max(a,b)<<".00000000";
return 0;
}