eg: 28=1+2+4+7+14

即一个数等于它所有公约数(除本身)之和

using System;
using System.Collections.Generic;
using System.Text;

namespace ExIsWanShu
{
    class IsWanShu
    {
        public bool IsWanShu(int Num)
        {
            int s=0;
            for (int i=1;i<Num;i++)
            {
                if((Num%i)==0)
                    s=s+i;
            }
            if (Num == s)
            {
                string str = s.ToString() + "=";
                for (int i = 1; i < s; i++)
                {
                    if ((s % i) == 0)
                    {
                        str = str +"+"+ i.ToString();
                    }
                }
                Console.Write(str+",");
                return true;
            }
            return false;
        }
        static void Main(string[] args)
        {
            IsWanShu e = new IsWanShu();
            int Num = Convert.ToInt32(Console.ReadLine());
            bool b = e.IsWanShu(Num);
            if (b)
            {
                Console.WriteLine("{0}是完数", Num);
            }
            else
            {
                Console.WriteLine("{0}不是完数", Num);
            }
        }
    }
}

相关文章:

  • 2022-12-23
  • 2021-08-01
  • 2022-01-10
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
猜你喜欢
  • 2022-02-08
  • 2021-06-30
  • 2021-10-31
  • 2022-01-17
  • 2021-11-26
相关资源
相似解决方案