1.21 B

B - 2

Time limit 1000 ms
Memory limit 262144 kB

Problem Description

Being a nonconformist, Volodya is displeased with the current state of things, particularly with the order of natural numbers (natural number is positive integer number). He is determined to rearrange them. But there are too many natural numbers, so Volodya decided to start with the first n. He writes down the following sequence of numbers: firstly all odd integers from 1 to n (in ascending order), then all even integers from 1 to n (also in ascending order). Help our hero to find out which number will stand at the position number k.

Input

The only line of input contains integers n and k (1 ≤ k ≤ n ≤ 1012).

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Output

Print the number that will stand at the position number k after Volodya’s manipulations.

Sample Input

10 3

7 7

Sample Output

5

6

问题链接:B - 2

问题简述:

输入n和k,分别代表截止的数和要输出数列中从左到右第k个数,如n=10,k=3,即输出1 3 5 7 9 2 4 6 8 10中第3个数,即5

问题分析:

首先分奇偶(要注意long long int),先算数列的中间数middle,偶数是n-1,奇数是n(不过并不是严格意义上的中间数),再算中间数所在位置,偶数是n/2,奇数是(n+1)/2(同理也不是严格意义上的中间数),然后根据中间数划分左右两个区域(即判断k和n/2((n+1)/2的大小),若k小或等于则用middle-2*(他们之间的差),若k大则用middle+2*(他们之间的差),最后输出即可

程序说明:

没啥好说的。。。

AC通过的C语言程序如下:

#include <iostream>
using namespace std;

int main()
{
	long long int n, k;
	cin >> n >> k;
	if (n % 2 == 0)
	{
		long long int middle = n - 1;
		if ((n / 2) >= k)
		{
			cout << middle - 2 * (n / 2 - k);
		}
		else { cout << 2 * (k-n / 2); }
	}
	else
	{
		long long int middle = n;
		if (((n+1) / 2) >= k)
		{
			cout << middle - 2 * ((n+1) / 2 - k);
		}
		else { cout << 2 * (k - (n+1) / 2); }
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44003969/article/details/86578412