用c++实现这个问题,并给出代码You are given a integer n (n > 0). Find any integer s which satisfies these conditions, or report that there are no such numbers: In the decimal representation of s: s > 0 s consists of n digits, no digit in s equals 0, s is not divisible by any of it's digits. Input The input consists of multiple test cases. The first line of the input contains a single integer t(1≤t≤400), the number of test cases. The next t lines each describe a test case. Each test case contains one positive integer n (1≤n≤10_5). It is guaranteed that the sum of n for all test cases does not exceed 10^5 Output For each test case, print an integer s which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for s, print any solution. Note In the first test case, there are no possible solutions for ss consisting of one digit, because any such solution is divisible by itself. For the second test case, the possible solutions are: 2323, 2727, 2929, 3434, 3737, 3838, 4343, 4646, 4747, 4949, 5353, 5454, 5656, 5757, 5858, 5959, 6767, 6868, 6969, 7373, 7474, 7676, 7878, 7979, 8383, 8686, 8787, 8989, 9494, 9797, and 9898. For the third test case, one possible solution is 239239 because 239239 is not divisible by 22, 33 or 99 and has three digits (none of which equals zero).
时间: 2023-06-14 16:03:19 浏览: 201
以下是用 C 语言实现的代码:
```c
#include <stdio.h>
#include <stdbool.h>
int main() {
int t;
scanf("%d", &t); // 输入测试用例个数
while (t--) { // 处理每个测试用例
int n;
scanf("%d", &n); // 输入位数
bool found = false; // 是否找到符合条件的数
for (int i = 1; i <= 9; i++) { // 枚举最高位
int s = i; // 构造数 s
bool valid = true; // 判断 s 是否符合条件
for (int j = 1; j < n; j++) { // 构造 s 的每一位
int digit = s % 10 + 1; // 枚举当前位可能的值
if (digit > 9) { // 如果枚举完了所有可能的值都不符合条件,就退出循环
valid = false;
break;
}
while (s % digit == 0) { // 如果当前位已经是一个因子,就将其值加一
digit++;
if (digit > 9) { // 如果枚举完了所有可能的值都不符合条件,就退出循环
valid = false;
break;
}
}
if (!valid) { // 如果已经不符合条件,就退出循环
break;
}
s = s * 10 + digit; // 将当前位加到 s 的末尾
}
if (valid) { // 如果找到符合条件的数,就输出它并标记 found 为 true
printf("%d\n", s);
found = true;
break;
}
}
if (!found) { // 如果找不到符合条件的数,就输出 -1
printf("-1\n");
}
}
return 0;
}
```
思路如下:
对于每个测试用例,我们首先枚举最高位,然后从高到低构造数 s 的每一位。对于每个位置,我们枚举可能的值(即当前位的值加 1 到 9),如果当前位已经是一个因子,则将其值加一。如果枚举完了所有可能的值都不符合条件,则退出循环。如果找到符合条件的数,则输出它并标记 found 为 true。如果枚举完所有可能的最高位都找不到符合条件的数,则输出 -1。
阅读全文