12--c完數/最大公約數/最小公倍數/素數/迴文數

 

完數/最大公約數/最小公倍數/素數/迴文數

 分類:
 
 

 

1.一個正整數的因子是全部能夠整除它的正整數。而一個數若是剛好等於除它自己外的因子之和,這個數就稱爲完數。例如6=1+2+3(6的因子是1,2,3)。post

 

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. #include <stdio.h>  
  2. #include <math.h>  
  3. int IsPerfect(int x);  
  4. int main()  
  5. {  
  6.     int m;  
  7.     printf("Input m:");  
  8.     scanf("%d", &m);  
  9.   
  10.     if (IsPerfect(m))  /* 徹底數斷定 */  
  11.         printf("%d is a perfect number\n", m);  
  12.     else  
  13.         printf("%d is not a perfect number\n", m);  
  14.     return 0;  
  15. }  
  16.   
  17.   
  18. /* 函數功能:判斷徹底數,若函數返回0,則表明不是徹底數,若返回1,則表明是徹底數 */  
  19. int IsPerfect(int x)  
  20. {  
  21.     int i;  
  22.     int total = 0;          /* 1沒有真因子,不是徹底數 */  
  23.   
  24.     for (i=1;i<x;i++)  
  25.     {  
  26.         if (x%i==0)  
  27.             total = total + i;  
  28.     }  
  29.     return total==x ? 1 : 0;  
  30. }  

2.設計一個函數MaxCommonFactor()利用展轉相除法計算兩個正整數的最大公約數spa

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. #include <stdio.h>  
  2. int MaxCommonFactor(int a, int b);  
  3. int main()  
  4. {  
  5.      int a, b, x;  
  6.      printf("Input a,b:");  
  7.      scanf("%d,%d", &a, &b);  
  8.      x =MaxCommonFactor(a,b);  
  9.   
  10.      if (x != -1)  
  11.      {  
  12.           printf("MaxCommonFactor = %d\n", x);  
  13.      }  
  14.      else  
  15.      {  
  16.           printf("Input error!\n");  
  17.      }  
  18.   
  19.      return 0;  
  20. }  
  21.   
  22. //函數功能: 計算兩個正整數的最大公約數,-1表示沒有最大公約數  
  23. int MaxCommonFactor(int a, int b)  
  24. {  
  25.      int r;  
  26.      if (a<=0 || b<=0) return -1; // 保證輸入的參數爲正整數  
  27.   
  28.      do{  
  29.           r=a%b;  
  30.           a = b;  
  31.           b = r;  
  32.      }while (r!=0);  
  33.   
  34.      return  a;  
  35. }  
[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. //函數功能: 計算兩個正整數的最大公約數,遞歸版本  
[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. <pre name="code" class="cpp"><p>int <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"><strong>MaxCommonFactor</strong></span>(int a,int b)</p>{  
  2.     int r;  
  3.     if(a<0 || b<0) return -1;  
  4.     if(b==0)  
  5.         r=a;  
  6.     else  
  7.         r=Gcd(b,a%b);  
  8.     return r;  
  9. }  

另外一種版本:.net

 

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. int divisor(int a,int b)  
  2. {  
  3.     int x = a<b?a:b;  //求a,b的最小數   
  4.     while(x)  
  5.     {  
  6.         if(a%x==0 && b%x==0)  
  7.             break;  
  8.         --x;      
  9.     }  
  10.     return x;  
  11. }  


 


 3.設計一個函數MinCommonMultiple(),計算兩個正整數的 
 最小公倍數

 

 

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. #include <stdio.h>  
  2. int MinCommonMultiple(int a, int b);  
  3. int main()  
  4. {  
  5.     int a, b, x;  
  6.     printf("Input a,b:");  
  7.     scanf("%d,%d", &a, &b);  
  8.     x = MinCommonMultiple(a,b);  
  9.   
  10.     if (x!=-1)  
  11.         printf("MinCommonMultiple = %d\n", x);  
  12.     else  
  13.         printf("Input error!\n");  
  14.   
  15.    return 0;  
  16. }  
  17. //函數功能:計算兩個正整數的最小公倍數,-1表示沒有最小公倍數  
  18. int MinCommonMultiple(int a, int b)  
  19. {  
  20.     int i;  
  21.   
  22.     if (a<=0 || b<=0) return -1;        // 保證輸入的參數爲正整數  
  23.   
  24.     for (i=1; i<b; i++)  
  25.     {  
  26.         if ((i*a)%b==0)   return i * a;  
  27.     }  
  28.   
  29.     return b * a;  
  30. }  

4.設計一個函數,用來判斷一個整數是否爲 素數

 

 

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. #include <math.h>  
  2. #include <stdio.h>  
  3. int IsPrimeNumber(int number);  
  4. int main()  
  5. {  
  6.      int n, ret;  
  7.      printf("Input n:");  
  8.      scanf("%d", &n);  
  9.      ret = IsPrimeNumber(n);  
  10.      if (ret!=0)  
  11.      {  
  12.           printf("%d is a prime number\n", n);  
  13.      }  
  14.      else  
  15.      {  
  16.           printf("%d is not a prime number\n", n);  
  17.      }  
  18.      return 0;  
  19. }  
  20. //函數功能:判斷number是不是素數,函數返回非0值,表示是素數,不然不是素數  
  21. int IsPrimeNumber(int number)  
  22. {  
  23.      int i;  
  24.   
  25.      if (number <= 1) return 0; // 負數、0和1都不是素數  
  26.          for (i=2; i<sqrt(number); i++)  
  27.          {  
  28.               if (number%i==0) // 被整除,不是素數  
  29.                   return 0;  
  30.      }  
  31.      return 1;  
  32. }  


 

來自哈爾濱工業大學MOOC課件

 

5.迴文數

輸出全部不超過n(取n<256)的、其平方具備對稱性質的正整數(也稱爲迴文數)。設計

如:  1*1=1; 2*2=4;3*3=9;11*11=121;1,2,3,11是迴文數。code

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. #include <stdio.h>  
  2. #include <stdbool.h>  
  3. bool isPalindrome(int num);  
  4. bool isPalindrome(int num)  //判斷迴文數字  
  5. {  
  6.     int pal = 0;  
  7.     int origin = num;  
  8.   
  9.     while(num)  
  10.     {  
  11.         pal *= 10;  
  12.         pal += num % 10;  
  13.         num /= 10;  
  14.     }  
  15.   
  16.     return pal == origin;  
  17. }  
  18. int main()  
  19. {  
  20.     int n,i;  
  21.     scanf("%d",&n);  
  22.     for(i=1;i<n;i++)  
  23.     {  
  24.         if(isPalindrome(i*i) && i<256)  //打印迴文數字  
  25.             {  
  26.                 printf("%d\n",i);  
  27.             }  
  28.     }  
  29.   
  30.    return 0;  
  31. }  
相關文章
相關標籤/搜索