题目链接
- 查找区间中的素数然后按格式输出。
- 素数的题都快做烂了,官方好像出过好几道了。
- python是真的慢,我也到网上找了几份代码,那个测试点都超时了。
c++代码
#include <iostream>
#include<string>
#include<vector>
#include<math.h>
using namespace std;
int judge(int n) {
if (n < 2)
return 0;
for (int i = 2; i*i <= n; i++) {
if (n%i == 0)
return 0;
}
return 1;
}
int main() {
int m, n;
int count = 0;
vector<int> res;
cin >> m >> n;
int i = 0;
while (count < n) {
i++;
if (judge(i) == 1) {
count++;
if (count >= m)
res.push_back(i);
}
}
count = 0;
for (int i = 0; i < res.size(); i++) {
count++;
if (i < res.size() - 1 && count < 10)
cout << res[i] << " ";
else
{
cout << res[i] << endl;
count = 0;
}
}
return 0;
}
复制代码
python3代码
mport math
def judge(n):
if(n< 2):
return 0
max = int(math.sqrt(n))
for i in range(2,max+1):
if(n%i==0):
return 0
return 1
def main():
s = input().split()
num = list(map(int,s))
res = []
i = 2
count = 0
while(count < num[1]):
if(judge(i) == 1):
count += 1
if(count >= num[0]):
res.append(i)
i += 1
count = 0
for i in range(res.__len__()):
count+=1
if(count < 10 and i != res.__len__() -1):
print(res[i],end=' ')
else:
print(res[i])
count = 0
main()
复制代码
转载于:https://juejin.im/post/5ce57c2af265da1b70048073