160 - Factors and Factorials

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

Moderator: Board moderators

Post Reply
koniloni
New poster
Posts: 15
Joined: Thu Apr 18, 2002 5:10 am

problem 160

Post by koniloni » Sun May 12, 2002 7:42 am

Can you tell me ,what will be output ,if I input 100 and 97? Please tell me soon.

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel » Sun May 12, 2002 1:51 pm

Code: Select all

100! = 97 48 24 16  9  7  5  5  4  3  3  2  2  2  2
        1  1  1  1  1  1  1  1  1  1
 97! = 94 46 22 14  8  7  5  5  4  3  3  2  2  2  2
        1  1  1  1  1  1  1  1  1  1
Before 97 there should be a space, but it is not displayed.

koniloni
New poster
Posts: 15
Joined: Thu Apr 18, 2002 5:10 am

problem 160

Post by koniloni » Tue May 21, 2002 7:33 am

Can you tell me what will be the output, if I input 2 and 1 and do I need to use "FILE" for this program? Please tell me soon.I think my program is right , but the judge's reply is "wrong answer, it ran during 0:10 seconds".What can I do now? [/code][/b][/c]

10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

HI~

Post by 10153EN » Tue May 21, 2002 9:33 am

2! = 1

and I think no need to test for the input of 1, since the smallest input is 2.

And what do u meant my FILE? The IO of the problem is the same as the others.

koniloni
New poster
Posts: 15
Joined: Thu Apr 18, 2002 5:10 am

problem 160

Post by koniloni » Wed May 22, 2002 8:11 am

Can you tell me why I always get "wrong answer"?How can I send you my code ?Please tell me soon.

10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN » Wed May 22, 2002 4:09 pm

You can send the code to my email at your convenience

Fresh
New poster
Posts: 46
Joined: Mon Apr 15, 2002 10:42 am
Contact:

Help

Post by Fresh » Wed May 22, 2002 7:34 pm

If you still fail on this problem try visit link below or mail me your code.

http://www.comp.nus.edu.sg/~stevenha/pr ... /vol1.html

-novice :wink:

koniloni
New poster
Posts: 15
Joined: Thu Apr 18, 2002 5:10 am

problem 160 code

Post by koniloni » Thu May 23, 2002 8:45 am

This is my problem 160 code:
[url]
[c][code][quote]
int main()
{
int x[25],p[99],f[10];
int i,y,count=0,n,k=0,l=0,z,j=0;
for(i=2;i<=100;++i)
{
for(y=1;y<=i;++y)
if(i%y==0)
++count;
if(count==2)
{
x[j]=i;
count=0;
++j;

}
else
count=0;
}
while(scanf("%d",&n)==1 && n!=0)
{
if(n>=2 &&n<=100)
{
printf("%3d! =",n);
j=0,count=0;
for(i=2;i<=n;++i)
{
p[j]=i;
++j;
}
for(i=0;i<=25;++i)
{

for(z=0;z<n-1;++z)
if(p[z]!=0)
++count;
if(count>=1)
{
count=0;
for(z=0;z<n-1;++z)
{
if(p[z]!=0 && p[z]%x[i]==0)
{
while(p[z]%x[i]==0) {
p[z]=p[z]/x[i];
++count;
}
if(p[z]==1)
p[z]=0;
}
}
if(k<15)
{
printf("%3d",count);
count=0;
++k;
}
else if(k>=15)
{
f[l]=count;
count=0;
++l;
++k;
}
}
else
{
if(k>=16)
{
printf("\n%9d",f[0]);
for(z=1;z<l;++z)
printf("%3d",f[z]);
for(z=0;z<l;++z)
f[z]=0;
l=0;
break;
}
else
break;
}
}
k=0;
}
}
return 0;
}
[/quote][/code][/c][/url]

koniloni
New poster
Posts: 15
Joined: Thu Apr 18, 2002 5:10 am

Re: Help

Post by koniloni » Sun May 26, 2002 6:49 am

Please help me to solve Problem 160.This is my program code:
#include<stdio.h>
int main()
{
int x[25],p[99],f[10];
int i,y,count=0,n,k=0,l=0,z,j=0;
for(i=2;i<=100;++i)
{
for(y=1;y<=i;++y)
if(i%y==0)
++count;
if(count==2)
{
x[j]=i;
count=0;
++j;

}
else
count=0;
}
while(scanf("%d",&n)==1 && n!=0)
{
if(n>=2 &&n<=100)
{
printf("%3d! =",n);
j=0,count=0;
for(i=2;i<=n;++i)
{
p[j]=i;
++j;
}
for(i=0;i<=25;++i)
{

for(z=0;z<n-1;++z)
if(p[z]!=0) ++count;
if(count>=1)
{
count=0;
for(z=0;z<n-1;++z)
{
if(p[z]!=0 && p[z]%x==0)
{
while(p[z]%x==0)
{
p[z]=p[z]/x;
++count;
}
if(p[z]==1)
p[z]=0;
}
}
if(k<15)
{
printf("%3d",count);
count=0;
++k;
}
else if(k>=15)
{
f[l]=count;
count=0;
++l;
++k;
}
}
else
{
if(k>=16)
{
printf("\n%9d",f[0]);
for(z=1;z<l;++z)
printf("%3d",f[z]);
for(z=0;z<l;++z)
f[z]=0;
l=0;
break;
}
else
break;
}
}
k=0;
}
}
return 0;
}

mark00lui
New poster
Posts: 6
Joined: Wed May 22, 2002 6:49 pm
Contact:

Post by mark00lui » Sun May 26, 2002 4:35 pm

You can compare me code
My code can do the right way

Code: Select all

[c]
#include<stdio.h>

int main(void)
{
	int input=0;
	
	while(1==scanf("%d", &input))
	{
		if(0==input)
			return 0;
		else
		{
			int array[25][2]=
			{{2,0},{3,0},{5,0},{7,0},{11,0}
			,{13,0},{17,0},{19,0},{23,0},{29,0}
			,{31,0},{37,0},{41,0},{43,0},{47,0}
			,{53,0},{59,0},{61,0},{67,0},{71,0}
			,{73,0},{79,0},{83,0},{89,0},{97,0}};

			int i=0, j=0, temp=0;

			for(i=0; i<input; i++)
			{
				for(temp=input-i, j=0; 1!=temp, j<25; j++)
				{
					if(0==temp%array[j][0])
					{
						array[j][1]++;
						temp/=array[j][0];
						j--;
					}
				}
			}

			printf("%3d! =", input);
			for(i=0; array[i][0]<=input&&i<25; i++)
			{
				if(0==(i+1)%16)
					printf("\n      ");
				printf(" %2d", array[i][1]);
			}
			printf("\n");
		}
	}

	return 0;
}[/c]

koniloni
New poster
Posts: 15
Joined: Thu Apr 18, 2002 5:10 am

thank you

Post by koniloni » Mon May 27, 2002 7:40 am

Thank you for helping me to slove the problem 160.

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

160

Post by htl » Thu Jul 11, 2002 12:39 pm

Why does this code alway get WA? I check the code many times and can't find out the bug.
[c]
#include<stdio.h>
#define YES 1
#define NO 0
void main(void)
{
int prime[25],i,is_prime,x,may_be_prime=5,gap=4,n,ans[25],temp,count;
prime[0]=2,prime[1]=3,prime[2]=5;
for(x=3;x<25;)
{
is_prime=YES;
gap=6-gap;
may_be_prime+=gap;
for(i=0;prime*prime<=may_be_prime && is_prime;i++)
if(may_be_prime%prime==0)
is_prime=NO;
if(is_prime)
prime[x++]=may_be_prime;
}
while(1)
{
scanf("%d",&n);
if(n==0)
break;
for(x=0;x<25;x++)
ans[x]=0;
for(x=1;x<=n;x++)
{
temp=x;
for(i=0;i<25;i++)
if(temp%prime==0)
while(temp%prime==0)
{
temp/=prime;
ans++;
}
}
printf("%3d! =",n);
for(x=0,count=1;x<25;x++,count++)
{
if(ans[x]!=0)
printf("%3d",ans[x]);
if(count%15==0)
printf("\n ");
}
printf("\n");
}
}
[/c]

imranul
New poster
Posts: 12
Joined: Fri Jul 19, 2002 6:28 pm
Location: Bangladesh

Post by imranul » Fri Jul 19, 2002 7:17 pm

The problem is right here:
you increased count every time.
But you should have increased count whenever you print a digit.


for(x=0,count=1;x<25;x++,count++)
{
if(ans[x]!=0)
printf("%3d",ans[x]);
if(count%15==0)
printf("\n ");
}
Life is like a box of Chocolates,
you never know what you're going to get...

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

Post by htl » Sun Jul 21, 2002 4:56 am

I've got accepted. Thanx

ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

hey hey hey, now help me please

Post by ec3_limz » Mon Jul 22, 2002 12:59 pm

The following code got a WA. Why?

[cpp]#include <stdio.h>

int main() {
const int primes[25] = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
};
int fact, factors[25], i, j;

while (true) {
for (i = 0; i < 25; i++)
factors = 0;

scanf("%d", &fact);
if (fact == 0)
break;

for (i = 0; i < 25; i++) {
if (primes > fact)
break;
for (j = primes; ; j *= primes) {
if (j > fact)
break;
factors += fact / j;
}
}

printf("%3d! = ", fact);
for (i = 0; i < 25; i++) {
if (i == 15)
printf("\n ");
if (factors == 0)
break;
printf("%3d", factors);
}
printf("\n");
}

return 0;
}[/cpp][/cpp]

Post Reply

Return to “Volume 1 (100-199)”