从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与中间数的比较,但是超时.
import java.util.Scanner; public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long n,k;
n = scanner.nextLong();
k = scanner.nextLong();
long mid = (n+1)/2;
if(k <= mid) {
System.out.println(k * 2 - 1);
}
else {
System.out.println((k - mid) * 2);
}
}
}