Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Sequence {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String[] input = sc.nextLine().split(" ");
- int k = Integer.parseInt(input[0]);
- int n = Integer.parseInt(input[1]);
- Queue<Integer> queue = new ArrayDeque<>();
- int count = 0;
- queue.offer(k);
- int number = k;
- for (int i = 1; i < n; i++) {
- count++;
- if (count == 1) {
- queue.offer(queue.peek() + 1);
- number = queue.peek() + 1;
- }
- if (count == 2){
- queue.offer((2* queue.peek()) + 1);
- number = (2* queue.peek()) + 1;
- }
- if(count == 3){
- queue.offer(queue.peek() + 2);
- number = queue.poll() + 2;
- count = 0;
- }
- }
- System.out.println(number);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment