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.
时间: 2024-01-05 22:02:48 浏览: 120
Sure, I can help you with that! The sequence that Polycarp likes consists of all positive integers that do not have a digit 3 in their decimal representation, except for those that are divisible by 3. To generate this sequence up to a certain index, we can simply iterate through positive integers and check whether each integer meets these conditions. Here's some Python code that does just that:
```python
def is_disliked(n):
return '3' in str(n) or n % 3 == 0
def get_polycarp_sequence(k):
sequence = []
n = 1
while len(sequence) < k:
if not is_disliked(n):
sequence.append(n)
n += 1
return sequence
t = int(input())
for i in range(t):
k = int(input())
sequence = get_polycarp_sequence(k)
print(sequence[-1])
```
Here, `is_disliked(n)` is a helper function that checks whether an integer `n` is disliked by Polycarp. The `get_polycarp_sequence(k)` function generates the sequence up to the `k`-th element by iterating through positive integers and appending each integer that is not disliked to the sequence. Finally, in the main loop, we read in the number of test cases `t`, and for each test case we read in `k`, generate the sequence up to the `k`-th element, and output the `k`-th element by printing the last element of the sequence.
阅读全文