Console算法—完数(没完成)

ylbtech-Arithmetic:Console-算法一完数
 
1.A,Demo(案例)

 一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
   找出1000以内的所有完数。

1.B,Solution(解决方案)
C语言程序
#include "stdio.h"
#include "conio.h"
main()
{
  static int k[10];
  int i,j,n,s;
  for(j=2;j<1000;j++)
  {
    n=-1;
    s=j;
    for(i=1;i<j;i++)
    {
      if((j%i)==0)
      {
        n++;
        s=s-i;
        k[n]=i;
      }
    }
    if(s==0)
    {
      printf("%d is a wanshu",j);
      for(i=0;i<n;i++)
      printf("%d,",k[i]);
      printf("%d\n",k[n]);
    }
  }
  getch();
}
Console
View Code
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] k = new int[10];
            int i, j, n, s;
            for(j=2;j<100;j++)
            {
                n = -1;
                s = j;
                for (i = 1; i < j; i++)
                {
                    if (j % i == 0)
                    {
                        n++;
                        s = s - i;
                        k[n]=i;
                    }
                    if (s == 0)
                    {
                        Console.WriteLine("{0} is a wanshu",j);
                        for (i = 0; i < n; i++)
                        {
                            Console.WriteLine(k[i]);
                            Console.WriteLine(k[n]);
                        }
                    }
                }
            }
        }
    }
}
1.C,Execution Result(运行结果)
 
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/ylbtech/p/3062768.html