输出所有小于1000的正整数的C#程序? [英] C# Program that outputs all perfect numbers less than 1000 ?

查看:153
本文介绍了输出所有小于1000的正整数的C#程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

C#程序输出所有小于1000的正整数.

正整数``n''的适当除数是那些不包括``n''本身的整数,它们精确地除以``n''.
例如:12的适当除数是1,2,3,4和6.

完美的数字是正整数,它等于其适当除数的和.
例如:6是一个理想数,因为6 = 1 + 2 + 3.

任何人都可以帮助解决这个问题吗?

在此先感谢

S.Naveen ...

Hi all,

C# Program that outputs all perfect numbers less than 1000.

The Proper divisors of a positive integer ''n'' are those integers,excluding ''n'' itself,that divide ''n'' exactly.
For Example : Proper divisor of 12 are 1,2,3,4 and 6.

A perfect number is the Positive Integer that is equal to Sum of its proper divisors.
For Example : 6 is a perfect number because 6 = 1 + 2 + 3 .

Can any one help on this issue ?

Thanks in advance

S.Naveen...

推荐答案

出于以下几个原因,我要咬一口…….

大约20年前,我不得不在Pascal进行此项目,我想知道它将如何转换为.NET世界.我喜欢做这样的事情,因为它迫使我学习新事物.一个很好的例子是,我从不知道您能够汇总一个列表.相当整洁的.NET功能.在过去"的日子里,我们将不得不创建一个数组,然后遍历遍历它的每个元素添加……遍历…….两种方式……在雪中.

我要考虑这是一次学习经历,哎呀,我今天学到了一些东西.因此,我的建议是:始终将您的问题并分解为最小的组件.是的,考虑一下大问题,但要分解.

想想看,其中最小的组成部分是什么?您知道您将必须做两个基本的事情:

#1.检查数字是否均分.

#2.检查所有除数是否等于您要测试的数.

如果您问我,听起来您有两种离散的方法.

因此,让我们看一下数字是否均分:
I''m going to bite on this one for a couple of reasons....

I had to do this project about 20 years ago in Pascal and I was wondering how it would translate to the .NET world. I like doing stuff like this because it forces me to learn new things. And one good example of this is that I never knew that you were able to sum a List. Pretty neat .NET feature. In the "old" days we would have had to create an array and then loop through it adding each element as we went... uphill.. both ways... in the snow.

I''m going to consider this a learning experience, heck, I learned something today. So here''s my advice: Always take your problem and split it up into the smallest components possible. Yes, think about the big problem, but break it down.

Think about it, what is the smallest component of this? You know you are going to have to do two basic things:

#1. Check to see if a number divides evenly.

#2. Check to see if all of the divisors equal the number that you are testing.

Sounds like you have two discrete methods if you ask me.

So let''s see if the number divides evenly:
public bool TestForEvenlyDivisable(int iNumerator, int iDenominator)
{
    if ((iNumerator % iDenominator) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}



您看到的一点%是mod运算符.您需要使用此功能来测试数字是否可以被整除.

现在让我们看另一个问题.您必须检查是否成功划分的数字实际上加起来就是您要测试的数字.在过去,我会传递一个数组,或者将其添加到一个整数中.现在,我们有了泛型和集合.进步很大吗?



The little % you see is the mod operator. You need this to test if a number is evenly dividable.

Now let''s look at the other problem. You have to check to see if the numbers that divided successfully actually add up to the number you are testing. In the old days, I would have passed an array, or added onto an integer. Now, we have generics and collections. It''s progress grand?

public bool TestForSumOfDivisors(int iNumberToTest, List<int> Divisors)
{

    if (iNumberToTest == Divisors.Sum())
    {
        return true;
    }
    else
    {
        return false;
    }
}



很少的Divisors.Sum()可以节省大量时间.

那么现在我们如何将它们放在一起?

好吧,我们将要处理FOR循环.特别是循环内部的循环.您需要从2循环(因为我们知道一个完美的数字不能为1)一直到N(在您的情况下为1000).在该循环中,我们必须从1一直循环到我们目前正在测试的号码.

抱歉,没有捷径.这是老派巨像风格暴力袭击.

在第一个循环中,您将得到要测试的号码.在第二个循环中,检查每个数字是否均分.如果可以,请将其添加到列表中.完成该测试后,请进行测试以查看除数是否等于您当前正在测试的数字.



That little Divisors.Sum() saves a lot of time.

So now how do we put it all together?

Well, we are going to be dealing with FOR loops. And specifically a loop inside of a loop. You need to loop from 2 (because we know a perfect number can''t be 1) all the way to N, which in your case would be 1000. Inside that loop, we have to loop from 1 all the way up to the number that we are currently testing.

Sorry, there is no shortcut. This is old school Colossus-style brute force assault.

In the first loop, you get the number that you are testing. In the second loop, you check to see if each number is evenly dividable. If it is, great, add it to the List. When you are finished with that test, then test to see if the divisors sum to the number you are currently testing.

for (int i = 2; i <= nThNumber; i++)
{
    List<int> ListOfDivisableNumbers = new List<int>();
    for (int j = 1; j < i; j++)
    {
        if (TestForEvenlyDivisable(i, j) == true)
        {
            ListOfDivisableNumbers.Add(j);
        }
    }
    if (TestForSumOfDivisors(i, ListOfDivisableNumbers) == true)
    {
        m_sResults += i.ToString() + "...";
    }
}



现在,我为您提供了执行此操作所需的所有工具.您可以弄清楚如何将所有这些东西放到一个类中并将其连接起来以便工作.

顺便说一句,有一种方法可以使该程序的运行速度快两倍.我将把编码工作留给您,但这是一个提示:您不需要测试大于测试用例一半的数字.

祝你好运,我的朋友.如果坚持下去,就会有一份有意义的职业.

Ryan McBeth



Now I''ve given you all of the tools you need to do this. You can figure out the rest on how to throw all of this stuff into a class and hook it up so it works.

By the way, there is a way to make this program run twice as fast. I''ll leave coding that up to you, but here is a hint: you don''t need to test numbers that are greater than half of the test case.

Good luck my friend. There is a rewarding career out there if you buckle down and stick with it.

Ryan McBeth


我看到您正忙于用功课向每个论坛发送垃圾邮件.更有效地利用您的时间只会使用google

http://answers.yahoo.com/question/index?qid=20081028061230AAKWF6P [ ^ ]

GOOGLE GOOGLE L @@ K L @@ K GOOGLE GOOGLE

http://www.google.co.uk/i [
I see you''re busy spamming every forum with your homework. A more efficient use of your time would be just to use google

http://answers.yahoo.com/question/index?qid=20081028061230AAKWF6P[^]

GOOGLE GOOGLE L@@K L@@K GOOGLE GOOGLE

http://www.google.co.uk/i[^]

You type things into it and click search and it will find them for you. It''s amazing.


Console.WriteLine(6);
Console.WriteLine(28);
Console.WriteLine(496);


瞧!


这篇关于输出所有小于1000的正整数的C#程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