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