https://www.luogu.org/problemnew/show/P1563
题解:题目不难,主要第一次遇到异或,就想写一下
C++版本一
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
typedef __int128 lll;
const int N=100000+1000;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
map< int,string>s;
bool a[N];
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
char str[20];
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d %s",&k,str);
a[i]=k;
s[i]=str;
}
int pos=1;
while(m--){
scanf("%d %d",&k,&q);
if(a[pos]^k){
pos+=q;
}else{
pos-=q;
}
if(pos<=0){
pos+=n;
}
if(pos>n){
pos-=n;
}
}
cout << s[pos] << endl;
//cout << "Hello world!" << endl;
return 0;
}
C++版本二
include<bits/stdc++.h>
using namespace std;
struct node
{
int head;
string name;
}a[100005];
int n,m,x,y;
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>a[i].head>>a[i].name;
}
int now=0;
for(int i=1;i<=m;i++)
{
cin>>x>>y;
if(a[now].head==0&&x==0)now=(now+n-y)%n;
else if(a[now].head==0&&x==1)now=(now+y)%n;
else if(a[now].head==1&&x==0)now=(now+y)%n;
else if(a[now].head==1&&x==1)now=(now+n-y)%n;
}
cout<<a[now].name<<endl;
return 0;
}
JAVA版本一
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Main {
static int place, n, m;// place是当前位置,n是人物个数,m是指令个数
static int[] N;// 存储人物朝向的数组
static String[] Name;// 存储人物名称的数组
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
n = in.nextInt();
m = in.nextInt();
N = new int[n];
Name = new String[n];
for (int i = 0; i < n; i++) {// 读取所有的人物
N[i] = in.nextInt();
Name[i] = in.next();
}
for (int i = 0; i < m; i++) {// 读取所有操作同时计算
opeart(in.nextInt(), in.nextInt());
}
System.out.println(Name[place]);
}
static void opeart(int i, int j) {
// 其实只有两种情况,
// 1 如果朝向内侧又向左移动表示为 0 0,如果朝向外侧有向右移动表示为1 1,这种情况下应该用当前位置减去移动的距离
// 2朝向和移动方向不想同0 1或者1 0,则应该用当前位置加上移动的距离
if (N[place] == i)
place = (place + n - j) % n;
else
place = (place + j) % n;
}
// 这是个内部类,借鉴cf上某个java大佬的写法,只是为了解决读取输入太慢的问题
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}