1 条题解

  • 0
    @ 2021-6-15 14:28:26

    C++ :

    #include <cstdio>
    #include <cstdlib>
    #include <memory.h>
    #include <algorithm>
    #include <string>
    #include <map>
    #include <set>
    #include <vector>
    #include <cmath>
    using namespace std;
    #define PI 3.14159265358979323846264338327950288
    int N, K;
    int main() {
       //freopen("function.in", "r", stdin);
       //freopen("function.out", "w", stdout);
       scanf("%d%d", &N, &K);
       if (N == 1)
          printf("1\n");
       else
          printf("%d\n", 2 * min(K, N - K + 1));
       return 0;
    }
    
    
    

    Java :

    import java.util.Scanner;
    
    public class Main {
        public static final int MAX_N = 55;
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            if (n == k && n == 1) {
                System.out.println(1);
            } else {
                if (2 * k <= n + 1)
                    System.out.println(2 * k);
                else
                    System.out.println(2 * (n - k + 1));
            }
    
        }
    }
    
    • 1

    信息

    ID
    996
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者