B - Captain Flint and a Long Voyage(训练)

B - 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

这道题的题意是将一个n位数,进行两部操作,首先变化为2进制数,再者就是将二进制的后n位数去掉,记作r,去寻找使二进制r最大的,最小n位数。
首先这道题需要知道一个点,9的二进制是四位的1001,而8是1000也是四位,要寻找使二进制r最大的,也就是最长的,即都由9和8组成。
如果是一位数9

9 1001 去掉1位是 100
8 1000 去掉1位是 100

是相同的
同样

99 10011001 去掉2位 100110
98 10011000 去掉2位 100110

相同
每次查找最小的数是8 98 998 9998
现在分析完成,需要寻找9 8 个数关系,发现n/4时候应该是八的位数
故代码如下
下面展示一些 内联代码片

#include <stdio.h>
int main(void)
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		int t;                               //想要转化出最大,则需要转化出最长的二进制,即为9或8时 
		scanf("%d",&t);
		for(int i=0;i<t- ((t-1)/4+1);i++)
			printf("9");
						//显然取最小需要后面取8 
		for(int i=0;i<((t-1)/4+1);i++)
			printf("8");
			
		printf("\n");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值