Page 2 of 14
Posted: Mon Jul 22, 2002 3:02 pm
There are two problems:
i) There is an extra space after the '=' sign in each step and also when you printed the newline character along with some spaces.

Code: Select all

``````here---->>printf("%3d! = ", fact);
for (i = 0; i < 25; i++) {
if (i == 15)
here----->>printf("\n       ");
if (factors[i] == 0)
break;
printf("%3d", factors[i]);
}
printf("\n");``````
ii) There is also a problem when the out put contains exactly 15 numbers after the '=' sign. Examine your code. Suppose there are 15 numbers in the output. then i==15 is satisfied and a newline is printed. But as there are not more then 15 numbers in the output no number is printed in the next line. And for the last "printf("\n");" there is another newline in the output. So for certain numbers there will be 2 newlines.

What u can do is just put the if(i==15) condition after the if(factors ==0)
condition. ### accepted thx

Posted: Tue Jul 23, 2002 9:47 am
Got accepted. Thanx a lot! ### 160:Always WA..why?

Posted: Tue Aug 06, 2002 2:05 am

#include <stdio.h>
#include <math.h>

void main()
{
long t,cn,p,pr,a,b,c,d,e,n,i,j;

for(;;)
{
scanf("%ld",&n);
if(n==0) break;
d=0;
for(a=2;a<=n;a++)
{
p=1;
b=a;
for(c=2;c<=sqrt(b);c++)
if(!(b%c))
{
p=0;
break;
}
if(p)
pr[d++]=b;
}
e=d;

for(d=0;d<e;d++)
cn[d]=0;

for(a=n;a>=2;a--)
{
b=a;
for(d=0;d<e;)
{
i=0;
if(!(b%pr[d]))
{
t=d;
b/=pr[d];
cn[t]++;
i=1;
}

if(b<pr[d]) break;
if(!i) d++;
}
}
printf("%3ld! =",n);
for(j=0;j<e;j++)
{
printf("%3ld",cn[j]);

if(j==14)
{
printf("\n");
printf(" ");
}

}
printf("\n");
}
}

### p-160 by shohag

Posted: Wed Aug 07, 2002 8:26 am
I think this is not good.
There is a good process.Like,
Below or equal 5 all prime are 2,3,5.
and for 5 the power of 2 is

k1=5/2;
k2=2/2;
sum=k1+k2;

total power of 2 is 3.
This process for 2,until ki<=2.
Same process for 3,5.

So, for 5

5!=2^3*3^1*5^1;

Is this a good process. Posted: Wed Aug 07, 2002 5:27 pm
Hi Razib....

There's nothing wrong about the method....u've done wrong with the printing.

Code: Select all

``````
for(j=0;j<e;j++)
{
printf("%3ld",cn[j]);

if(j==14)
{
printf("\n");
printf(" ");
}

``````
look at ur code...there are 2 mistakes:

i) when the output goes to the 2nd line u've printed less spaces
(i'm not sure about it coz as u paste ur code here sometimes the spaces vanish. so u should place ur code within code tag )

ii) Think of the case when e equals to 15. In that case when j=14 u've printed a newline and a space...and then the loop terminates and after that u've printed another newline. So for certain numbers there are two newlines (47 to 52)

what u can do is replace the if(j==14) part and do it in the folllowing way:

Code: Select all

``````
for(j=0;j<e;j++)
{
if(j==15)
{
printf("\n");
printf("      ");
}

printf("%3ld",cn[j]);

}
``````

### Thanx for ur support !!!

Posted: Thu Aug 08, 2002 9:00 pm
Hi,
thanx a lot for ur sujession...
I had to cut the spaces just...
Then everything was fine...

Thanx again...
Razib ### I need help. I'm trying to resolve problem 160 in java.

Posted: Mon Jun 23, 2003 2:22 am
Hello :

I'm new here, and I have been having troubles using the judge: first because I couldn't use java.oi and after because I got a Wrong answer when I'm really sure my solution works.

So I give you my java code and I hope some of you could help me.

[java]
// @JUDGE_ID: 32852WA 160 Java
import java.io.*;
import java.util.*;

class Main{

int ajuste = 0;
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}

public static void main (String args[]) throws IOException{
Main funciona = new Main();
funciona.empieza();
}

void empieza(){

String input;
while ((input = Main.ReadLn (255)) != null) {
String linea = input;
StringTokenizer st = new StringTokenizer(linea);
int num = Integer.parseInt(st.nextToken());
ajuste = 0;
if(!(num == 0 || num < 2 || num > 100)){
if(num == 100 ){
System.out.print(num + "!" + " = ");
}else if(num < 100 && num >= 10){
System.out.print(" " + num + "!" + " = ");
}else{
System.out.print(" " + num + "!" + " = ");
}
int primo = 2;
do{
if(primo != 2 && ajuste != 15)
System.out.print(" ");
int veces = cuentaVeces(num, primo);
imprimeVeces(veces);
primo = nextPrimo(primo);
}while(num >= primo);
}else{
break;
}
}
}

int nextPrimo(int n){
int posible = n + 1;
while(true){
for(int i = 2; i < posible; i++){
if(posible % i == 0){
posible++;
i = 2;
}
}
return posible;
}
}

void imprimeVeces(int veces){
if(ajuste == 15){
System.out.print("\n ");
System.out.flush();
ajuste = 0;
}
if(veces < 100 && veces >= 10){
System.out.print(veces + "");
}else{
System.out.print(" " + veces + "");
}
ajuste++;
}

