Advertisement
Jhonny_V

Untitled

Oct 1st, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. package com.company;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner s = new Scanner(System.in);
  9.         int N = Integer.parseInt(s.nextLine());
  10.         //create array of booleans
  11.         String[] answers = new String[N];
  12.         //keep all answers in array
  13.         for (int i = 0; i < N; i++) {
  14.             answers[i] = String.valueOf(CheckSort());
  15.         }
  16.         //print answers elements
  17.         for (String el : answers) {
  18.             System.out.println(el);
  19.         }
  20.     }
  21.     //create function that takes list input from console and returns boolean if list is sorted
  22.     private static boolean CheckSort() {
  23.         Scanner s = new Scanner(System.in);
  24.         String StringInput = s.nextLine();
  25.         //create array of Strings
  26.         String[] ArrStrings = StringInput.split(",");
  27.         //create sorted Array and keep clone of original Array, change both arrays to strings and compare them
  28.         String[] sorted = ArrStrings.clone();
  29.         Arrays.sort(sorted);
  30.         String strSorted = String.join(",", sorted);
  31.         String strArray = String.join(",", ArrStrings);
  32.         return strArray.equals(strSorted);
  33.  
  34.     }
  35. }
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement