AtCoder Beginner Contest 244 题解A~D

A题

代码

#include <iostream>
#include <cstring>
using namespace std;


int main()
{
	int n;
	string str;
	cin>>n>>str;
	cout<<str[n-1];
	return 0;
}

B题

代码

#include <iostream>
#include <cstring>
using namespace std;


int main()
{
	int n,t=0,x=0,y=0;//t=0东 t=1南 t=2西 t=3北 
	string str;
	cin>>n>>str;
	for(int i=0;i<n;i++)
	{
		if(str[i]=='S')
		{
			if(t==0) x++;
			else if(t==1) y--;
			else if(t==2) x--;
			else y++;
		}
		else
		{
			if(t==3) t=0;
			else t++;
		}
	}
	cout<<x<<" "<<y;
	return 0;
}

C题

分析

这是一道交互题,因此在输出后要加上一行cout.flush();
我们可以用数组记录每个数是否出现过。为了优化,再用一个数组记录从 1 1 1开始已知连续出现过了的数的个数 + 1 +1 +1。遍历时从所记录的数开始。

代码

#include <iostream>
#include <cstdio>
using namespace std;

int n,x,t=2;
bool a[2010];
int main()
{
	scanf("%d",&n);
	printf("1\n");
	cout.flush();
	for(int i=0;i<n;i++)
	{
		scanf("%d",&x);
		a[x]=true;
		while(true)
		{
			if(a[t]==false)
			{
				printf("%d\n",t);
				cout.flush();
				a[t]=true;
				t++;
				break;
			}
			t++;
		}
	}
	scanf("%d",&x);
	return 0;
}

D题

分析

不妨来看下面几种情况:

  • S S S T T T是完全一样的。
  • S S S T T T有有且只有两个字母位置反了。
  • S S S T T T完全不一样。

那这三种情况下的 S S S能不能在 1 0 18 10^{18} 1018次交换后和 T T T一样呢?

  • S S S中任选两个字符来回交换,结束时即可为 T T T
  • 无法交换为 T T T
  • 先花 2 2 2步替换成 T T T,然后操作同第一种情况。

分析完这几种情况后,代码就很好写了。

代码

#include <iostream>
using namespace std;


int main()
{
	char s1,s2,s3,t1,t2,t3;
	cin>>s1>>s2>>s3>>t1>>t2>>t3;
	if(s1==t1&&s2==t2&&s3==t3) cout<<"Yes";
	else if(s1!=t1&&s2!=t2&s3!=t3) cout<<"Yes";
	else cout<<"No";
	return 0;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值