Codeforces 1560A Dislike of Threes Solution & Explanation

Difficulty : 800

Problem Description

Polycarp doesn’t like integers that are divisible by 3 or end with the digit 3 in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.

Polycarp starts to write out the positive (greater than 0) integers which he likes: 1,2,4,5,7,8,10,11,14,16,…. Output the k-th element of this sequence (the elements are numbered from 1).

Input

The first line contains one integer t (1≤t≤100) — the number of test cases. Then t test cases follow.

Each test case consists of one line containing one integer k (1≤k≤1000).

Output

For each test case, output in a separate line one integer x — the k-th element of the sequence that was written out by Polycarp.

Examples

Input5
213132
973894
045207
000000
055776
OutputYES
NO
YES
YES
NO

Solution

In our program, we use a counter and a variable to find the k-th number that meets Polycarp’s preferences.

C# Solution

Solution1

int t = int.Parse(Console.ReadLine());

for(int i=0; i<t; i++){
    int k = int.Parse(Console.ReadLine());
    int cnt = 0;
    int num = 1;
    while(true){
        //numbers he dislike
        if(num%3==0||num%10==3){
            num+=1;
            continue;
        }
        
        //numbers he like
        cnt+=1;
        
        if(cnt==k){
            Console.WriteLine(num);
            break;
        }
        
        num+=1;
    }
}

The main approach involves using a loop and conditional checks to examine each number to see if it meets Polycarp’s preference criteria, and to count the numbers that meet the criteria until finding the k-th such number for each test case.

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = Integer.parseInt(scanner.nextLine());

        for (int i = 0; i < t; i++) 
        {
            int k = Integer.parseInt(scanner.nextLine());
            int cnt = 0;
            int num = 1;
            while(true){
                //numbers he dislike
                if(num%3==0||num%10==3){
                    num+=1;
                    continue;
                }
                
                //numbers he like
                cnt+=1;
                
                if(cnt==k){
                    System.out.println(num);
                    break;
                }
                
                num+=1;
            }
        }
    }
}
Klook.com

Python3 Solution

Solution1

t = int(input())

for i in range(t):
    k = int(input())
    cnt = 0
    num = 1
    
    while(True):
        if(num%3==0 or num%10==3):
            num+=1
            continue
    
        cnt+=1
        
        if(cnt==k):
            print(num)
            break
        
        num+=1

Conclusion

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 1560A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *