2022.4.13 20:06 洛谷P1563

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 = 0

for 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 - b
        else:
            now_index = now_index - b

    elif ((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 - n
        else:
             now_index = now_index + b

print((t[now_index])[1])


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值