Facebook
From Do Quyen, 1 Month ago, written in C++.
Embed
Download Paste or View Raw
Hits: 132
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. bool snt[1000000];
  5. void sang(int n) {
  6.  
  7.     for(int i = 0; i <= n; i++) {
  8.         snt[i] = true;
  9.     }
  10.  
  11.     snt[0] = false;
  12.     snt[1] = false;
  13.  
  14.     for(int i = 2; i * i <= n; i++) {
  15.          if(snt[i] == true) {
  16.              for(int j = i * i; j <= n; j += i)
  17.                  snt[j] = false;
  18.         }
  19.     }
  20. }
  21.  
  22. int main()
  23. {
  24.     freopen("cloprime.inp", "r", stdin);
  25.     freopen("cloprime.out", "w", stdout);
  26.     sang(1000000);
  27.     int n;
  28.     cin>>n;
  29.  
  30.     for (int i=1; i<=n; i++){
  31.         int x;
  32.         cin>>x;
  33.         while (snt[x] == false)
  34.             x--;
  35.         cout<<x<<endl;
  36.     }
  37.     return 0;
  38. }
  39.