Advertisement
Georgi_Benchev

Untitled

Dec 17th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. package FinalCodingTasksDSA;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Task2 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int num = scanner.nextInt();
  10.         int pos = scanner.nextInt();
  11.  
  12.         int[] memorisation = new int[pos];
  13.         for (int i = 1; i <= pos; i++) {
  14.             getPos1(num, i, memorisation);
  15.  
  16.         }
  17.         System.out.println(memorisation[pos - 1]);
  18.     }
  19.  
  20.     private static void getPos1(int num, int pos, int[] memorisation) {
  21.         int specialPos = pos % 3;
  22.         if (pos == 1) {
  23.             memorisation[0] = num;
  24.  
  25.  
  26.         } else if (specialPos == 2) {
  27.             memorisation[pos - 1] = memorisation[pos / 3] + 1;
  28.  
  29.         } else if (specialPos == 0) {
  30.             memorisation[pos - 1] = 2 * memorisation[(pos - 1) / 3] + 1;
  31.  
  32.         } else if (specialPos == 1) {
  33.             memorisation[pos - 1] = memorisation[(pos - 2) / 3] + 2;
  34.         }
  35.     }
  36.  
  37.     // 2 10
  38.     // 2   3 5 4  4 7 5
  39.  
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement