Advertisement
llvlleo1810

Đếm các phần tử là số nguyên tố trên đường chéo chính và đườ

Apr 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <math.h>
  4. bool snt(int n){
  5.     if (n <= 1) return false ;
  6.     if (n == 2) return true ;
  7.     if (n % 2 == 0) return false ;
  8.     else {
  9.         for (int i = 3; i <= sqrt(n); i += 2)
  10.             if (!(n % i)) return false ;
  11.             return true ;
  12.     }
  13. }
  14. int main(void) {
  15.     int n, c = 0 ;
  16.     scanf("%d", &n) ;
  17.     int a[n][n], danhdau[100000] ;
  18.     for (int i = 0; i < 100000; i ++) danhdau[i] = 0 ;
  19.     for (int i = 0; i < n; i ++){
  20.         for (int j = 0 ; j < n; j ++) {
  21.             scanf("%d", &a[i][j]) ;
  22.             if (i == j || i == n - j - 1)
  23.                 if (snt(a[i][j]) && !danhdau[a[i][j]]) {
  24.                     c ++ ;
  25.                     danhdau[a[i][j]] ++ ;
  26.                 }
  27.         }
  28.     }
  29.     printf("%d", c) ;
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement