zoukankan      html  css  js  c++  java
  • 算法15---数论2---亲密数

    算法16---数论2---亲密数

    如果整数a的因子和等于整数b,整数b的因子和等于整数a,因子包括1但不包括本身,且a不等于b,则称a和b为亲密数对。

      1 /*
      2     题目:亲密数
      3     author taoliu——alex  2016.10
      4 
      5     主要实现两种
      6     1 判断两个数是不是亲密数。
      7     2 找出一定范围内的亲密数。
      8 
      9 */
     10 
     11 
     12 #include <stdio.h>
     13 #include <stdlib.h>
     14 
     15 int  friendnum(int a,int b);
     16 int factor_sum(int n);
     17 void scope_friendnum(int scope);
     18 
     19 //判断两个数是不是亲密数。
     20 
     21 int  friendnum(int a,int b)
     22 {
     23     if (a==b)
     24     {
     25         return -1;
     26     }
     27     int a_sum=0;
     28     int b_sum=0;
     29     a_sum=factor_sum(a);
     30     b_sum=factor_sum(b);
     31     //printf("the factor sum of %d is %d
    ",a,a_sum);
     32     //printf("the factor sum of %d is %d
    ",b,b_sum);
     33     if ((a_sum==b)&&(b_sum==a))
     34     {
     35         return 1;
     36     }
     37     else
     38     {
     39         return 0;
     40     }
     41 
     42 }
     43 
     44 int factor_sum(int n)
     45 {
     46     int sum=0;
     47     for (int i = 1; i < n/2+1; i++)
     48     {
     49         if (n%i==0)
     50         {
     51             sum=sum+i;
     52         }
     53     }
     54     return sum;
     55 }
     56 
     57 // 找出一定范围内的亲密数。
     58 void scope_friendnum(int scope)
     59 {
     60     for (int i = 1; i < scope; i++)
     61     {
     62         for (int j = i; j < scope; j++)
     63         {
     64             int ans=friendnum(i,j);
     65             if(ans==1)
     66                 printf("%d and %d is friendnum
    ",i,j);
     67         }
     68     }
     69 }
     70 
     71 
     72 int main()
     73 {
     74     int judge;
     75     int a,b,scope;
     76     printf("what you want to do :  0 means judge two number is friendnum or not;
    ");
     77     printf(" 1 means you want to find the friendnum in scope
    ");
     78     //fflush(stdin);
     79     scanf("%d",&judge);
     80     if (judge==0)
     81     {
     82         printf("input two number you want to judge!
    ");
     83         //fflush(stdin);
     84         scanf("%d%d",&a,&b);
     85         int ans=friendnum(a, b);
     86         if (ans==1)
     87         {
     88             printf("%d and %d is friendnum
    ",a,b);
     89         }
     90         if (ans==0)
     91         {
     92             printf("%d and %d is not friendnum
    ",a,b);
     93         }
     94         else
     95             printf("%d and %d is the same number ,error!
    ", a,b);
     96     }
     97     if (judge==1)
     98     {
     99         printf("input the scope you want to find
    ");
    100         //fflush(stdin);
    101         scanf("%d",&scope);
    102         scope_friendnum(scope);
    103     }
    104     return 0;
    105 }
  • 相关阅读:
    SQL Server 的事务和锁(一)
    Sql server脏读、更新丢失、不可重复读、幻象读问题及解决方案
    Sql server锁机制
    Windows系统变量列表
    windows运行命令大全
    C# 捕获数据库自定义异常
    sql日期函数
    C# ado.net 操作存储过程(二)
    C# ado.net 操作(一)
    url传参特殊字符问题(+、%、#等)
  • 原文地址:https://www.cnblogs.com/tao-alex/p/5933506.html
Copyright © 2011-2022 走看看