Advertisement
Centipede18

beautifulNumber

Apr 1st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. int prime(long long n){
  6.     if(n < 2) return 0;
  7.     for(int i=2; i <= sqrt(n); i++){
  8.         if(n % i == 0) return 0;
  9.     }
  10.     return 1;
  11. }
  12.  
  13. int beautifulNumber(int n){
  14.     int i=1, a[100005];
  15.     int r = n%10;
  16.     n/=10;
  17.     a[0] = r;
  18.     while(n > 0){
  19.         int t = n % 10;
  20.         if(t == r) return 0;
  21.         n/=10;
  22.         a[i++]=t;
  23.     }
  24.     int check1 = 0, check2 = 0;
  25.     for(int j = 1; j <= i; j++){
  26.         if(a[i] - a[i-1] > 0) check1 = 1;
  27.         if(a[i] - a[i-1] < 0) check2 = 1;
  28.     }
  29.     if(check1 == 0 && check2 == 1) return 1; //day tang vi mang a dang bi dao nguoc
  30.     if(check1 == 1 && check2 == 0) return 1; //day giam vi mang a dang bi dao nguoc
  31.     return 0;
  32. }
  33.  
  34. main(){
  35.     int n, count = 0;
  36.     cin>>n;
  37.     for(int i = pow(10, n-1); i < pow(10, n); i++){
  38.         if(beautifulNumber(i) && prime(i)){
  39.             count++;
  40. //          cout<<i<<' ';
  41.         }
  42.     }
  43.     cout<<count;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement