C#实现1000以内的完数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _04完数
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "";
            //i用来判断这个数
            for (int i = 2; i < 1000; i++)
            {
                //记录每个数的因数之和
                int res = 0;
                //判断i的所有的因数,并求其之和
                //string str = "1";
                //j用来记录约数
                for (int j = 1; j < i; j++)
                {
                    int yu = i % j;
                    if (yu==0)
                    {
                        res = res + j;
                    }
                }
                if (res==i)
                {
                    label1.Text = label1.Text + i.ToString() + ",";
                }
            }
            
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43551373/article/details/85603070