A
A. Madoka and Math Dad
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 nn.
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 tt (1≤t≤10001≤t≤1000) — the number of test cases. Description of the test cases follows.
The only line of each test case contains an integer nn (1≤n≤10001≤n≤1000) — the required sum of the digits.
Output
For each test case print the maximum number you can obtain.
input:
5
1
2
3
4
5
output:
1
2
21
121
212
Note
The only numbers with the sum of digits equal to 22 without zeros are 22 and 1111. But the last one has two ones in a row, so it's not valid. That's why the answer is 22.
The only numbers with the sum of digits equal to 33 without zeros are 111111, 1212, 2121, and 33. The first one has 22 ones in a row, so it's not valid. So the maximum valid number is 2121.
The only numbers with the sum of digits equals to 44 without zeros are 11111111, 211211, 121121, 112112, 1313, 3131, 2222, and 44. Numbers 11111111, 211211, 112112, 2222 aren't valid, because they have some identical digits in a row. So the maximum valid number is 121121.
题解:这题的意思就是:给你一个十进制的数,要你找到一个最大的数使数的每位之和等于n,并且要求这个数相邻的数不能相同如:11,且不能含0
分析:想要使这个数越大越好,那么我们可以从数的位数入手,位数越多就越大,应为不能含有0 ,所以要想位数多,就只能选最小的数,而相邻的数不能相同所以只能选1和2.现在要确定的就是应该是那个数开头,这里我们可以把书看做由(“0”1 ,2)组成的三进制数当n%3=1的时候就把1放最前面,余数为2或者0就把2放在最前面
#include<iostream>
using namespace std;
int n, t;
int main() {
cin >> t;
while (t--) {
cin >> n;
if (n % 3 == 1) {//余数为1,数字以1开头
int num = 1;
for (int i = n; i; num++) {//当i为0的时候退出循环
if (num & 1) {//作用:使1 2循环出现 每次加一能够使二进制的位数在 0 1 之间变换
cout << "1";
i -= 1;
}
else {
cout << "2";
i -= 2;
}
}
cout << endl;
}
else {
int num = 1;
for (int i = n; i; num++) {
if (num & 1) {
cout << "2";
i -= 2;
}
else {
cout << "1";
i -= 1;
}
}
cout << endl;
}
}
return 0;
}