C:(100分)
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#define Max 100010 //题目得知 最多可能有100000个人int direction[Max]; //用于记录每个人 是朝内还是朝外,0为朝内,1为朝外,下标从1开始应用char all[Max][11] = { 0 }; //用于储存接收到的所有人的名字,下标从1开始应用int now_index = 1; //用于表示当前为 几号小人int n, m;void zhixing(int a, int b)
{//思路://朝外 + 向右 = 朝内 + 向左 (顺时针)//朝内 + 向右 = 朝外 + 向左 (逆时针)// 朝外 + 向右 == 朝内 + 向左 (顺时针)if ((direction[now_index] == 1 && a == 1) || (direction[now_index] == 0 && a == 0)){if (now_index - b <= 0) //这里要<= 如果是单纯的<只有95分,要考虑特殊情况{now_index = n + now_index - b;}else{now_index -= b;}}// 朝内 + 向右 == 朝外 + 向左 (逆时针)else if ((direction[now_index] == 0 && a == 1) || (direction[now_index] == 1 && a == 0)){if (now_index + b > n){now_index = now_index + b - n;}else{now_index += b;}}return;
}int main()
{scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++){scanf("%d %s", &direction[i], all[i]);}for (int i = 1; i <= m; i++){int a, b;scanf("%d %d", &a, &b);zhixing(a, b); //用来计算跳人数的结果}printf("%s", all[now_index]);return 0;
}
C++:(100分)
发现如果参照C的方法,只单纯加入string和bool的话,或有栈溢出的问题
于是参考了其他大佬的思路,用了类
#define _CRT_SECURE_NO_WARNINGS 1#include <iostream>#include <string>using namespace std;#define Max 100010 //题目得知 最多可能有100000个人class people
{
public:bool direction;string name;}people[Max];int now = 1; //用于表示当前为 几号小人int n, m;void zhixing(int a, int b)
{//思路://朝外 + 向右 = 朝内 + 向左 (顺时针)//朝内 + 向右 = 朝外 + 向左 (逆时针)// 朝外 + 向右 == 朝内 + 向左 (顺时针)if ((people[now].direction == 1 && a == 1) || (people[now].direction == 0 && a == 0)){if (now - b <= 0) //这里要<= 如果是单纯的<只有95分,要考虑特殊情况{now = n + now - b;}else{now -= b;}}// 朝内 + 向右 == 朝外 + 向左 (逆时针)else if ((people[now].direction == 0 && a == 1) || (people[now].direction == 1 && a == 0)){if (now + b > n){now = now + b - n;}else{now += b;}}return;
}int main()
{cin >> n >> m;for (int i = 1; i <= n; i++){cin >> people[i].direction >> people[i].name;}for (int i = 1; i <= m; i++){int a, b;scanf("%d %d", &a, &b);zhixing(a, b); //用来计算跳人数的结果}cout << people[now].name << endl;return 0;
}
python:(35分)
仿C语言版写的,然鹅并不能AC
n, m = map(int, input().split(" "))t = []now_index = 0for i in range(0, n):t.append(input().split(" "))for i in range(0, m):a, b = map(int, input().split(" "))if ((t[now_index])[0] == '1' and a == 1) or ((t[now_index])[0] == '0' and a == 0):if now_index - b < 0:now_index = n + now_index - belse:now_index = now_index - belif ((t[now_index])[0] == '0' and a == 1) or ((t[now_index])[0] == '1' and a == 0):if (now_index + b) > n:now_index = now_index + b - nelse:now_index = now_index + bprint((t[now_index])[1])