找到一个完美的数字。 [英] Finding a perfect number.

查看:55
本文介绍了找到一个完美的数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试着编写一个程序来判断一个数字是否完美




这是我的逻辑:


1)读入数字

2)拆分(数字 - 1)

3)将所有拆分数字放入数组

4)弄清楚原始数字是否可以被数组中任何一个

数字整除。

5)只加上数字在数组中平均分配原始数字。

6)将这些数字与原始数字进行比较,得到是或

没有回答。


我开始了,这就是它的出现:


int main(void)


{

int number,i,split,total;


printf(" Enter number:");

scanf("%d",& number);

for(i = 0; i< = number; i ++)

{

int array [number];


split = number - 1;

number = split;

array [number] = split;


printf("%d",array [number]); //只是检查数组

是否有效。

}

}


显然,这甚至不接近解决方案,但至少我得到了一些东西。当然,程序会跳过''1'并打印出

,剩下的只有5位数。它不会再分裂这个数字。


无论如何,任何提示都会很棒。

Trying to write a program that will figure out if a number is perfect
or not.

Here is my logic:

1) Read in the number
2) Split it up (number - 1)
3) Put all the split up numbers into an array
4) Figure out if the original number is evenly divisible by any of the
numbers in the array.
5) Add up only the numbers that devide the original number evenly in
the array.
6) Compare the total of these to the original number and give a yes or
no answer.

I started it, and here is how its coming out:

int main (void)

{
int number, i, split, total;

printf("Enter number: ");
scanf("%d", &number);
for (i =0 ; i <= number; i++)
{
int array[number];

split = number - 1;
number = split;
array[number] = split;

printf("%d", array[number]); //just to check if the array
worked.
}
}

Obviously, this isn''t even close to the solution, but i am getting
something, at least. Ofcourse, the program skips ''1'' and prints out
the rest up to only 5 digits. It won''t ''split'' the number any more.

Anyhow, any hints would be great.

推荐答案

在文章< mn *********************** @ mail.com>,

gk245< ;到***** @ mail.com>写道:
In article <mn***********************@mail.com>,
gk245 <to*****@mail.com> wrote:
试着编写一个程序来判断一个数字是否完美



这真的是一个算法问题,而不是关于C的问题。

算法问题在comp.programming中更具局部性和

其他类似的新闻组。

这是我的逻辑:
1)读入数字
2)拆分(数字 - 1)
3)把所有拆分的数字放到一个数组中


我不明白你的意思是拆分,而我不是

了解如何(数字-1)(第2步)将给你

你希望用于在步骤3中填充数组的几个数字。


4)弄清楚原始数字是否可以被数组中的任何数字整除。
5)只添加在数组中均匀分配原始数字的数字。
6)将这些总数与原始数字进行比较,并给出是或否答案。


[OT for comp.lang.C]


你可以做得非常好。


一个完美的数字总是形式(2 ** N-1)*(2 **(N-1))其中

我在这里使用**来表示取幂。因此,您可以将试用号码的位数向右移,直到您发现最后一位

不再为零,计算出的班次数量。

如果剩下的东西没有与你移动的

完全相同的位数,如果这些位不是全1',那么数字是不是完美的b $ b。如果满足这个条件,那么你可以测试移位的

数来确定它是否是素数:如果是,那么原来的

就是一个完美的数字。 />

例如,

6 =(2 ** 2-1)*(2 **(2-1))= 3 * 2

28 =(2 ** 3-1)*(2 **(3-1))= 7 * 4


我开始了它,这是它的方式出来:
int main(void)
{
int number,i,split,total;
printf(" Enter number:");
scanf ("%d",& number);
for(i = 0; i< = number; i ++)
{
int array [number];


该语法在C99中有效,但在C89中无效。 C89不允许

声明具有动态确定长度的数组。

(最接近的C89等效是使用malloc()或calloc()来动态地

分配必要的内存。)

split = number - 1;
number = split;
array [number] = split;


这些步骤都没有涉及你的变量i,因此你可能会在循环之外而不是循环内完成它们。 />
(这需要移动数组的声明。)


我担心你声明你的数组有数字

元素在它,但你循环(数字+ 1)次:它暗示

无论你在想什么可能有一个一个一个错误。


printf("%d",array [number]); //只是为了检查数组是否有效。
}
}
显然,这甚至不接近解决方案,但我至少得到了一些东西。当然,程序会跳过''1'并打印出剩下的最多只有5位数。它不会再分裂这个数字了。
Trying to write a program that will figure out if a number is perfect
or not.
That''s really an algorithm question, not a question about C.
algorithm questions are more topical in comp.programming and
other similar newsgroups.
Here is my logic: 1) Read in the number
2) Split it up (number - 1)
3) Put all the split up numbers into an array
I don''t understand what you mean by "split it up", and I do not
understand how (number - 1) (step 2) is going to give you the
several numbers you expect to use to populate the array in step 3.

4) Figure out if the original number is evenly divisible by any of the
numbers in the array.
5) Add up only the numbers that devide the original number evenly in
the array.
6) Compare the total of these to the original number and give a yes or
no answer.
[OT for comp.lang.C]

You can do noticably better than that.

A perfect number is always of the form (2**N-1)*(2**(N-1)) where
I am here using ** to denote exponentiation. So you can shift bits
of your trial number to the right until you find that the bottom bit
is no longer a zero, counting the number of shifts as you go.
If what is left over does not have exactly the same number of bits as
you shifted, and if those bits are not all 1''s, then the number is not
perfect. If that condition is satisfied, then you can test the shifted
number to determine whether it is prime: if it is, then the original
is a perfect number.

For example,
6 = (2**2-1)*(2**(2-1)) = 3*2
28 = (2**3-1)*(2**(3-1)) = 7*4

I started it, and here is how its coming out: int main (void) {
int number, i, split, total;
printf("Enter number: ");
scanf("%d", &number);
for (i =0 ; i <= number; i++)
{
int array[number];
That syntax is valid in C99 but not in C89. C89 does not allow
declaration of arrays with a length which is dynamically determined.
(The closest C89 equivilent is to use malloc() or calloc() to dynamically
allocate the necessary memory.)

split = number - 1;
number = split;
array[number] = split;
None of these steps have involved your variable i, so you might
as well have done them outside of the loop instead of inside the loop.
(That would require moving the declaration of array.)

I am concerned that you are declaring your array to have number
elements in it, but you are looping (number+1) times: it hints that
whatever you are thinking of probably has an off-by-one error.

printf("%d", array[number]); //just to check if the array
worked. }
} Obviously, this isn''t even close to the solution, but i am getting
something, at least. Ofcourse, the program skips ''1'' and prints out
the rest up to only 5 digits. It won''t ''split'' the number any more.




我不知道你的意思是拆分在这个问题上,但是5位数

提供了一个可能解释的线索。


您的变量号被声明为int,并且你的scanf()格式是

"%d"这是int的适当格式。但是你的实现支持的signed int的最大大小是什么?如果

你的有符号int恰好是16位长,那么符号需要1位{有效}

和值为15位,这将给出最大值

可能是32767(32768在非常模糊的实现中。)

和32767恰好是5位数。


您可以尝试更改为long类型的变量并使用%ld;格式

为scanf()。但是请注意,实现通常不会允许大型数组以C99方式声明大型数组,或者b $ b,所以如果你的数据远大于32768,你可能会发现该程序因内存不足而崩溃

。为此做出的调整将是

malloc()所需的内存(在完整性检查后,这个数字的大小为
)。

-

有些想法如此错误,只有非常聪明的人才能相信他们。 - George Orwell



I don''t know what you mean by "split" in this matter, but the "5 digits"
offers a clue as to a possible explanation.

Your variable number is declared as an int, and your scanf() format is
"%d" which is the appropriate format for an int. But what is the
maximum size of signed int that your implementation supports? If
your signed int happens to be 16 bits long, then 1 bit is {effectively}
needed for the sign and 15 bits for the value, which would give a maximum
possible int of 32767 (32768 in very obscure implementations.)
And 32767 happens to be exactly 5 digits long.

You could try changing to variables of type long and using a "%ld" format
for the scanf(). Be warned, though, that implementations often do not
allow large arrays to be declared in the C99 way your are proceeding,
so if you go much larger than 32768 you might find the program crashing
for lack of memory. The adjustment to make for that would be to
malloc() the necessary memory instead (after sanity-checking the
size of the number!).
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell




gk245写道:

gk245 wrote:
试着编写一个程序来判断一个数字是多少这是我的逻辑:

1)读入数字
2)拆分(数字 - 1)
3)将所有拆分的数字放入一个数组中
4)弄清楚原始数字是否可以被数组中的任何数字整除。
5)添加只有在数组中均匀分配原始数字的数字。
6)将这些数字与原始数字进行比较并给出是或否答案。
<我开始了,这就是它的出现:

int main(void)

{int / int int,i,split,total;

printf("输入数字:");
scanf("%d",& number);

for(i = 0; i< = number; i ++)
{int int array [number];

分裂= number - 1;
number = split;
array [number] = split;

printf("%d",array [number]); //只是为了检查数组是否有效。

}

}

显然,这甚至都不接近解决方案,但我至少得到了一些东西。当然,程序会跳过''1'并打印出剩下的最多只有5位数。它不再分裂这个数字。

无论如何,任何提示都会很棒。
Trying to write a program that will figure out if a number is perfect
or not.

Here is my logic:

1) Read in the number
2) Split it up (number - 1)
3) Put all the split up numbers into an array
4) Figure out if the original number is evenly divisible by any of the
numbers in the array.
5) Add up only the numbers that devide the original number evenly in
the array.
6) Compare the total of these to the original number and give a yes or
no answer.

I started it, and here is how its coming out:

int main (void)

{
int number, i, split, total;

printf("Enter number: ");
scanf("%d", &number);
for (i =0 ; i <= number; i++)
{
int array[number];

split = number - 1;
number = split;
array[number] = split;

printf("%d", array[number]); //just to check if the array
worked.
}
}

Obviously, this isn''t even close to the solution, but i am getting
something, at least. Ofcourse, the program skips ''1'' and prints out
the rest up to only 5 digits. It won''t ''split'' the number any more.

Anyhow, any hints would be great.




一些观察:

1)完美数字的含义并不明显。我没有兴趣查询它...

2)在你的循环中你增加我和
$ b是很奇怪的$ b递减数字,以便循环在它们满足一半的时候终止。
方式。我不确定你是否打算这样做。



A few observations:
1) it''s not obvious what you mean by a "perfect number". I have no
interest in looking it up...
2) It''s very odd that in your loop you are incrementing i and
decrementing number, so that the loop terminates when they meet half
way. I''m not sure that you intended that to happen.


Bill Pursell于2006年4月28日解释:
Bill Pursell explained on 4/28/2006 :
gk245写道:
试着编写一个程序来判断一个数字是否完美

这是我的逻辑:
< 1)读入数字
2)将其拆分(数字 - 1)
3)将所有拆分的数字放入数组中
4)弄清楚原始数字是否正确可以被数组中的任何数字整除。
5)只添加在数组中均匀分配原始数字的数字。
6)比较总数这些是原来的号码,给出了是或否答案。

我开始了,这就是它的出现:

int main(void )

{int,i,split,total;

printf("输入数字:");
scanf(" %d",& number);

for(i = 0; i < =数字; i ++)
{int array [number];

split = number - 1;
number = split;
array [number] = split;

printf("%d",array [number]); //只是为了检查数组是否有效。

}

}

显然,这甚至都不接近解决方案,但我至少得到了一些东西。当然,程序会跳过''1'并打印出剩下的最多只有5位数。它不再分裂了这个数字。

无论如何,任何提示都会很棒。
Trying to write a program that will figure out if a number is perfect
or not.

Here is my logic:

1) Read in the number
2) Split it up (number - 1)
3) Put all the split up numbers into an array
4) Figure out if the original number is evenly divisible by any of the
numbers in the array.
5) Add up only the numbers that devide the original number evenly in
the array.
6) Compare the total of these to the original number and give a yes or
no answer.

I started it, and here is how its coming out:

int main (void)

{
int number, i, split, total;

printf("Enter number: ");
scanf("%d", &number);
for (i =0 ; i <= number; i++)
{
int array[number];

split = number - 1;
number = split;
array[number] = split;

printf("%d", array[number]); //just to check if the array
worked.
}
}

Obviously, this isn''t even close to the solution, but i am getting
something, at least. Ofcourse, the program skips ''1'' and prints out
the rest up to only 5 digits. It won''t ''split'' the number any more.

Anyhow, any hints would be great.



一些观察:
1)用完美数字表示你的意思并不明显。我没有兴趣查找它...
2)在你的循环中你递增i和
递减数字是非常奇怪的,所以当它们循环时循环终止遇到一半的方式。我不确定你是否打算这样做。



A few observations:
1) it''s not obvious what you mean by a "perfect number". I have no
interest in looking it up...
2) It''s very odd that in your loop you are incrementing i and
decrementing number, so that the loop terminates when they meet half
way. I''m not sure that you intended that to happen.




对不起,一个完美的数字是其设计者的数字(除了
$ b) $ b数字本身)加起来原始数字。所以,举例来说,

6. 6可以拆分。到6,5,4,3,2,1。设计师(其中6美元,即:剩余部分为0)均为6个,分别为3分,2分和1分(不算数为
6本身)。 3,2和1加起来为6.所以,6是​​一个完美的数字。



Sorry, a perfect number is a number which in which its devisors (except
the number itself) add up to the original number. So, for example take
6. 6 can be "split-up" to 6,5,4,3,2,1. The devisors (which evenly
devide six,i.e: remainder is 0) here for 6 are 3, 2 and 1 (don''t count
6 itself). 3, 2, and 1 add up to 6. So, 6 is a perfect number.


这篇关于找到一个完美的数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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