当前位置:   article > 正文

图论-寒假

图论-寒假

A - Amusement Arcade

题意:

有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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

B - Brexiting and Brentering

题意:

给定一个字符串,需要将最后一个辅音字母,后面删除并加上 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

I - Monty’s Hall

题意:

一共有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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/51880
推荐阅读
相关标签
  

闽ICP备14008679号