CF每日一水

CF每日一水

A. Madoka and Math Dad

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Madoka finally found the administrator password for her computer. Her father is a well-known popularizer of mathematics, so the password is the answer to the following problem.

Find the maximum decimal number without zeroes and with no equal digits in a row, such that the sum of its digits is n.

Madoka is too tired of math to solve it herself, so help her to solve this problem!

Input
Each test contains multiple test cases. The first line contains a single integer t (1≤t≤1000) — the number of test cases. Description of the test cases follows.

The only line of each test case contains an integer n (1≤n≤1000) — the required sum of the digits.

Output
For each test case print the maximum number you can obtain.

Example
inputCopy
5
1
2
3
4
5
outputCopy
1
2
21
121
212
Note

The only numbers with the sum of digits equal to 2 without zeros are 2 and 11. But the last one has two ones in a row, so it’s not valid. That’s why the answer is 2.

The only numbers with the sum of digits equal to 3 without zeros are 111, 12, 21, and 3. The first one has 2 ones in a row, so it’s not valid. So the maximum valid number is 21.

The only numbers with the sum of digits equals to 4 without zeros are 1111, 211, 121, 112, 13, 31, 22, and 4. Numbers 1111, 211, 112, 22 aren’t valid, because they have some identical digits in a row. So the maximum valid number is 121.

下面是翻译

在这里插入图片描述
大佬的思路:
题目要求构造各位不包含0,各位相加等于n且相邻两位不同的最大的数,想要构造最大的数最好就是位数多,位数越多构造出来的数越大,根据要求相邻两位不能相同,所以取两个最小的正整数1和2来构造答案。
可以发现,构造出来的答案一定是1和2交替出现,既然要交替出现,那么1和2的数量最多相差一个。

菜鸡我的思路

选取最小的数,尽可能的让最后的长度最长,也就是说位数越长越好,位数越长意味着可使用的数就小了,其实题目中说“不能包含零”,“两字符不能重复”就已经给了思路了,就1和2拼接就完事了,代码如下:

#我这里只是读取一个数字,题目要求n个,加个循环就是,
需求过于简单,不予实现

n=int(input()) 
t=n//3
l=n%3
s="21"*t
str=s
if l==1:
    str="1"+s
elif l==2:
    str=s+"2"
print(str)
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值