2019-01-21-B

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.

Examples
Input
10 3
Output
5
Input
7 7
Output
6
Note
In the first sample Volodya's sequence will look like this: {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}. The third place in the sequence is therefore occupied by the number 5.
問題鏈接:https://cn.vjudge.net/contest/279624#problem/B
問題簡述:n個(gè)數(shù)重新排序并進(jìn)行查詢
問題分析:
1.n是奇數(shù),k位置鎖定在n/2+1前后并有遞推公式
2.n是偶數(shù),k位置鎖定在n/2前后并有遞推公式

程序說明:剛開始的想法:思路是把奇數(shù)和偶數(shù)重新復(fù)制到新的兩個(gè)數(shù)組在進(jìn)行搜索,但發(fā)現(xiàn)會(huì)出現(xiàn)運(yùn)行時(shí)間錯(cuò)誤問題 建議少使用數(shù)組,使用時(shí)是最好有提示數(shù)據(jù)不大
程序如下:

#include<iostream>
using namespace std;
int main()
{
    long long int n, k;
    cin >> n>>k;
    
    if (n%2 == 0)
    {
        if (k <= n / 2)
        {

            cout << 2 * k - 1;
        }
        else cout << 2 * (k-n/2);
    }
    else 
    {
        if (k <= n / 2 + 1)
        {
            cout << 2 * k - 1;
        }
        else cout << 2 * (k - n / 2-1);
    }
    system("pause");
    return 0;
}



?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,826評(píng)論 0 10
  • 2018.11.2相信當(dāng)下呈現(xiàn)的一切都是你的內(nèi)在美,并將它活出來。 今天收到梅姐的禮物,(一身運(yùn)動(dòng)衣質(zhì)量特別好.)...
    生活點(diǎn)滴_5224閱讀 156評(píng)論 0 2
  • PHP 在 5.3 版本中映入了 Namespace 機(jī)制, 避免協(xié)同開發(fā)中遇到的同名問題. 同時(shí)增加了魔術(shù)常量:...
    觀星漢閱讀 1,177評(píng)論 0 50
  • 老家院子里一個(gè)輩分比我大的95后姑娘,被親媽逼著在家相親,她媽說不結(jié)婚就哪兒也別想去!從年前到現(xiàn)在,她已參加相親無...
    葉子216閱讀 3,430評(píng)論 0 6
  • 今天鴿了 劃劃水寫了兩篇論文PPT
    deathneverdie閱讀 96評(píng)論 0 0

友情鏈接更多精彩內(nèi)容