sunfresh

Sequence

Jan 30th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Sequence {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Scanner sc = new Scanner(System.in);
  8.  
  9. String[] input = sc.nextLine().split(" ");
  10.  
  11. int k = Integer.parseInt(input[0]);
  12. int n = Integer.parseInt(input[1]);
  13.  
  14. Queue<Integer> queue = new ArrayDeque<>();
  15. int count = 0;
  16. queue.offer(k);
  17. int number = k;
  18. for (int i = 1; i < n; i++) {
  19. count++;
  20. if (count == 1) {
  21. queue.offer(queue.peek() + 1);
  22. number = queue.peek() + 1;
  23. }
  24. if (count == 2){
  25. queue.offer((2* queue.peek()) + 1);
  26. number = (2* queue.peek()) + 1;
  27. }
  28. if(count == 3){
  29. queue.offer(queue.peek() + 2);
  30. number = queue.poll() + 2;
  31. count = 0;
  32. }
  33. }
  34. System.out.println(number);
  35. }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment