A prime number (or a prime) is a natural number greater than 111 that cannot be formed by multiplying two smaller natural numbers.
Now lets define a number NNN as the supreme number if and only if each number made up of an non-empty subsequence of all the numeric digits of NNN must be either a prime number or 111.
For example, 171717 is a supreme number because 111, 777, 171717 are all prime numbers or 111, and 191919 is not, because 999 is not a prime number.
Now you are given an integer N (2≤N≤10100)N\ (2 \leq N \leq 10^{100})N (2≤N≤10100), could you find the maximal supreme number that does not exceed NNN?
Input
In the first line, there is an integer T (T≤100000)T\ (T \leq 100000)T (T≤100000) indicating the numbers of test cases.
In the following TTT lines, there is an integer N (2≤N≤10100)N\ (2 \leq N \leq 10^{100})N (2≤N≤10100).
Output
For each test case print "Case #x: y"
, in which xxx is the order number of the test case and yyy is the answer.
样例输入
2 6 100
样例输出
Case #1: 5 Case #2: 73
题目来源
题意:找到N以内最大的supreme number:此数为1或素数,且该数的子序列也是1或素数(不按顺序,随机重组);
可以发现1位数有:1,2,3,5,7;2位数有:11,13,17,23,31,37,53,71,73;3位数有:113,131,137,173,311,317;
代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int a[20] = {1,2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317};
int s;
int t,count = 1;
cin >> t;
while(t--)
{
cin >> s;
cout << "Case #" << count++ << ": " ;
if(s >= 317)
{
cout << a[19] << endl;
continue;
}
else
{
for(int i = 19;i >= 0;i --)
{
if(s >= a[i])
{
cout << a[i] << endl;
break;
}
}
}
}
return 0;
}
刚开始我错了很多次的代码是这样的:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int a[20] = {1,2,3,5,7,
11,13,17,23,31,37,53,71,73,
113,131,137,173,311,317};
int s;
int t,count = 1;
cin >> t;
while(t--)
{
cin >> s;
cout << "Case #" << count++ << ": " ;
if(s >= 317)
{
cout << a[19] << endl;
continue;
}
else
{
for(int i = 19;i >= 0;i --)
{
if(s >= a[i])
{
cout << a[i] << endl;
break;
}
}
}
}
return 0;
}
在数组里加了不必要的换行;。。。。。。。。
我用的是codeblocks,可以正常运行,提交的时候就不对了。。。。。
还是基础不行啊。