shusu1669 『入门』(递归专题)m位数问题
This commit is contained in:
parent
777981363e
commit
ef43e42fd4
|
@ -0,0 +1,17 @@
|
|||
# 1669 『入门』(递归专题)m位数问题
|
||||
|
||||
[问题描述]
|
||||
考官只给两个整数n和m(1 <= n <= 8,1 <= m <= 5),要求选手从1,2,…,n中取出m个数字,组成一个m位整数,统计所有的m位整数中一共有多少个素数。
|
||||
如n=3,m=2时,符合条件的整数有:11 12 13 21 22 23 31 32 33。其中素数有:11 13 23 31,一共有4个。
|
||||
|
||||
[输入格式]
|
||||
一行,两个整数n m,(1 <= n <= 8,1 <= m <= 5 )。
|
||||
|
||||
[输出格式]
|
||||
一行,一个整数,表示素数的个数。
|
||||
|
||||
[输入样例]
|
||||
3 2
|
||||
|
||||
[输出样例]
|
||||
4
|
|
@ -0,0 +1 @@
|
|||
#
|
|
@ -0,0 +1,41 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
int shu,chang,flag=0,ans=0;
|
||||
bool pan(int n){
|
||||
if(n<2){
|
||||
return 0;
|
||||
}
|
||||
if(n==2){
|
||||
return 1;
|
||||
}
|
||||
int len=sqrt(n);
|
||||
for(int i=2;i<=len;i++){
|
||||
if(n%i==0){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void dg(int now){
|
||||
if(now==chang+1){
|
||||
if(pan(flag)==1){
|
||||
ans++;
|
||||
}
|
||||
}else{
|
||||
for(int i=1;i<=shu;i++){
|
||||
flag*=10;
|
||||
flag+=i;
|
||||
dg(now+1);
|
||||
flag/=10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
cin>>shu>>chang;
|
||||
dg(1);
|
||||
cout<<ans;
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
8 4
|
|
@ -0,0 +1 @@
|
|||
8 5
|
|
@ -0,0 +1 @@
|
|||
8 1
|
|
@ -0,0 +1,18 @@
|
|||
## z:\Chao\src\1669_nweishu\test\in.txt
|
||||
2020/04/30 ÖÜËÄ 18:38:22.64
|
||||
464
|
||||
-----------------------------------------------
|
||||
Process exited after 150 ms with return value 0
|
||||
|
||||
## z:\Chao\src\1669_nweishu\test\in2.txt
|
||||
2020/04/30 ÖÜËÄ 18:38:22.64
|
||||
2871
|
||||
-----------------------------------------------
|
||||
Process exited after 140 ms with return value 0
|
||||
|
||||
## z:\Chao\src\1669_nweishu\test\in3.txt
|
||||
2020/04/30 ÖÜËÄ 18:38:22.64
|
||||
4
|
||||
-----------------------------------------------
|
||||
Process exited after 100 ms with return value 0
|
||||
|
Loading…
Reference in New Issue