赞
踩
有n个座位,人依次选择座位入座,他们在选择座位时会选择尽可能距离人远的位置,不能出现相邻座位同时坐人的,问是否能正常入座,若可以则输出第一个人选择的位置。
只有当第一人左右的位置数都是2的次方时,才能正常入座。
#include<iostream> #include<algorithm> #include<cstring> #define int long long using namespace std; int check(int x) { return (x>0)&&(x&(x-1))==0; } void solve() { int t; int x=1; cin>>t; if(t==1||t==3) { cout<<1<<endl; return; } for(int i=0;i<=60;i++) { x*=2; if(check(t-x-1)) { cout<<x+1<<endl; return; } } cout<<"impossible"<<endl; } signed main() { solve(); return 0; }
给定一个字符串,需要将最后一个辅音字母,后面删除并加上 n t r y ntry ntry输出。
从后开始遍历,标记最后一个辅音字母的位置,进行删除和添加操作。
#include<iostream> #include<algorithm> #include<cstring> using namespace std; string s; int main() { cin>>s; int temp; for(int i=s.size()-1;i>=0;i--) { if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u') { temp=i; break; } } for(int i=0;i<=temp;i++) cout<<s[i]; cout<<"ntry"<<endl; }
一共有d个门,其中有一个门后有一只山羊,你需要需要选择s个门,然后会在另外你没有选择的门中选择e个门打开,然后你可以更换你所选择的门。问你能获取羊的最大概率。
三门问题,结论:你需要尽可能地更换掉你原来选择的门,这样才能最大概率获取羊。证明可参考
#include<iostream> #include<algorithm> using namespace std; void solve() { double d,s,e; cin>>d>>s>>e; double p=s/d; int m=d-s-e; double res=0; if(m>=s) { res+=(1-p)*s/m; } else{ res+=(1-p); res+=(s-m)/d; } printf("%.8lf\n", res); } int main() { solve(); return 0; }
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。