1

次の演習を手伝ってくれませんか?(これは宿題ではなく、私が使用している本の演習です。)

「整数は、1 (ただし、数値自体ではない) を含む要素の合計が完全数であると言われます。たとえば、6 = 1 + 2 + 3 であるため、6 は完全数です。書き込み方法 Perfectパラメータ値が完全数であるかどうかを判断します。このメソッドは、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);
            }
        }
    }
}

どのようにしますか?私のコードは修正可能ですか? どのように修正しますか?ありがとう!

行 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);
        }
    }
  }
  }

これで、すべての数字を 1000 まで繰り返し表示し、それが完全かどうか (true または false) を表示します [これは演習で求められたものではありませんが、正しい方向への一歩です (演習では、表示する必要があると述べています)完全数のみ)]。

いずれにせよ、奇妙なのは、完全数ではない 24 番が true であるということです.... http://en.wikipedia.org/wiki/Perfect_numbers#Examples

なぜ24は違うのですか?

どうもありがとう

4

6 に答える 6

18

次の演習を教えてください。

はい。エラーがどこにあるかを示すのではなく、エラーを見つける方法を教えます。さらに良いことに、同じ手法を使用すると、そもそもエラーが発生する可能性が低くなります。

ここで重要なのは、問題を小さな部分に分割して、それぞれの小さな部分を個別にテストできるようにすることです。あなたはすでにこれを始めています!と の 2 つの方法がMainありIsItPerfectます。少なくともあと 3 つのメソッドが必要です。必要なメソッドは次のとおりです。

  • IsDivisor-- 2 つの整数を取り、最初の整数が 2 番目の整数を割る場合に true を返します。
  • GetAllDivisors-- 整数を取り、すべての除数のリストを返します
  • Sum-- 整数のリストを取り、合計を返します

あなたのメソッドIsPerfectは呼び出しGetAllDivisorsSum、合計を元の数値と比較する必要があります。メソッドは などGetAllDivisorsを呼び出す必要がありますIsDivisor

メソッドが多すぎるため、バグを簡単に見つけることができません。正しい結果が得られず、メソッドが 1 つではなく 4 つある場合は、各メソッドを個別にテストして、機能することを確認するか、機能しない場合は修正します。

于 2013-08-29T20:32:46.993 に答える
0

あなたが抱えている24の問題を解決するのに役立ちます. したがって、ここでは 24 が true に反転されます。

Factors of 24 | Total so far
    1                1
    2                3
    3                6
    4                10
    6                16
    8                24     <-- returns true
    12               36     <-- should be false, but flag is never reset
于 2013-08-29T20:31:22.907 に答える
0

最初の for ループは 1 回だけ実行されます。

for (int i = value; i <= value; i++)

たとえば、値 = 6 の場合

for (int i = 6; i <= 6; i++)
于 2013-08-29T20:33:43.270 に答える