列表

详情


NC222156. CocktailWithSnake

描述

The favorite game of Mr.Cocktail is dota2. Serpentine walking is a useful skill to dodge the enemy's attack. Serpentine walking refers to the path of movement that keeps swing direction like a snake. Suppose the map is a two-dimensional plane. Mr.Cocktail at the point in initial.

He will first go to the right to the end, that is, point . Then turn and go one step upwards to point . Go to the left again to the end, that is, point ,. Then go one step up, and go to the right again to the end.

The picture above is the action line when and .


Now the map size is . The number of steps of Cocktail's movement are known. Find the Manhattan distance from the position after steps to the starting point according to the rule.

Manhattan distance Definition: The distance between two points measured along axes at right angles. In a plane with at and p_2 at , it is .

输入描述

The first line contains a positive integer , which represents the number of groups to be tested. 

Next contains lines, each row contains three integers

输出描述

For each test data, output a line of one coordinate to represent the answer. (A positive integer separated by two spaces represents the  and  coordinates respectively) 

示例1

输入:

5
3 3 1
3 3 2
3 3 3
3 3 4
3 3 5

输出:

1
2
3
2
1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3 解法, 执行用时: 80ms, 内存消耗: 6876K, 提交时间: 2021-05-22 16:55:56

n = int(input())
nums = [list(map(int, input().split())) for _ in range(n)]
for i in range(n):
    a, b, c = nums[i][0], nums[i][1], nums[i][2]
    d = c // a
    if d & 1 == 1:
        print(d + a - 1 - (c % a))
    else:
        print(d + c % a)

C 解法, 执行用时: 13ms, 内存消耗: 456K, 提交时间: 2021-05-22 14:19:32

#include<stdio.h>
int main(void){
	long long k,x,y,t,n,m;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld %lld %lld",&n,&m,&k);
		y=k/n;
		if(y%2==0)
			x=k%n;
		else
			x=n-1-k%n;
        printf("%lld\n",x+y);
	}
	return 0;
}

C++ 解法, 执行用时: 11ms, 内存消耗: 1144K, 提交时间: 2021-05-26 00:18:38

#include<stdio.h>
int main()
{
	long long k,x,y,t,n,m;
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld %lld %lld",&n,&m,&k);
		y=k/n;
		if(y%2==0)
		x=k%n;
		else
		x=n-1-k%n;
		printf("%lld\n",x+y);
	}
	return 0;
}

上一题