[Java] Extract substrings (AOJ13 --shuffle)

Extract a substring from a string

public class Main {
	public static void main(String[] args) {
    String str = "Why is a cat so MOFU?";
    //10~Extract up to the 14th character
    String mofu= str.substring(10,15);
    //Show Yamazaki
    System.out.println(mofu); //MOFU
    }
}

Shuffle (ITP1-9)

Shuffle a pile of n cards with one alphabet drawn on them. In one shuffle, you take out the h cards from the bottom and stack them on top of the remaining pile of cards. The pile of cards is given as a single string as follows. abcdeefab The first letter indicates the card at the bottom and the last letter indicates the card at the top. For example, if you shuffle this with a h of 4, the first 4 characters abcd will be concatenated to the end of the remaining characters eefab, so: eefababcd Repeat this shuffle several times. Create a program that reads the first sequence (character string) and h column of the card pile and outputs the last sequence (character string). Input Multiple datasets are given as input. Each dataset is given in the following format: A string representing the first sequence Shuffle count m h1 h2 . . hm When the character string representing the first sequence is "-", it is the end of input. Output For each data set, output the last sequence (character string) on one line.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			String a = scanner.next();
			if (a.equals("-")) {
				break;
			}
			int b = scanner.nextInt();
			//Calculate the total value for b lines
			int sum = 0;
			for (int i = 0; i < b; i++) {
				sum += scanner.nextInt();
			}
			System.out.println(sum); //7
			System.out.println(a); //vwxyz 5
			//Double
			String c = a + a; //vwxyzvwxyz
            //System.out.println(c);
            
            /*One character at a time in the string a,(sum/String)I want to move it to the left by the amount of
substring method
Starting position:(sum/String a length)Too much 7/5=1...2   2
End position:(sum/String a length)Too much+String a length 2+5=9
			*/
			System.out.println(c.substring(sum % a.length(), sum % a.length() + a.length()));
		}
	}
}

Recommended Posts

[Java] Extract substrings (AOJ13 --shuffle)
[Java] Search for substrings / elements (AOJ12 --ring)
Java to extract PDF text content
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
[Java] Output multidimensional array / spreadsheet (AOJ⑥ spreadsheet)
Try to extract java public method