algorithm
[c++] 뒤집은 소수, 소수의 개수
tonirr
2020. 12. 1. 22:54
- 뒤집은 소수
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
bool isPrime(int x){
bool flag=true;
int i;
if(x==1) {
return false;
}
for(i=2; i<x; i++){
if(x%i==0){
flag=false;
break;
}
}
return flag;
}
int reverse(int x){
int tmp=0, rev=0;
while(x>0){
tmp=x%10;
rev=rev*10+tmp;
x=x/10;
}
return rev;
}
int main() {
//freopen("input.txt", "rt", stdin);
int n, i, num, tmp;
scanf("%d", &n);
for(i=0; i<n; i++){
scanf("%d", &num);
tmp=reverse(num);
if(isPrime(tmp)){
printf("%d ", tmp);
}
}
return 0;
}
- 소수의 개수
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
//freopen("input.txt", "rt", stdin);
int n, i, j, flag, cnt=0;
scanf("%d", &n);
for(i=2; i<=n; i++){
flag=1;
for(j=2; j*j<=i; j++){
if(i%j==0){
flag=0;
}
}
if(flag==1){
cnt++;
}
}
printf("%d ", cnt);
return 0;
}