Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.text.DecimalFormat;
- import java.util.Arrays;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.stream.Collectors;
- public class Main {
- public static void main (String [] args){
- Scanner scanner = new Scanner(System.in);
- List<Integer> numbers = Arrays.stream(scanner.nextLine()
- .split(" ")).map(Integer::parseInt)
- .collect(Collectors.toList());// List of numbers
- String text = scanner.nextLine();// String
- List<String> list = Arrays.stream(text.split(""))
- .collect(Collectors.toList());// Turns the string into List
- List<String> result = new ArrayList<>();// Empty list for the output
- for (int i=0; i<numbers.size(); i++){// For loop, repeats for each number in the number list
- int element = digitsSum(numbers.get(i));// The char position in the string; comes from the method below
- if (element>=list.size()){// If the char position is beyond the list size, start counting from 0
- element = element - list.size();
- }
- result.add(list.get(element));// Takes the character and puts it in the result list
- list.remove(element);// Removes the character from the original list
- }
- for (String letter : result){// Prints the final result as string
- System.out.print(letter);
- }
- }
- public static int digitsSum (int number){// Method to get the sum of digits from a number
- String temp = "" + number;
- int result = 0;
- for (int i=0; i<temp.length(); i++){
- result+=(int)temp.charAt(i) - 48;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement