[英]C# perfect numbers exercise
您可以通過以下運動幫助我嗎? (這不是家庭作業,只是我正在使用的書中的練習。)
“如果整數的因素(包括一個數字(但不包括數字本身))加到該數字上,則該整數被稱為完美數字。例如,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);
}
}
}
}
你會怎么做? 我的代碼可以修復嗎? 您將如何解決? 謝謝!
我在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,然后顯示是否正確(正確或錯誤)[這不是練習所要求的,但這是朝正確方向邁出的一步(練習說應顯示只有完美的數字)]。
無論如何,奇怪的是它在第24位說的是正確的,這不是一個完美的數字。... http : //en.wikipedia.org/wiki/Perfect_numbers#Examples
為什么24個不同?
非常感謝
請您幫我做以下練習?
是。 除了向您展示錯誤所在之外,我還將教您如何查找錯誤。 更好的是,相同的技術將降低您首先導致錯誤的機會。
這里的關鍵是將問題分解成小部分,每個小部分都可以獨立測試 。 您已經開始執行此操作! 您有兩種方法: Main
和IsItPerfect
。 您應該至少再有三種方法 。 您應該使用的方法是:
IsDivisor
接受兩個整數,如果第一個除以第二個,則返回true。 GetAllDivisors
取一個整數,返回所有除數的列表 Sum
-取一個整數列表,返回總和 您的方法IsPerfect
應該調用GetAllDivisors
和Sum
並將總和與原始數字進行比較,這就是它要做的全部 。 您的方法GetAllDivisors
應該調用IsDivisor
,依此類推。
您無法輕易找到該錯誤,因為您的方法做得太多。 如果沒有得到正確的結果,並且有四種方法(而不是一種),則可以獨立測試每種方法以確保其有效,如果不能,請進行修復。
對您遇到的24個問題有一些幫助:24實際返回時,因為您實際上在每個其他因素上都檢查它是否完美。 因此,這里有24個變為真:
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
您的第一個for循環將只執行一次。
for (int i = value; i <= value; i++)
例如值= 6
for (int i = 6; i <= 6; i++)
我剛剛完成了同樣的練習,該練習來自Deitel先生寫的一本非常出色的書,名為Visual c#2012。
我開始解決的方法是,我從弄清楚如何計算數字階乘開始,然后慢慢從那里開始。
由於您正在閱讀同一本書,因此我建議您不要使用本章練習中未涵蓋的內容,例如您使用過的列表集合,因為這會使練習不必要地困難。 否定了作者提出的學習方法。
這是我的代碼,希望對您有所幫助。
class Program
{
static int factorTotal = 1;
static void Main(string[] args)
{
int count = 1;
while (count <= 10000)
{
bool isPerfect = IsPerfectNumber(count);
if (isPerfect && (factorTotal >1))
{
Console.WriteLine("Is Perfect: {0}", factorTotal);
}
factorTotal = 1;
count++;
}
} // end main
static bool IsPerfectNumber(int n)
{
int temp;
int counter = 2;
bool IsPerfect = false;
while (counter <= (n - 1))
{
temp = n % counter;
if (temp == 0) // if true than factor found
{
factorTotal = factorTotal + counter;
}
counter++;
}
if ((factorTotal) == n)
IsPerfect = true;
else
IsPerfect = false;
return IsPerfect;
}
}//end class
在控制台應用程序的Main方法下,復制並粘貼以下代碼。 我在代碼末尾解釋了幾件事...
================================================== ===================
{
Console.WriteLine("perfect numbers/n");
Console.Write("Enter upper limit: ");
int iUpperLimit = int.Parse(Console.ReadLine());
string sNumbers = "";
List<int> lstFactor = new List<int>();
for(int i = 1;i<=iUpperLimit;i++)
{
for(int k = 1;k<i;k++)
{
if (i % k == 0)
{
lstFactor.Add(k); //this collect all factors
}
if (k == i-1)
{
if (lstFactor.Sum() == i) //explain1
{
sNumbers += " " + i;
lstFactor.Clear(); //explain2
break;
}
else
{
lstFactor.Clear(); //explain2
}
}
}
}
Console.WriteLine("\nperfect numbers are: " + sNumbers);
Console.ReadKey();
}
}
================================================== =====================請注意, i
是我們測試的數字, k
是其因子。
說明1 =>我們將收集的所有因子相加並檢查它們是否等於i
(我們只是檢查i
是否為完美數)
describe2 =>我們必須先清除列表,然后才能檢查下一個數字i
是否為理想數字,以使前一個數字的因數不干擾當前數字的因數。
int start=1;
int end=50;
for(int a=end ; a > start ;a--)
{
int b=1;
int c=0;
bool x=false;
for(int i=1 ; i < a ;i++)
{
b=a/i;
if(b*i==a)
{
c+=i;
}
if(c==a & i==a/2)
{
x=true;
}
}
if(x==true)
Console.Write("{0} is : {1}",a,x);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.