C#完美数字练习

use*_*261 1 c# math perfect-numbers

你可以帮我做下面的运动吗?(这不是家庭作业,只是我正在使用的书中的练习.)

"一个整数被认为是一个完美的数字,如果它的因素,包括一个(但不是数字本身),总和到数字.例如,6是一个完美的数字,因为6 = 1 + 2 + 3.写方法完美确定参数值是否为完美数字.在确定并显示2到1000之间的所有完美数字的应用程序中使用此方法.显示每个完美数字的因子以确认数字确实完美."

所以这就是我到目前为止所得到的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Perfect_Numbers2
{
    class Program
    {
        static bool IsItPerfect(int value)
        {
            int x = 0;

            int counter = 0;

            bool IsPerfect = false;

            List<int> myList = new List<int>();

            for (int i = value; i <= value; i++)
            {
                for (int j = 1; j < value; j++)
                {
                    // if the remainder of i divided by j is zero, then j is a factor of i
                    if (i%j == 0) {
                        myList[counter] = j; //add j to the list
                        counter++;
                    }
                    for (int k = 0; k < counter; k++)
                    {
                        // add all the numbers in the list together, then
                        x = myList[k] + myList[k + 1]; 
                    }
                    // test if the sum of the factors equals the number itself (in which case it is a perfect number)
                    if (x == i) {
                        IsPerfect = true;
                    }
                }
                Console.WriteLine(i);
            }
            return IsPerfect;
        }
        static void Main(string[] args)
        {
            bool IsItAPerfectNum = false;

            for (int i = 2; i < 1001; i++)
            {
                IsItAPerfectNum = IsItPerfect(i);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你会怎么做?我的代码可修复吗?你会怎么解决它?谢谢!

我在线上遇到错误myList [counter] = j; (索引超出范围)除了它没有显示完美的数字,就像它应该....

编辑=我做了一些改变;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Perfect_Numbers2
{
class Program
{
    static bool IsItPerfect(int value)
    {
        int x = 0;

        int counter = 0;

        bool IsPerfect = false;

        List<int> myList = new List<int>();

        for (int i = value; i <= value; i++)
        {
            for (int j = 1; j < i; j++)
            {
                if (i%j == 0)  // if the remainder of i divided by j is zero, then j is           a factor of i
                {
                    myList.Add(j); //add j to the list


                }
                x = myList.Sum();
                if (x == i)                        // test if the sum of the factors       equals the number itself (in which case it is a perfect number)
                {
                    IsPerfect = true;
                }

        }
            Console.WriteLine(i);
        }
        return IsPerfect;
    }
    static void Main(string[] args)
    {
        bool IsItAPerfectNum = false;

        for (int i = 2; i < 1001; i++)
        {
            IsItAPerfectNum = IsItPerfect(i);
            Console.WriteLine(IsItAPerfectNum);
            Console.ReadKey(true);
        }
    }
  }
  }
Run Code Online (Sandbox Code Playgroud)

现在我可以循环遍历所有数字直到1000并显示它是否完美(真或假)[这不是练习所要求的,但它是向正确方向迈出的一步(练习说它应该显示)只有完美的数字)].

在任何情况下,奇怪的是它在第24位表示正确,这不是一个完美的数字.... http://en.wikipedia.org/wiki/Perfect_numbers#Examples

为什么24不同?

非常感谢

Eri*_*ert 18

你可以帮我做下面的练习吗?

是.我会告诉你如何找到你的错误,而不是告诉你错误的位置.更好的是,相同的技术将首先降低导致错误的可能性.

这里的关键是将问题分解成小部件,每个小部件都可以独立测试.你已经开始这样做了!你有两种方法:MainIsItPerfect.你应该至少有三种方法.你应该拥有的方法是:

  • IsDivisor - 取两个整数,如果第一个除以第二个,则返回true.
  • GetAllDivisors - 取一个整数,返回所有除数的列表
  • Sum - 获取整数列表,返回总和

你的方法IsPerfect应该是呼吁GetAllDivisorsSum其和比较原来的号码,而这一切应该做的事情.你的方法GetAllDivisors应该是调用IsDivisor,等等.

您无法轻易找到错误,因为您的方法做得太多了.如果你没有得到正确的结果并且你有四种方法而不是一种方法,那么你可以独立测试每种方法以确保它有效,或者如果没有则可以修复它.

  • @Rotem:因为这是一个初学者,正在做这项工作作为练习.编写自己的简单辅助方法实现可以构建字符,并且它鼓励您将这些辅助方法视为真正的人们必须设计,实现和发布的非魔法代码片段. (7认同)