[思维]Supreme Number

题目链接:https://nanti.jisuanke.com/t/31452

思路:首先,十以内的质数:2、3、5、7。然后一百以内的,2只且只能和3搭配即23,5同理,所以只剩下了3、7。但每个质数不能出现两次(当然1有些时候可以出现两次,但1不是质数呀)否则子序列就是合数了。

  在两位数的基础上加1、3、7(上面提过了不能加2、5),枚举出剩下的可能,打个表ok~

代码如下:

 1 #include <iostream>
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 int a[20]={1,2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317};
 5 int num,len;
 6 string str;
 7 void change()
 8 {
 9     num=0;
10     for(int i=0;i<len;i++)
11     {
12         num*=10;
13         num+=str[i]-'0';
14     }
15     //cout << num << endl;
16 }
17 int main()
18 {
19     int T;
20     cin>>T;
21     for(int i=1;i<=T;i++)
22     {
23         cin>>str;
24         len=str.size();
25         if(len>=4)
26         {
27             printf("Case #%d: ",i);
28             printf("317\n");
29         }
30         else
31         {
32             change();
33             int ans;
34             for(int j=0;j<20;j++)
35             {
36                 if(a[j]<=num)
37                     ans=a[j];
38                 else
39                     break;
40             }
41             printf("Case #%d: ",i);
42             printf("%d\n",ans);
43         }
44 
45     }
46     //cout << "Hello world!" << endl;
47     return 0;
48 }
View Code

猜你喜欢

转载自www.cnblogs.com/SoulSecret/p/9610244.html