Advertisement
halexandru11

atestat_10.cpp

Nov 24th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. // returneaza cifra de control a numarului(x)
  8. // cifra de control a unui numar in baza 10 (b) este egala cu restul impartirii numarului la 9 (b-1)
  9. // daca restul este 0 atunci cifra de control este 9 (b-1)
  10. unsigned int cifra(unsigned int x) {
  11.     return (x%9 ? x%9 : 9);
  12. }
  13.  
  14. // determina daca numarul x este prim
  15. unsigned int prim(unsigned int x) {
  16.     if(x < 2) {
  17.         return 0;
  18.     }
  19.  
  20.     for(unsigned int div = 2; div*div <= x; ++div) {
  21.         if(x%div == 0) {
  22.             return 0;
  23.         }
  24.     }
  25.     return 1;
  26. }
  27.  
  28. int main() {
  29.     ifstream fin("date.in");
  30.     ofstream fout("date.out");
  31.  
  32.     unsigned int n, cnt = 0;
  33.     fin >> n;
  34.     while(n--) {
  35.         unsigned int x;
  36.         fin >> x;
  37.         x = cifra(x);
  38.         if(prim(x)) {
  39.             ++cnt;
  40.         }
  41.     }
  42.     fout << cnt;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement