11 Ocak 2013 Cuma

找出數字字串中的最大質數


問題描述 : 給定一個2~100,000之間的數字,找出該數字的子字串中最大的質數值。 子字串為原字串(長度n)中連續取k個字元所組合而成,1 ≤ k ≤ n。
輸入說明 : 一個正整數,值介於2~100,000之間
輸出說明 : 若有,則輸出該數字的子字串中最大的質數值; 若無,請輸出”No prime found”
範例
123

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <math.h>  
  4.   
  5. int PrimeNumber(int temp){  
  6.     int i;  
  7.         if(temp==0){  
  8.         return 0;  
  9.     }  
  10.     for(i=2;i<=sqrt(temp);i++){  
  11.         if(temp%i==0){  
  12.             return 0;  
  13.         }  
  14.     }  
  15.     return 1;  
  16. }  
  17.   
  18. int Prime(int temp){  
  19.     int i,j,d,max=0,ttemp;  
  20.       
  21.     ttemp=temp;  
  22.     for(i=0;temp>0;i++,temp/=10);  
  23.     d=i;  
  24.     for(i=d;i>=0;i--){  
  25.         for(j=i-1;j>=0;j--)  
  26.         {  
  27.             temp=ttemp%(int)floor(pow(10,i));  
  28.             temp=temp/(int)floor(pow(10,j));  
  29.             if((max<temp)&&(PrimeNumber(temp)))  
  30.             {  
  31.                 max=temp;  
  32.             }  
  33.         }  
  34.     }  
  35.     return max;  
  36. }  
  37. int main(){  
  38.     int num;  
  39.     scanf("%d",&num);   
  40.     if(Prime(num)==0)  
  41.     {  
  42.         printf("No prime found\n");  
  43.     }  
  44.     else  
  45.     {  
  46.         printf("%d\n",Prime(num));  
  47.     }  
  48.   
  49.     system("PAUSE");  
  50.     return 0;  
  51. }  

Hiç yorum yok:

Yorum Gönder

Binary Conversion

Problem Description Convert two binary numbers into two decimal numbers and compute their sum. Your program has to convert two binary number...