Can you tell me ,what will be output ,if I input 100 and 97? Please tell me soon.
160 - Factors and Factorials
Moderator: Board moderators
problem 160
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
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
problem 160
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]
HI~
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.
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.
problem 160
Can you tell me why I always get "wrong answer"?How can I send you my code ?Please tell me soon.
Help
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
http://www.comp.nus.edu.sg/~stevenha/pr ... /vol1.html
-novice

problem 160 code
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]
[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]
Re: Help
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;
}
#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;
}
You can compare me code
My code can do the right way
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]
160
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]
[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]
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 ");
}
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...
you never know what you're going to get...
hey hey hey, now help me please
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]
[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]