zoukankan      html  css  js  c++  java
  • 12--c完数/最大公约数/最小公倍数/素数/回文数

     

    完数/最大公约数/最小公倍数/素数/回文数

     分类:
     
     

    1.一个正整数的因子是所有可以整除它的正整数。而一个数如果恰好等于除它本身外的因子之和,这个数就称为完数。例如6=1+2+3(6的因子是1,2,3)。

    [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 ", m);  
    12.     else  
    13.         printf("%d is not a perfect number ", 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()利用辗转相除法计算两个正整数的最大公约数

    [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 ", x);  
    13.      }  
    14.      else  
    15.      {  
    16.           printf("Input error! ");  
    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. }  

    另一种版本:

    [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 ", x);  
    12.     else  
    13.         printf("Input error! ");  
    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);  
    13.      }  
    14.      else  
    15.      {  
    16.           printf("%d is not a prime number ", 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是回文数。

    [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 ",i);  
    27.             }  
    28.     }  
    29.   
    30.    return 0;  
    31. }  
  • 相关阅读:
    idea 配置git
    mybatisgenerator自动生成Mapper.dao.entity
    Eclipse中设置作者日期等Java注释模板
    二分图匹配KM算法
    网络流最小费用最大流
    图论tarjan
    STL的一些基本操作
    图论拓扑排序
    字符串的一些基本操作
    网络流最大流
  • 原文地址:https://www.cnblogs.com/wohenben/p/5425747.html
Copyright © 2011-2022 走看看