Supreme Number
A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying two smaller natural numbers.
Now lets define a number NN as the supreme number if and only if each number made up of an non-empty subsequence of all the numeric digits of NN must be either a prime number or 11.
For example, 1717 is a supreme number because 11, 77, 1717 are all prime numbers or 11, and 1919 is not, because 99 is not a prime number.
Now you are given an integer N\ (2 \leq N \leq 10^{100})N (2≤N≤10100), could you find the maximal supreme number that does not exceed NN?
Input#
In the first line, there is an integer T\ (T \leq 100000)T (T≤100000) indicating the numbers of test cases.
In the following TT lines, there is an integer N\ (2 \leq N \leq 10^{100})N (2≤N≤10100).
Output#
For each test case print "Case #x: y"
, in which xx is the order number of the test case and yy is the answer.
样例输入复制
2
6
100
样例输出复制
Case #1: 5
Case #2: 73
#include <iostream>
#include <sstream>
#define ll long long
using namespace std;
int a[20]={317, 311,173,137,131,113,73,71,53,37,31,23,17,13,11,7,5,3,2,1};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
for(int t=1;t<=T;t++)
{
int aa;
stringstream aaa;
string s;
cin>>s;
aaa<<s;
aaa>>aa;
int len=s.length();
if(len>=4||aa>317) {
cout<<"Case #"<<t<<": "<<a[0]<<endl;
continue;
}
if(aa==317) {
cout<<"Case #"<<t<<": "<<"317"<<endl;continue;
}
for(int i=1;i<20;i++)
{
if(aa<a[i-1]&&aa>=a[i])
{
cout<<"Case #"<<t<<": "<<a[i]<<endl;
break;
}
}
}
return 0;
}
这个题的题意,说的是给你一个大于等于2,且小于等于10的100次方,要你求的是,小于这个数,但是这个数要是素数,而且这个数里面任意x个数的组合也要是素数的数,x个数可以不连续。
首先输入很大, 所以使用了string类,读入一个字符串,然后用了stringstream,避免缓存溢出。因为将字符串转换为整数的方式可以用stdio里面的sprintf转换,但是由于数字太大,所以就换用stringstream这个字符串流。
然后就是枚举所有的是素数,而且每一位都是素数,且任何几位的组合都是素数的数,总共也就二十个,应该是吧,请读者自行验证之。
然后就是最上面的ios_base::sync_with_stdio(flase)解除绑定的,和cin.tie(0)都是加快cin的速度的。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· 大模型工具KTransformer的安装
· [计算机/硬件/GPU] 显卡