Even Odds (java)

从1到n的奇数,从1到n之间的偶数,排列在一起,找到第k个数

Input

输入包含 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

输出第 k个数

Examples

Input
10 3
Output
5
Input
7 7
Output
6

Note

案例1中排列为 {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}. 第三个数字是5

 

这个题主要是找个规律吧,一开始的时候,我是枚举的形式枚举到第k个数,当然为了省时,也做了下k与中间数的比较,但是超时.

 1 import java.util.Scanner;
 2 
 3 public class Main{
 4     public static void main(String[] args) {
 5         Scanner scanner = new Scanner(System.in);
 6         long n,k;
 7         n = scanner.nextLong();
 8         k = scanner.nextLong();
 9         long mid = (n+1)/2;
10         if(k <= mid) {
11             System.out.println(k * 2 - 1);
12         }
13         else {
14             System.out.println((k - mid) * 2);
15         }
16     }
17 }

 

猜你喜欢

转载自www.cnblogs.com/youpeng/p/10360033.html
今日推荐