636 - Squares (III)

All about problems in Volume 6. 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
CelebiX
New poster
Posts: 5
Joined: Tue Jun 18, 2002 11:05 am

636 - Squares (III)

Post by CelebiX »

I got WA every time..... plz help me ><
plz ><

[cpp]#include <iostream>
#include <math.h>
#include <string>

long long i,j,k,l,tmp,a,digits[100];
string strg;

int main() {
while(cin >> strg,strg!="0") {
j=2;
for(i=0,k=strg.length()-1;i<strg.length();++i) {
digits=strg[k-i]-48;
if(digits>j)
j=digits;
}

for(;j<100;++j) {
tmp=0;
a=1;
for(k=0;k<i;++k) {
tmp+=digits[k]*a;
a*=j;
}
a=sqrt(tmp);
if(tmp==a*a) {
cout << j << endl;
break;
}
}
}
}
[/cpp]

cyfra
Experienced poster
Posts: 144
Joined: Thu Nov 22, 2001 2:00 am
Location: Gdynia, Poland

Hi!

Post by cyfra »

Hi!

I didn't analize your code but I only tested it....

For the input:
20
0
It should be:
8
And your program gives
2
What is wrong answer as you can see...

So change this and resubmit....

Good Luck :wink:

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

[none]

Post by ec3_limz »

BTW, are you allowed to use cin and cout for OJ? My friend said that OJ doesn't accept cin and cout as standard input and output streams.

medv
Learning poster
Posts: 85
Joined: Sun Jul 14, 2002 1:17 pm

636 - WA. Why?

Post by medv »

Why WA? Help me, please!

program p636;
var
s:string;
b,i,base,len,n:longint;
mas:array[1..100] of integer;
begin
while true do
begin
readln(s); len := length(s);
if s = '0' then break;
for i:=1 to len do mas[i] := Ord(s[i]) - Ord('0');
b := 2;
for i:=1 to len do
if mas[i] > b then b := mas[i];
for base:=b+1 to 100 do
begin
n := 0;
for i:=1 to len do
n := base*n + mas[i];
i := trunc(sqrt(n));
if i*i=n then break;
end;
writeln(base);
end;
end.

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

Post by ec3_limz »

The problem statement states that,
for each number, there exists a sought base and it is less than 100
Therefore, I do not think that you have to consider base 100.

This is so far the only difference I can see between your code and my code, which was Accepted. You may want to try this.

Good luck.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

636

Post by sumankar »

Code: Select all

[c]
#include<stdio.h>
#include<math.h>

int main()
{
	long n, temp;
	int i, j;
	char num[12];
	double t, val;

	while( scanf("%s",num) == 1 ) {
		if( !(n = atoi(num)) )
			exit(0);
		i = 1;
		for( j = strlen(num)-1; j >= 0; j-- )
			if( i < num[j]-'0' )
				i = num[j]-'0';		
		i++;
		for( ; i < 101; i++)  {
			temp = n;
			val = 0.0;
			for( j = 0; temp > 0; j++ ) {
				val += (temp%10)*pow(i, j);
				temp /= 10;
			}
			t = sqrt(val);
			if( t*t == val )
				break;
		}
		printf("%d\n",i);
	}
	return 0;
}
[/c]


Any bugs ???
Suman

zubi
New poster
Posts: 4
Joined: Sun Mar 25, 2007 5:49 pm

636 - Squares

Post by zubi »

I dunno why im getting WA :(

Code: Select all

#include <iostream>
#include<math.h>
using namespace std;

long long x,t,res,lsd,base=3,num,maxi;


long long convert(long long base)
{

long long count=0,res=0;

while(t!=0)
{   lsd=t%10;
    res+=(lsd*(pow(base,count)));
    t/=10;
    count++;
}

return res;

}

long long issquare(long long no)
{
 if(sqrt((long long)no)*sqrt((long long)no)==no)  { return 1;}//cout<<no<<"is square"<<endl;
 //cout<<no<<"is not square"<<endl;
 return 0;
}

main()
{

    long long retcon,retis,current_base,temp;

//cin>>x>>base;
//t=x;

//convert(base);
//issquare(81);

cin>>num;

while(num!=0)
{
maxi=0;


t=num;

while(t!=0)
{
  temp=(t%10);
  if (temp>maxi) maxi=temp;
  t/=10;
}




current_base=maxi+1;
while(current_base<100)
{
    t=num;

  //  cout<<"current_base:"<<current_base;
    retcon=convert(current_base);

   // cout<<"\tAt base : "<<current_base<<" \t"<<num<<" is :"<<retcon<<endl;

    retis=issquare(retcon);
    if(retis)
    {
        cout<<current_base;
        break;
    }


    current_base++;

}
cout<<endl;
cin>>num;

}

}



is it wrong presentation?

mgavin2
New poster
Posts: 43
Joined: Sat Jul 28, 2012 6:29 pm

Re: 636 - Squares

Post by mgavin2 »

I have no clue why I'm getting WA either :\ .. except maybe that the numbers get too large for long long?

brianfry save us! :) <3

Code: Select all

Got AC
I was searching for the highest digit in the number... but I had

Code: Select all

for (int i; i < strlen(num); ++i) highest = max(highest, num[0]-0x30); 
and num[0] should've been num :\
all that matters is AC

Post Reply

Return to “Volume 6 (600-699)”