int cuentaVeces(int num, int primo){
int aux = 0;
int primoAux = primo;
while(num >= primo){
aux += num/primo;
primo *= primoAux;
}
return aux;
}
}
[/java]

The messange that I received was:
"Your program has not solved the problem. It ran during 0.008 seconds."
but why??

Posted: Mon Jun 23, 2003 3:21 am
Are u trying to print out the prime factors using the code u posted? I think the problem is asking u to print the number of '2's, '3's, and so on that appears in n!, where n is a number. Read the problem description again.

Here's what I did to solve this problem: Dynamic programming and a single-dimension array will do the job.

In fact, u can import java.io for the IOException class, but u cannot use any of the other classes there.

### Re: I need help. I'm trying to resolve problem 160 in java.

Posted: Mon Jun 23, 2003 3:28 am
hectorzeta wrote:Hello :

[java]
void imprimeVeces(int veces){
if(ajuste == 15){
System.out.print("\n "); System.out.flush();
ajuste = 0;
}
if(veces < 100 && veces >= 10){
System.out.print(veces + " ");
}else{
System.out.print(" " + veces + " ");
}
ajuste++;
}

}[/java]

The messange that I received was:
"Your program has not solved the problem. It ran during 0.008 seconds."
but why??
There is also a problem I saw with the statement in bold, why are you doing this: System.out.print("\n "); ? U will add extra spaces in the next line. Use the following instead: System.out.println();

### Why 160 P.E.

Posted: Sat Jun 28, 2003 4:53 am
I solved this problem in 0.0s
but still P.E.

Who can help me?

I don't understand.

[cpp]#include <iostream>
#include <iomanip.h>

int const Max=25;
int Prime[Max];
int Count[Max]={0};

void CreatePrime(int max)
{
int k=0;
int i,j;
for(i=2;i<max;i++)
{
for(j=i-1;j>1;j--)
if(i%j==0) break;
if(j==1)
{
//cout<<i<<" ";
Prime[k++]=i;
}
}
}

void main()
{
CreatePrime(100);
int n; // range from 2 to 100
while(1)
{
cin>>n;
if(n==0) break;
setiosflags(ios::right);
cout<<setw(3)<<n<<"! =";
int i,j;
for(i=n;i>1;i=n)
{
for(j=0;i>1&&j<Max;j++)
if(i>=Prime[j]&&i%Prime[j]==0)
{
i/=Prime[j];
Count[j]++;
j--; // continue test the current number
}
n--;
}
for(i=0;i<Max;i++)
if(Count==0) break;
j=i;
int k;
for(i=0,k=0;i<j;i++,k++)
{
if(k==15)
{
cout<<endl<<" ";// 6 spaces
k=0;
}
setiosflags(ios::right);
cout<<setw(3)<<Count<<" ";
Count=0; // Clear the early stage
}
cout<<endl;
}
}[/cpp]

Posted: Sun Jun 29, 2003 10:39 am
I know what's wrong with my code.

Thank you all visitors.

Posted: Fri Oct 31, 2003 3:09 pm
This thread helped me too, i was printing newline accidently. But why it gives WA? shouldn't it give Presentation Error ?

Posted: Sat Nov 08, 2003 3:21 pm
Well Maarten,
maybe you should post your code so I can examine it!!   ### 160 wa help me plz

Posted: Tue Nov 25, 2003 11:47 am
wa.think its very simple.every input output is ok.plz help me.

[cpp]

/* @JUDGE_ID: xxxxxx 160 C++ */

#include<stdio.h>
int main()
{
long int i,j,n,k,p=0;
static int x;
scanf("%ld",&n);
while(n>0)
{ for(i=2;i<=n;i++)
{ j=i;
for(k=2;k<=i;k++)
{ while(j%k==0)
{ ++x[k];
j/=k;
}
}
}
printf("%3ld! = ",n);
for(i=0;i<=99;i++)
{
if(x)
{
p++;
printf("%3d ",x);
}
if(p%15==0&&p>0)
{
if(n==100)
printf("\n ");
else
printf("\n ");
p=0;
}
}
for(i=0;i<=99;i++)
x=0;
printf("\n");
scanf("%ld",&n);
//if(n==100)
//printf(" ");
p=0;
}
return 0;
}
[\cpp]

### PE why ?help me.

Posted: Wed Dec 10, 2003 11:03 am
PE help me plz.here is my source code.

/* @JUDGE_ID: xxxxxx 160 C++ */

#include<stdio.h>
#include<string.h>
int main()
{
long int i,j,n,k,p=0;
static int x;
memset(x,0,500);
scanf("%ld",&n);
while(n>0)
{ for(i=2;i<=n;i++)
{ j=i;
for(k=2;k<=i;k++)
{ while(j%k==0)
{ ++x[k];
j/=k;
}
}
}
printf("%2ld! = ",n);
for(i=0;i<=99;i++)
{
if(x)
{
p++;
if(p==16&&n==100)
{
printf("\n ");
p=0;
}
if(p==16)
{
printf("\n ");
p=0;
}
printf("%2d ",x);
}
/* if(n>=53&&p>=15)
{
if(n==100)
printf("\n ");
else
printf("\n ");
p=0;
} */
}
memset(x,0,500);
printf("\n");
scanf("%ld",&n);
p=0;
}
return 0;
}