C++ PTA 小于m的最大的10个素数

2023-03-15,,

7-5 小于m的最大的10个素数 (15分)

  给定一个整数m(50<m<20000),找出小于m的最大的10个素数。

输入格式:

  输入在一行中给出一个正整数m(50<m<20000)。

输出格式:

  在一行中按递减顺序输出10个满足条件的素数,每个素数输出占6列。没有其它任何附加格式和字符。

输入样例:

  229

输出样例:

  227 223 211 199 197 193 191 181 179 173

#include <iostream>
#include<iomanip>
using namespace std;
int judge(int n) {
int i;
for (i = 2; i < n; i++)
if (n % i == 0)break;
if (i < n) return 0;
else return 1;
}
int main() {
int m, i, count = 0;
cin >> m;
for (i = m - 1; i > 1; i--) {
if (judge(i)) {
cout << setw(6) << i;
count++;
}
if (count == 10)break;
}
return 0;
}

C++ PTA 小于m的最大的10个素数的相关教程结束。

《C++ PTA 小于m的最大的10个素数.doc》

下载本文的Word格式文档,以方便收藏与打印。