1 solutions

  • 0
    @ 2025-11-5 18:14:02

    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

    Information

    ID
    18824
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By