## 382 - Perfection

Moderator: Board moderators

turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:
I think you've overdone this problem by implementing a linked-list ... but, probably you have good reasons ...

Code: Select all

``for(p=head;p->next;p=p->next)``
Don't you think it's should be:

Code: Select all

``for(p=head;p;p=p->next)``
And the output specification explicitly says 2 blank-spaces after n ...

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).

Zhao Le
Learning poster
Posts: 80
Joined: Mon May 05, 2003 4:09 am
Location: Shanghai,China

### Thank you

Thank you i got ac.
Next time i must be more careful.

Marcin
New poster
Posts: 2
Joined: Sat Jul 05, 2003 12:45 am
Location: Gdynia

### 382-Perfection -why WA? Pascal

Please tell me what's wrong with my code:
[pascal]program Perfection;
var
a,i:longint;
s:longint;
begin
writeln('PERFECTION OUTPUT');
while a<>0 do begin s:=0;
if a=1 then writeln(' 1 PERFECT') else begin
for i:=1 to ((a mod 2) + (a div 2)) do begin
if a mod i = 0 then s:=s+i;
if a<s then break;
end;

write(a:5);
if a=s then writeln(' PERFECT')else
if a<s then writeln(' ABUNDANT')else
writeln(' DEFICIENT');
end;
end;
writeln('END OF OUTPUT');
end.[/pascal]

hank
Experienced poster
Posts: 146
Joined: Mon Feb 04, 2002 2:00 am
Location: VCORE.
be careful "1 is DEFICIENT"

Marcin
New poster
Posts: 2
Joined: Sat Jul 05, 2003 12:45 am
Location: Gdynia
Thanks Hank.
I changed it and got AC

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

### 382 WA why???

[cpp]
#include "stdio.h"
int main(int argc, char* argv[])
{
int x,perfect,loop,first;
first=0;
while(1)
{
scanf("%d",&x);
if(first==0)
{
printf("PERFECTION OUTPUT\n");
first=1;
}
perfect=0;
if(x==0) break;
if(x==1)
{
printf("%5d PERFECT\n",x);
continue;
}
for(loop=1;loop<=x/2;loop++)
{
if(x%loop==0) perfect+=loop;
}
if(perfect==x) printf("%5d PERFECT\n",x);
if(perfect>x) printf("%5d ABUNDANT\n",x);
if(perfect<x) printf("%5d DEFICIENT\n",x);
}
printf("END OF OUTPUT");
return 0;
}[/cpp]
"Learning without thought is useless；thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

pavelph
Learning poster
Posts: 57
Joined: Wed Dec 10, 2003 7:32 pm
Location: Russia, Saint-Petersburg
I don't know C but I know that very often mistake for this problem that 1 is DEFICIENT. Check it please.
Best regards.

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China
1 is perfect,isn't it?
And what i am using is c++
"Learning without thought is useless；thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

pavelph
Learning poster
Posts: 57
Joined: Wed Dec 10, 2003 7:32 pm
Location: Russia, Saint-Petersburg
No, my AC code contain line
[pascal] if (n=1) then writeln(n:5, ' DEFICIENT') [/pascal]
Judge suppose that you must to count only divisors that <n. And for 1 the sum of divisors will be 0. Ofcourse it DEFICIENT.

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China
Hey,thanks indeed :P
"Learning without thought is useless；thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

New poster
Posts: 10
Joined: Mon Jul 19, 2004 6:35 pm

### 382 - WA

This program does everything well when I run it in my computer but wen I submit it it gives a wrong answer. Does anyone can explain me why?

[c]
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

int check(int bah);
void organizer();

char list[300];
int numbers[100], a=0, g;

int main(int argc, char* argv[])
{

for(g=0; g<100; g++)
numbers[g]=0;

gets(list);

printf("PERFECTION OUTPUT\n");

organizer();

while(numbers[a])
{
switch(check(numbers[a]))
{
case 0: printf("%5d %s\n", numbers[a], "DEFICIENT");break;

case 1: printf("%5d %s\n", numbers[a], "PERFECT");break;

case 2: printf("%5d %s\n", numbers[a], "ABUNDANT");break;
}
a++;
}

printf("END OF OUTPUT");

return 0;
}

void organizer()
{
char *p;
int i;

p=strtok(list, " ");
numbers[0]=atoi(p);

for(i=1; p!=NULL;i++)
{
p=strtok('\0', " ");

if(p==NULL)
break;

numbers=atoi(p);
}
}

int check(int bah)
{
int soma=0, j;

if(bah==1)
return 1;

for(j=1; j<bah; j++)
{
if(bah%j==0)
soma+=j;
}

if(soma>bah)
return 2;

if (soma==bah)
return 1;

else return 0;
}

[/c]

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

### can be shorter

Here are few modification that I made to your code and it got Ac.

--> First of all , you don't have to use strtok cos you know it terminates with a zero.

--> 1 is not a perfect number

--> You don't have to store the numbers in an array before processing..
.. the best is to read a number and process it immediately.

Here is a slight modification of your code which got AC. Note the changes

[c]
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

int check(int bah);
int a;

int main( )
{

printf("PERFECTION OUTPUT\n");

while( scanf("%d", &a ) )
{
if( a == 0 )
break;
switch( check(a) )
{
case 0: printf("%5d %s\n", a, "DEFICIENT");break;

case 1: printf("%5d %s\n", a, "PERFECT");break;

case 2: printf("%5d %s\n", a, "ABUNDANT");break;
}

}

printf("END OF OUTPUT\n");

return 0;
}

int check(int bah)
{
int soma=0, j;

if(bah==1)
return 0;

for(j=1; j<bah; j++)
{
if(bah%j==0)
soma+=j;
}

if(soma>bah)
return 2;

if (soma==bah)
return 1;

else return 0;
}

[/c]

Hope it helps and that you can see the difference.

New poster
Posts: 10
Joined: Mon Jul 19, 2004 6:35 pm
Thank you very much.

CrazyTerabyte
New poster
Posts: 25
Joined: Fri Jul 16, 2004 3:19 am
Location: Brazil
Contact:

### 382 - I need a bigger problem

Hi!

I solved 382 so well that it can give me solution for all numbers until 60000 in less than two seconds. I would like to know about a problem similar to 382, but with larger testcase, or larger numbers.

Thanks.

efr_shovo
New poster
Posts: 38
Joined: Wed Sep 22, 2004 9:09 am

### Help 382 Gives P.E

#include<stdio.h>

long count,sum,n[100],d,mod,m,z;

int input()
{
int i=0,f;
do
{
scanf("%d",&n);
f=n;
i++;
}while(f!=0);
return i;
}

void space(int j)
{
if(n[j]<10)
printf(" ");
if(n[j]>=10&&n[j]<100)
printf(" ");
if(n[j]>=100&&n[j]<1000)
printf(" ");
if(n[j]>=1000&&n[j]<10000)
printf(" ");
}

void main()
{
int i=0;
z=input();
if(z>1&&z<100)
{
printf("PERFECTION OUTPUT\n");
while(1)
{
if(n>60000)
break;
if(n==0)
break;
d=n/2;
for(count=1;count<=d;count++)
{
mod=n%count;
if(mod==0)
sum=sum+count;
}
space(i);
if(sum>n)
printf("%ld ABUNDANT\n",n);
else
if(sum==n)
printf("%ld PERFECT\n",n);
else
printf("%ld DEFICIENT\n",n[i]);
sum=0;
i++;
}
printf("END OF OUTPUT");
}
}