Advertisement

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).

请勿在C++中使用%lld格式化符来读取或写入64位整数。建议采用cin、cout流或使用%I64d格式化符。
输出位置k处的数字。
输入包括两个整数n和k。
目标是根据Volodya的重排规则确定第k个位置上的数字是什么。
重排规则为:首先将1到n的所有奇数按升序排列;接着将所有偶数也按升序排列。
编写程序以快速确定第k个位置上的数字。
解决方案较为直接。
代码如下:

复制代码
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    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;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    代码解释

全部评论 (0)

还没有任何评论哟~