382 - Perfection

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok » Fri May 09, 2003 2:15 am

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

Post by Zhao Le » Fri May 09, 2003 8:15 am

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

Post by Marcin » Sat Jul 05, 2003 12:56 am

Please tell me what's wrong with my code:
[pascal]program Perfection;
var
a,i:longint;
s:longint;
begin
writeln('PERFECTION OUTPUT');
read(a);
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;
read(a);
end;
writeln('END OF OUTPUT');
end.[/pascal]

User avatar
hank
Experienced poster
Posts: 146
Joined: Mon Feb 04, 2002 2:00 am
Location: VCORE.

Post by hank » Sat Jul 05, 2003 4:54 pm

be careful "1 is DEFICIENT"

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

Post by Marcin » Sat Jul 05, 2003 10:27 pm

Thanks Hank.
I changed it and got AC :D

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

382 WA why???

Post by Morning » Tue Feb 03, 2004 3:27 pm

[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

Post by pavelph » Tue Feb 03, 2004 5:21 pm

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

Post by Morning » Tue Feb 03, 2004 5:24 pm

1 is perfect,isn't it?
And what i am using is c++ :oops:
"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

Post by pavelph » Tue Feb 03, 2004 5:30 pm

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. :wink:

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

Post by Morning » Tue Feb 03, 2004 5:31 pm

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

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

382 - WA

Post by vadiu » Mon Jul 19, 2004 6:44 pm

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]

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

can be shorter

Post by sohel » Tue Jul 20, 2004 7:14 am

Hi Vadiu:

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.
:)

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

Post by vadiu » Tue Jul 20, 2004 3:19 pm

Thank you very much. :lol:

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

382 - I need a bigger problem

Post by CrazyTerabyte » Wed Jul 28, 2004 1:02 am

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

Post by efr_shovo » Wed Sep 29, 2004 9:10 am

Why 382 Gives P.E.Please help




#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");
}
}

Post Reply

Return to “Volume 3 (300-399)”