Advertisement
desislava_topuzakova

04.

Jan 27th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. package Arrays;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ArrayRotation_04 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String [] array = scanner.nextLine().split(" ");
  9. //"51 47 32 61 21".split(" ") -> ["51", "47", "32", "61", "21"]
  10. int countRotations = Integer.parseInt(scanner.nextLine());
  11.  
  12. for (int rotation = 1; rotation <= countRotations; rotation++) {
  13. //ротация на масива
  14. //1. взимам първия елемент
  15. String firstElement = array[0]; //"51"
  16.  
  17. //2. преместваме елементите наляво
  18. //["51", "47", "32", "61", "21"] -> ["47", "32", "61", "21", "21"]
  19. for (int index = 0; index < array.length - 1 ; index++) {
  20. array[index] = array[index + 1];
  21. }
  22.  
  23. //3. поставям първия елемент на последно място
  24. //["47", "32", "61", "21", "51"]
  25. array[array.length - 1] = firstElement;
  26. }
  27.  
  28. //приключваме с ротациите -> принтираме всички елементи на масива
  29. for (String element : array) {
  30. System.out.print(element + " ");
  31. }
  32. }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement