10110 - Light, more light
Moderator: Board moderators
10110 - Light, more light
Works for all test cases. Don't know why it WAs. Some help from them JAVA people would be nice.
[java]import java.io.*;
import java.lang.*;
class Main
{
public static void main(String args[])
{
StringBuffer B;
String S;
int N,Length,i;
char c;
while(true)
{
try
{
B=new StringBuffer();
while((c=(char)System.in.read())!='\n'&&c!=-1)
B.append(c);
}
catch(IOException e)
{
break;
}
S=B.toString();
N = Integer.parseInt(S.substring(0,S.length()-1));
if(N==0||c==-1)
break;
if(Math.pow(Math.floor(Math.sqrt(N)),2)==N)
System.out.println("yes");
else
System.out.println("no");
}
}
};[/java]
[java]import java.io.*;
import java.lang.*;
class Main
{
public static void main(String args[])
{
StringBuffer B;
String S;
int N,Length,i;
char c;
while(true)
{
try
{
B=new StringBuffer();
while((c=(char)System.in.read())!='\n'&&c!=-1)
B.append(c);
}
catch(IOException e)
{
break;
}
S=B.toString();
N = Integer.parseInt(S.substring(0,S.length()-1));
if(N==0||c==-1)
break;
if(Math.pow(Math.floor(Math.sqrt(N)),2)==N)
System.out.println("yes");
else
System.out.println("no");
}
}
};[/java]
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
That program ONLY took me 7 hours in JAVA:O I am starting to see why without the good classes, JAVA is horribly complicated and slow. Although the error checking compiler is quite nice. It is much more strict than C++ and won't let you access out of bounds and such. The other thing I noticed is that the compiler on the 24 hour judge doesn't support all the features of even simple classes; there are wierd little discrepancies. However, I think it would be useful to know from things like TopCoder where you need to analyze other people's code as well as use the nice BigInt. It is silly how some people think StringTokenizer is better than strtok; they haven't mastered strtok. Strings and general input/output is SOOO much easier in C++ (e.g. SSCANF).
10110 More Light's Please
Why I receive WA ??? What's the problem ???
[cpp]
#include<stdio.h>
#include<math>
void main()
{
int n;
while (1)
{
scanf("%d",&n);
if (n == 0) break;
if (((int)sqrt(n))==sqrt(n))
printf("yes\n");
else
printf("no\n");
}
}
[/cpp]
Thanx in advance![8)](./images/smilies/icon_cool.gif)
[cpp]
#include<stdio.h>
#include<math>
void main()
{
int n;
while (1)
{
scanf("%d",&n);
if (n == 0) break;
if (((int)sqrt(n))==sqrt(n))
printf("yes\n");
else
printf("no\n");
}
}
[/cpp]
Thanx in advance
![8)](./images/smilies/icon_cool.gif)
More Pascal Light also
I tried this in Pascal but it didn't work also, i need to check if N has integer square root .
Please help !
[pascal]
program p10110;
var
n: integer;
begin
while not eof(input) do
begin
readln(n);
if (n = 0) then break;
if frac(sqrt(n))<0.000001 then writeln('yes') else
writeln('no');
end;
end.
[/pascal]
P.S: also tried
[pascal]
if abs(trunc(sqrt(n))-sqrt(n))<0.000001 then writeln('yes') else writeln('no');
if trunc(sqrt(n))*trunc(sqrt(n))=n then writeln('yes') else writeln('no');
[/pascal]
Please help !
[pascal]
program p10110;
var
n: integer;
begin
while not eof(input) do
begin
readln(n);
if (n = 0) then break;
if frac(sqrt(n))<0.000001 then writeln('yes') else
writeln('no');
end;
end.
[/pascal]
P.S: also tried
[pascal]
if abs(trunc(sqrt(n))-sqrt(n))<0.000001 then writeln('yes') else writeln('no');
if trunc(sqrt(n))*trunc(sqrt(n))=n then writeln('yes') else writeln('no');
[/pascal]
Why don't you try generating all squares and then search the array for n.
You'll have 65535 squares at max. So, using binary search you could determine, whteher sqrt(n) is an integer or not. The array won't be that big, but it could be not the fastest way.
Ivor
P.S I haven't solved the problem myself, so I'm just giving some ideas that popped up my mind.
You'll have 65535 squares at max. So, using binary search you could determine, whteher sqrt(n) is an integer or not. The array won't be that big, but it could be not the fastest way.
Ivor
P.S I haven't solved the problem myself, so I'm just giving some ideas that popped up my mind.
UNSIGNED PROBLEM
I didn't read problem carefully enough
, 2^32 - 1 is 4,294,967,295 and this must be UNSIGNED int .
Thanx anyway !
![:(](./images/smilies/icon_frown.gif)
Thanx anyway !
Last edited by ante on Wed Apr 24, 2002 11:42 pm, edited 1 time in total.
use type cardinal in pascal
type integer is signed 32 bits.
type cardinal is unsigned 32 bits (could be more, I don't know).
this code works:
[pascal]var
n,r:cardinal;
begin
repeat
readln(n);
if (n=0) then break;
r:=trunc(sqrt(n));
if (r*r=n) then writeln('yes') else writeln('no');
until false;
end.
[/pascal]
so there's no rounding problem
type cardinal is unsigned 32 bits (could be more, I don't know).
this code works:
[pascal]var
n,r:cardinal;
begin
repeat
readln(n);
if (n=0) then break;
r:=trunc(sqrt(n));
if (r*r=n) then writeln('yes') else writeln('no');
until false;
end.
[/pascal]
so there's no rounding problem
10110 - Lights, more lights (slow and fat)
How do you reduce either the running time or the memory usage for
this problem? I was astonished to see my tiny Pascal program using
0.670 seconds and something like 468K! This code doesn't use
anything that other programs I'm submitted, which all ran in 0.000
and 64K.
Any ideas?
--
Chris Long, Mathematics Department, Rutgers University
Mozart needs food. Beethoven now has extra fight power. Bach is about to die.
this problem? I was astonished to see my tiny Pascal program using
0.670 seconds and something like 468K! This code doesn't use
anything that other programs I'm submitted, which all ran in 0.000
and 64K.
Any ideas?
--
Chris Long, Mathematics Department, Rutgers University
Mozart needs food. Beethoven now has extra fight power. Bach is about to die.