Codeforces--318A--Even Odds

题目描述:
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.
输入描述:
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.
输出描述:
Print the number that will stand at the position number k after Volodya’s manipulations.
输入:
10 3
7 7
输出:
5
6
题意:
重排自然数的顺序,假设有1到n的数字,重排规则如下:首先从1到n的所有奇数(按升序排序),然后从1到n的所有偶数(也按升序排列)。重排顺序之后,请写程序快速找出位置k的数字是谁。
题解
直接搞
代码:

#include
#include
#include
#include
using namespace std;

typedef long long ll;

int main(){
    ll n,k;
    while(scanf("%I64d%I64d",&n,&k)!=EOF){
        ll ans = 0;
        if(n % 2 == 0)
            ans = n / 2;
        else
            ans = (n / 2) + 1;
        if(k <= ans)
            printf("%I64d\n", 2 * k - 1);
        else
            printf("%I64d\n", 2 * (k - ans));
    }
    return 0;
}

你可能感兴趣的:(算法,数学)