C++ PTA 小於m的最大的10個素數


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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM