hwangnk1004 / Algorithm

0 stars 0 forks source link

StepByStep #shuffle #277

Open hwangnk1004 opened 4 years ago

hwangnk1004 commented 4 years ago

import java.util.Scanner;

public class Main { public static void main(String args[]) {

    Scanner scanner = new Scanner(System.in);

    while (true) {
        int n = scanner.nextInt();
        int r = scanner.nextInt();
        if (n == 0 || r == 0) {
            break;
        }

        int A[] = new int[n];
        for (int i = 0; i < n; i++) {
            A[i] = i + 1;
        }
        while (r-- > 0) {
            int p = scanner.nextInt();
            int c = scanner.nextInt();
            int B[] = new int[n];
            int idx = 0;
            for (int i = 0; i <= n - p - c; i++) {
                B[idx++] = A[i];
            }

            for (int i = n - p+1; i < n; i++) {
                B[idx++] = A[i];
            }
            for (int i = n - p - c + 1; i <= n - p; i++) {
                B[idx++] = A[i];
            }

            for (int i = 0; i < n; i++) {
                A[i] = B[i];
            }
        }
        System.out.println(A[n-1]);
    }

}

}