#28437: C解法(真的不會的可以看一下)


az07260906 (呢嗎嗎)

學校 : 不指定學校
編號 : 125438
來源 : [61.224.2.106]
最後登入時間 :
2022-01-22 18:31:27
d387. 10235 - Simply Emirp -- UVa10235 | From: [61.224.8.191] | 發表日期 : 2021-12-09 22:54

#include <stdio.h>

#include <math.h>

 

int prime(int);//用來判斷是否為質數 

int swap(int);//用來翻轉數字 

int cycle(int);//用來驗證是否為迴文質數

 

int main(){

int m;

int i;

 

while(scanf("%d", &m)!=EOF){

 

if(prime(m) != 0 && m>=10 && prime(swap(m)) != 0 ){

if(cycle(m) == 0){

printf("%d is prime.\n", m);

}

else{

printf("%d is emirp.\n", m);

}

}

else if(prime(m) == 0){

printf("%d is not prime.\n", m);

}

else{

printf("%d is prime.\n", m);

}

}

 

return 0;

}

int prime(int N){

int a;

int counter;

 

counter = 1;

a = 2; 

while(a <= sqrt(N)){

if(N%a != 0){

a++;

}

else{

counter = 0;

break;

}

}

return counter;

int swap(int M){

int a[100];

int i,j;

int total;

 

i=total=0;

while(M != 0){

a[i] = M%10;

M = M/10;

i++;

}

 

for(j=0; j<i; j++){

total = (total*10)+a[j]; 

}

return total;

}

int cycle(int K){

int s[35];

int i,j,k,counter;

i = counter = 0;

 

while(K != 0){

s[i] = K%10;

K = K/10;

i++;

}

 

for(j=0, k=i-1; j<=k; j++, k--){

if(s[j] != s[k]){

counter = 1;

}

}

 

return counter;

}

 
#28438: Re:C解法(真的不會的可以看一下)


az07260906 (呢嗎嗎)

學校 : 不指定學校
編號 : 125438
來源 : [61.224.2.106]
最後登入時間 :
2022-01-22 18:31:27
d387. 10235 - Simply Emirp -- UVa10235 | From: [61.224.8.191] | 發表日期 : 2021-12-09 23:13

#include

#include

 

int prime(int);//用來判斷是否為質數 

int swap(int);//用來翻轉數字 

int cycle(int);//用來驗證是否為迴文質數

 

int main(){

int m;

int i;

 

while(scanf("%d", &m)!=EOF){

 

if(prime(m) != 0 && m>=10 && prime(swap(m)) != 0 ){

if(cycle(m) == 0){

printf("%d is prime.\n", m);

}

else{

printf("%d is emirp.\n", m);

}

}

else if(prime(m) == 0){

printf("%d is not prime.\n", m);

}

else{

printf("%d is prime.\n", m);

}

}

 

return 0;

}

int prime(int N){

int a;

int counter;

 

counter = 1;

a = 2; 

while(a <= sqrt(N)){

if(N%a != 0){

a++;

}

else{

counter = 0;

break;

}

}

return counter;

int swap(int M){

int a[100];

int i,j;

int total;

 

i=total=0;

while(M != 0){

a[i] = M%10;

M = M/10;

i++;

}

 

for(j=0; j<i; j++){

total = (total*10)+a[j]; 

}

return total;

}

int cycle(int K){

int s[35];

int i,j,k,counter;

i = counter = 0;

 

while(K != 0){

s[i] = K%10;

K = K/10;

i++;

}

 

for(j=0, k=i-1; j<=k; j++, k--){

if(s[j] != s[k]){

counter = 1;

}

}

 

return counter;

}

第一個if條件中m>=10要改掉。

沒有查好迴文質數的定義就丟上來,抱歉..

 
ZeroJudge Forum