码迷,mamicode.com
首页 > 编程语言 > 详细

Even Odds (java)

时间:2019-02-10 20:17:20      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:new   class   lin   data   ase   display   ref   line   port   

从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 }

 

 

Even Odds (java)

标签:new   class   lin   data   ase   display   ref   line   port   

原文地址:https://www.cnblogs.com/youpeng/p/10360033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!