Captain Flint and a Long Voyage
Captain Flint and a Long Voyage
- 題目
- 解釋
- 代碼
題目
Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.
In the beginning, uncle Bogdan wrote on a board a positive integer x consisting of n digits. After that, he wiped out x and wrote integer k instead, which was the concatenation of binary representations of digits x consists of (without leading zeroes). For example, let x=729, then k=111101001 (since 7=111, 2=10, 9=1001).
After some time, uncle Bogdan understood that he doesn’t know what to do with k and asked Denis to help. Denis decided to wipe last n digits of k and named the new number as r.
As a result, Denis proposed to find such integer x of length n that r (as number) is maximum possible. If there are multiple valid x then Denis is interested in the minimum one.
All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?
Note: in this task, we compare integers (x or k) as numbers (despite what representations they are written in), so 729<1999 or 111<1000.
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
Next t lines contain test cases — one per test case. The one and only line of each test case contains the single integer n (1≤n≤105) — the length of the integer x you need to find.
It’s guaranteed that the sum of n from all test cases doesn’t exceed 2?105.
Output
For each test case, print the minimum integer x of length n such that obtained by Denis number r is maximum possible.
Example
Input
2
1
3
Output
8
998
Note
In the second test case (with n=3), if uncle Bogdan had x=998 then k=100110011000. Denis (by wiping last n=3 digits) will obtain r=100110011.
It can be proved that the 100110011 is the maximum possible r Denis can obtain and 998 is the minimum x to obtain it.
解釋
題意看半天居然挺懵逼的
題意大概是:
輸入n
你需要輸出十進制n位數x
這個十進制x滿足:{
x的每一位轉化成二進制,再加到一起的這個二進制數,
去掉最后n位,是最大的情況
}
而且,在0到9中,8(1000)和9(1001)這兩個數字的二進制有四位,其他都少于四位,所以直接在8和9中選擇結果
結合代碼的注釋差不多了
代碼
#include<bits/stdc++.h> using namespace std; int main() {int t;cin >> t;while (t--){int n;cin>>n;//n代表2進制最后n位,也代表總數量 int q = n/4;//最后n位包含十進制的幾位 if(n%4 != 0){//位數余出n%4位,就是十進制的那個數的二進制被切了一部分,只要有取余出,q++ q++;}for(int i=0 ; i<n-q ; i++){printf("9");}for(int i=0 ; i<q ; i++){printf("8");}printf("\n");}return 0; }參考:
Codeforces #660 (Div. 2) B. Captain Flint and a Long Voyage
總結
以上是生活随笔為你收集整理的Captain Flint and a Long Voyage的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派vsftpd 425 Failed
- 下一篇: 缓存更新的机制