PTA 部分题目

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;
}

猜你喜欢

转载自www.cnblogs.com/dirror/p/12768079.html