343 - What Base Is This?
Moderator: Board moderators
-
- New poster
- Posts: 42
- Joined: Fri Jun 13, 2003 3:47 pm
- Location: Dhaka , Bangladesh
- Contact:
343-WA(Please help me)
//here i compute both +\- input
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str1[1000],str2[1000],*ptr,temp;
long num1,num2,i,j,flag,a,b;
while(scanf("%s %s",str1,str2)==2)
{
if(strcmp(str1,"0")==0 && strcmp(str2,"0")==0)
printf("0 (base 2) = 0 (base 2)\n");
else
{
temp='0';
flag=0;
if(str1[0]=='-' && str2[0]=='-')
{
for(i=0;i<=strlen(str1)-1;i++)
str1=str1[i+1];
str1='\0';
for(i=0;i<=strlen(str2)-1;i++)
str2=str1[i+1];
str2='\0';
for(i=0;i<strlen(str1);i++)
{
if(str1>temp)
temp=str1;
}
if(temp>='A' && temp<='Z')
a=temp-54;
else
a=temp-47;
temp='0';
for(i=0;i<strlen(str2);i++)
{
if(str2>temp)
temp=str2;
}
if(temp>='A' && temp<='Z')
b=temp-54;
else
b=temp-47;
for(i=a;i<=36;i++)
{
num1=strtol(str1,&ptr,i);
for(j=b;j<=36;j++)
{
num2=strtol(str2,&ptr,j);
if(num1==num2)
{
flag=1;
break;
}
}
if(flag==1) break;
}
if(flag==1)
printf("-%s (base %ld) = -%s (base %ld)\n",str1,i,str2,j);
else
printf("-%s is not equal to -%s in any base 2..36\n",str1,str2);
memset(str1,'\0',1000);
memset(str2,'\0',1000);
}
else if(str1[0]=='-' && str2[0]!='-')
{
for(i=0;i<=strlen(str1)-1;i++)
str1=str1[i+1];
str1='\0';
for(i=0;i<strlen(str1);i++)
{
if(str1[i]>temp)
temp=str1[i];
}
if(temp>='A' && temp<='Z')
a=temp-54;
else
a=temp-47;
temp='0';
for(i=0;i<strlen(str2);i++)
{
if(str2[i]>temp)
temp=str2[i];
}
if(temp>='A' && temp<='Z')
b=temp-54;
else
b=temp-47;
for(i=a;i<=36;i++)
{
num1=strtol(str1,&ptr,i);
for(j=b;j<=36;j++)
{
num2=strtol(str2,&ptr,j);
if(num1==num2)
{
flag=1;
break;
}
}
if(flag==1) break;
}
if(flag==1)
printf("-%s (base %ld) = %s (base %ld)\n",str1,i,str2,j);
else
printf("-%s is not equal to %s in any base 2..36\n",str1,str2);
memset(str1,'\0',1000);
memset(str2,'\0',1000);
}
else if(str1[0]!='-' && str2[0]=='-')
{
for(i=0;i<=strlen(str2)-1;i++)
str2[i]=str1[i+1];
str2[i]='\0';
for(i=0;i<strlen(str1);i++)
{
if(str1[i]>temp)
temp=str1[i];
}
if(temp>='A' && temp<='Z')
a=temp-54;
else
a=temp-47;
temp='0';
for(i=0;i<strlen(str2);i++)
{
if(str2[i]>temp)
temp=str2[i];
}
if(temp>='A' && temp<='Z')
b=temp-54;
else
b=temp-47;
for(i=a;i<=36;i++)
{
num1=strtol(str1,&ptr,i);
for(j=b;j<=36;j++)
{
num2=strtol(str2,&ptr,j);
if(num1==num2)
{
flag=1;
break;
}
}
if(flag==1) break;
}
if(flag==1)
printf("%s (base %ld) = -%s (base %ld)\n",str1,i,str2,j);
else
printf("%s is not equal to -%s in any base 2..36\n",str1,str2);
memset(str1,'\0',1000);
memset(str2,'\0',1000);
}
else
{
for(i=0;i<strlen(str1);i++)
{
if(str1[i]>temp)
temp=str1[i];
}
if(temp>='A' && temp<='Z')
a=temp-54;
else
a=temp-47;
temp='0';
for(i=0;i<strlen(str2);i++)
{
if(str2[i]>temp)
temp=str2[i];
}
if(temp>='A' && temp<='Z')
b=temp-54;
else
b=temp-47;
for(i=a;i<=36;i++)
{
num1=strtol(str1,&ptr,i);
for(j=b;j<=36;j++)
{
num2=strtol(str2,&ptr,j);
if(num1==num2)
{
flag=1;
break;
}
}
if(flag==1) break;
}
if(flag==1)
printf("%s (base %ld) = %s (base %ld)\n",str1,i,str2,j);
else
printf("%s is not equal to %s in any base 2..36\n",str1,str2);
memset(str1,'\0',1000);
memset(str2,'\0',1000);
}
}
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str1[1000],str2[1000],*ptr,temp;
long num1,num2,i,j,flag,a,b;
while(scanf("%s %s",str1,str2)==2)
{
if(strcmp(str1,"0")==0 && strcmp(str2,"0")==0)
printf("0 (base 2) = 0 (base 2)\n");
else
{
temp='0';
flag=0;
if(str1[0]=='-' && str2[0]=='-')
{
for(i=0;i<=strlen(str1)-1;i++)
str1=str1[i+1];
str1='\0';
for(i=0;i<=strlen(str2)-1;i++)
str2=str1[i+1];
str2='\0';
for(i=0;i<strlen(str1);i++)
{
if(str1>temp)
temp=str1;
}
if(temp>='A' && temp<='Z')
a=temp-54;
else
a=temp-47;
temp='0';
for(i=0;i<strlen(str2);i++)
{
if(str2>temp)
temp=str2;
}
if(temp>='A' && temp<='Z')
b=temp-54;
else
b=temp-47;
for(i=a;i<=36;i++)
{
num1=strtol(str1,&ptr,i);
for(j=b;j<=36;j++)
{
num2=strtol(str2,&ptr,j);
if(num1==num2)
{
flag=1;
break;
}
}
if(flag==1) break;
}
if(flag==1)
printf("-%s (base %ld) = -%s (base %ld)\n",str1,i,str2,j);
else
printf("-%s is not equal to -%s in any base 2..36\n",str1,str2);
memset(str1,'\0',1000);
memset(str2,'\0',1000);
}
else if(str1[0]=='-' && str2[0]!='-')
{
for(i=0;i<=strlen(str1)-1;i++)
str1=str1[i+1];
str1='\0';
for(i=0;i<strlen(str1);i++)
{
if(str1[i]>temp)
temp=str1[i];
}
if(temp>='A' && temp<='Z')
a=temp-54;
else
a=temp-47;
temp='0';
for(i=0;i<strlen(str2);i++)
{
if(str2[i]>temp)
temp=str2[i];
}
if(temp>='A' && temp<='Z')
b=temp-54;
else
b=temp-47;
for(i=a;i<=36;i++)
{
num1=strtol(str1,&ptr,i);
for(j=b;j<=36;j++)
{
num2=strtol(str2,&ptr,j);
if(num1==num2)
{
flag=1;
break;
}
}
if(flag==1) break;
}
if(flag==1)
printf("-%s (base %ld) = %s (base %ld)\n",str1,i,str2,j);
else
printf("-%s is not equal to %s in any base 2..36\n",str1,str2);
memset(str1,'\0',1000);
memset(str2,'\0',1000);
}
else if(str1[0]!='-' && str2[0]=='-')
{
for(i=0;i<=strlen(str2)-1;i++)
str2[i]=str1[i+1];
str2[i]='\0';
for(i=0;i<strlen(str1);i++)
{
if(str1[i]>temp)
temp=str1[i];
}
if(temp>='A' && temp<='Z')
a=temp-54;
else
a=temp-47;
temp='0';
for(i=0;i<strlen(str2);i++)
{
if(str2[i]>temp)
temp=str2[i];
}
if(temp>='A' && temp<='Z')
b=temp-54;
else
b=temp-47;
for(i=a;i<=36;i++)
{
num1=strtol(str1,&ptr,i);
for(j=b;j<=36;j++)
{
num2=strtol(str2,&ptr,j);
if(num1==num2)
{
flag=1;
break;
}
}
if(flag==1) break;
}
if(flag==1)
printf("%s (base %ld) = -%s (base %ld)\n",str1,i,str2,j);
else
printf("%s is not equal to -%s in any base 2..36\n",str1,str2);
memset(str1,'\0',1000);
memset(str2,'\0',1000);
}
else
{
for(i=0;i<strlen(str1);i++)
{
if(str1[i]>temp)
temp=str1[i];
}
if(temp>='A' && temp<='Z')
a=temp-54;
else
a=temp-47;
temp='0';
for(i=0;i<strlen(str2);i++)
{
if(str2[i]>temp)
temp=str2[i];
}
if(temp>='A' && temp<='Z')
b=temp-54;
else
b=temp-47;
for(i=a;i<=36;i++)
{
num1=strtol(str1,&ptr,i);
for(j=b;j<=36;j++)
{
num2=strtol(str2,&ptr,j);
if(num1==num2)
{
flag=1;
break;
}
}
if(flag==1) break;
}
if(flag==1)
printf("%s (base %ld) = %s (base %ld)\n",str1,i,str2,j);
else
printf("%s is not equal to %s in any base 2..36\n",str1,str2);
memset(str1,'\0',1000);
memset(str2,'\0',1000);
}
}
}
return 0;
}
You can try some test cases here..
http://online-judge.uva.es/board/viewto ... hlight=343
Don't create a new thread if there is one already..
http://online-judge.uva.es/board/viewto ... hlight=343
Don't create a new thread if there is one already..
My code for 343 is getting WA
My code is able to give correct answers for the following test cases but still gets WA
Thanks a bunch! Jan my code got AC 

Code: Select all
ERASED

Last edited by mrunmoy on Fri Apr 13, 2007 11:12 am, edited 1 time in total.
Best Regards,
Mrunmoy.
I belong to { IDIOTS }
IDIOTS - Intelligent Dynamic & Innovative On-The-Spot!
Mrunmoy.
I belong to { IDIOTS }
IDIOTS - Intelligent Dynamic & Innovative On-The-Spot!
Try the cases...
Input:
Output:
Hope these help.
Input:
Code: Select all
2E 2
153 10
N 1
331 87
9D 312
86 5V
Code: Select all
2E is not equal to 2 in any base 2..36
153 is not equal to 10 in any base 2..36
N is not equal to 1 in any base 2..36
331 (base 6) = 87 (base 15)
9D (base 21) = 312 (base 8)
86 (base 25) = 5V (base 35)
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 19
- Joined: Fri Jun 22, 2007 6:17 pm
- Location: bangladesh
help me plz.....to solve 343
i don't find any reason to get wr,plz anybody help me to find the bug in this code.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
unsigned long con(char * c,int n)
{
int i,m,n1;
unsigned long p=0;
m=strlen(c);
for(i=0;i<m;i++)
{ n1=48;
if(c>='A'&&c<='Z')n1=n1+7;
p=(c-n1)*pow(n,m-i-1) +p;
}
return p;
}
void main()
{
long b,m,n,i,j,k,p;
unsigned long c1[38],d1[100];
char c[20],d[20];
//freopen("c:\\123.txt","w",stdout);
while(scanf("%s %s",c,d)==2)
{
if(strcmp(c,"0")==0&&strcmp(d,"0")==0)
{
printf("%s (base 2) = %s (base 2)\n",c,d);
continue;
}
m=strlen(c);
b=0;
for(i=0;i<m;i++)
{n=48;
if(c>='A'&&c<='Z')n=n+7;
if(b<c-n)b=c-n;
}
k=b+1;
m=strlen(d);
b=0;
for(i=0;i<m;i++)
{ n=48;
if(d>='A'&&d<='Z')n=n+7;
if(b<d-n)b=d[i]-n;
}
b=b+1;
for(i=k;i<=36;i++)
c1[i]=con(c,i);
for(i=b;i<=36;i++)
d1[i]=con(d,i);
p=0;
for(i=k;i<=36;i++)
{
for(j=b;j<=36;j++)
{
if(c1[i]==d1[j]){m=i;n=j;p=1;break;}
}
if(p==1)break;
}
if(p==1)
printf("%s (base %ld) = %s (base %ld)\n",c,m,d,n);
else printf("%s is not equal to %s in any base 2..36\n",c,d);
}
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
unsigned long con(char * c,int n)
{
int i,m,n1;
unsigned long p=0;
m=strlen(c);
for(i=0;i<m;i++)
{ n1=48;
if(c>='A'&&c<='Z')n1=n1+7;
p=(c-n1)*pow(n,m-i-1) +p;
}
return p;
}
void main()
{
long b,m,n,i,j,k,p;
unsigned long c1[38],d1[100];
char c[20],d[20];
//freopen("c:\\123.txt","w",stdout);
while(scanf("%s %s",c,d)==2)
{
if(strcmp(c,"0")==0&&strcmp(d,"0")==0)
{
printf("%s (base 2) = %s (base 2)\n",c,d);
continue;
}
m=strlen(c);
b=0;
for(i=0;i<m;i++)
{n=48;
if(c>='A'&&c<='Z')n=n+7;
if(b<c-n)b=c-n;
}
k=b+1;
m=strlen(d);
b=0;
for(i=0;i<m;i++)
{ n=48;
if(d>='A'&&d<='Z')n=n+7;
if(b<d-n)b=d[i]-n;
}
b=b+1;
for(i=k;i<=36;i++)
c1[i]=con(c,i);
for(i=b;i<=36;i++)
d1[i]=con(d,i);
p=0;
for(i=k;i<=36;i++)
{
for(j=b;j<=36;j++)
{
if(c1[i]==d1[j]){m=i;n=j;p=1;break;}
}
if(p==1)break;
}
if(p==1)
printf("%s (base %ld) = %s (base %ld)\n",c,m,d,n);
else printf("%s is not equal to %s in any base 2..36\n",c,d);
}
}
-
- New poster
- Posts: 2
- Joined: Tue Jan 23, 2007 10:40 pm
- Location: India
- Contact:
Re: plz help me ......
Code: Select all
#include<algorithm>
#include<numeric>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cassert>
#include<cmath>
#include<complex>
using namespace std;
#define FOR(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define sz size()
#define pb push_back
#define all(a) a.begin(),a.end()
typedef vector<int> vi;
typedef vector<string> vs;
class base
{
public:
int code[91];
base()
{
char a[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int j=0;
REP(i,36)
{
code[(int)a[i]]=j;
j++;
}
}
void input()
{
string in="";
while(getline(cin,in))
{
stringstream ss(in);
string s1,s2;
ss>>s1>>s2;
//cout<<s1<<" "<<s2<<"\n";
int m1,m2,v1,v2;
m1=min_base(s1);m2=min_base(s2);
if(m1==1)
m1++;
if(m2==1)
m2++;
v1=value_decimal(s1,m1);
v2=value_decimal(s2,m2);
//cout<<v1<<" "<<v2<<"\n";
if(v1==v2)
{
cout<<s1<<" (base "<<m1<<") = "<<s2<<" (base "<<m2<<")"<<"\n";
continue;
}
long long match[37];
for(int i=m1;i<=36;i++)
{
long long v1=value_decimal(s1,i);
match[i]=v1;
//cout<<v1<<" ";
}
//cin.get();cin.get();
bool f=false;
for(int i=m2;i<=36;i++)
{
long long v=value_decimal(s2,i);
int lo=m1,hi=36;
while(lo<hi-1)
{
int mid=(lo+hi)/2;
if(v==match[mid])
{
cout<<s1<<" (base "<<mid<<") = "<<s2<<" (base "<<i<<")"<<"\n";
f=true;
break;
}
if(v<match[mid])
hi=mid;
else
lo=mid;
//cout<<mid<<" ";
// cin.get();
}
if(f)
break;
}
if(!f)
cout<<s1<<" is not equal to "<<s2<<" in any base 2..36\n";
}
}
int min_base(string s)
{
int v=0;
int ln=s.sz;
REP(i,ln)
{
if(v<code[(int)s[i]])
v=code[(int)s[i]];
}
return (v+1);
}
long value_decimal(string s,int b)
{
long long result=0;
int ln=s.sz;
REP(i,ln)
{
result=result*(long long)b+(long long)code[(int)s[i]];
}
return result;
}
};
int main()
{
base a;
a.input();
//system("pause");
return 0;
}
Ranjan Kumar Ojha
IIIT-Allahabad
IIIT-Allahabad
Somebody help me!
I've tried all the test cases I could find on the board but still WA.
Could someone give me more critical test cases?
By the way is there any negetive numbers in the input or some test case like this?
I've tried all the test cases I could find on the board but still WA.
Could someone give me more critical test cases?
Code: Select all
deleted
By the way is there any negetive numbers in the input or some test case like this?
Code: Select all
000 00000
Last edited by WingletE on Thu Nov 15, 2007 1:57 pm, edited 1 time in total.
Try the cases.
Input:
Output:
Btw I think the case you provided will not be given as input.
Hope these help.
Input:
Code: Select all
9 21
B 14
6 20
Code: Select all
9 (base 10) = 21 (base 4)
B (base 12) = 14 (base 7)
6 (base 7) = 20 (base 3)
Hope these help.
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 1
- Joined: Tue Feb 12, 2008 1:18 pm
- Location: programming
343
can anyone tell me why i am getting presentation error
.....................................
________________________
CODE(C++)
_____________________________
#include<iostream>
#include<string>
#include<vector>
#define rev(s) reverse(s.begin(),s.end())
#define fr(i,j,k) for(int i=j;i<k;i++)
#define frr(i,j,k) for(i=j;i<k;i++)
#define rf(i,j,k) for(int i=j;i>k;i--)
#define vs vector<string >
using namespace std;
int pow(int x,int n)
{
if(n==0) return 1;
if(n%2==0) return pow(x,n/2)*pow(x,n/2);
else return pow(x,n-1)*x;
}
int DI(string s)
{//returns starting base
int j=0,max=0,t;
fr(i,0,s.size()){
if(s<='9') j=s-'0';
else j=s-'A'+10;
if(max<j) max=j;
}
t=max+1;
if(t==1) return 2;
return t;
}
int B(string s,int b)
{//returns decimal equivalent
int j=0,sum=0,n=0;
rf(i,s.size()-1,-1){
if(s<='9') j=s-'0';
else j=s-'A'+10;
sum+=pow(b,n)*j;
n++;
}
return sum;
}
main()
{
string s,temp;
while(getline(cin,s)){
int c1=0,c2=0;
string ss,dd,s1,s2;
int ii=s.size()-1;
while(s[ii]==' ') ii--;
while(s[ii]!=' ')
{ s2+=s[ii];
ii--;
}
ii--;
while(s[ii]==' ') ii--;
if(ii==0) s1+=s[ii];
else{
while(ii!=-1)
{ if(s[ii]==' ') {ii--;continue;}
s1+=s[ii];
ii--;
}
}
rev(s1);
rev(s2);
int i,j,sb1,sb2,sum1,sum2,check=0;
if((s1[0]=='-' && s2[0]!='-') || (s1[0]!='-' && s2[0]=='-')){
temp+=s1+" is not equal to "+s2+" in any base 2..36\n";
continue;
}
if(s1[0]=='-') {s1.erase(0,1);c1=1;}
if(s2[0]=='-') {s2.erase(0,1);}
sb1=DI(s1);
sb2=DI(s2);
frr(i,sb1,37){
frr(j,sb2,37){
sum1=B(s1,i);
sum2=B(s2,j);
if(sum1==sum2){
check=1;
break;
}
}
if(check==1) break;
}
for(int x=i;x!=0;x/=10) ss+=x%10+'0';
for(int x=j;x!=0;x/=10) dd+=x%10+'0';
rev(ss);
rev(dd);
if(check==1) {
if(c1==1)
temp+="-"+s1+"(base "+ss+") = -"+s2+"(base "+dd+")\n";
else
temp+=s1+"(base "+ss+") = "+s2+"(base "+dd+")\n";
}
else {
if(c1==0)
temp+=s1+" is not equal to "+s2+" in any base 2..36\n";
else
temp+="-"+s1+" is not equal to -"+s2+" in any base 2..36\n";
}
s.clear();
}
cout<<endl<<temp;
}
.....................................
________________________
CODE(C++)
_____________________________
#include<iostream>
#include<string>
#include<vector>
#define rev(s) reverse(s.begin(),s.end())
#define fr(i,j,k) for(int i=j;i<k;i++)
#define frr(i,j,k) for(i=j;i<k;i++)
#define rf(i,j,k) for(int i=j;i>k;i--)
#define vs vector<string >
using namespace std;
int pow(int x,int n)
{
if(n==0) return 1;
if(n%2==0) return pow(x,n/2)*pow(x,n/2);
else return pow(x,n-1)*x;
}
int DI(string s)
{//returns starting base
int j=0,max=0,t;
fr(i,0,s.size()){
if(s<='9') j=s-'0';
else j=s-'A'+10;
if(max<j) max=j;
}
t=max+1;
if(t==1) return 2;
return t;
}
int B(string s,int b)
{//returns decimal equivalent
int j=0,sum=0,n=0;
rf(i,s.size()-1,-1){
if(s<='9') j=s-'0';
else j=s-'A'+10;
sum+=pow(b,n)*j;
n++;
}
return sum;
}
main()
{
string s,temp;
while(getline(cin,s)){
int c1=0,c2=0;
string ss,dd,s1,s2;
int ii=s.size()-1;
while(s[ii]==' ') ii--;
while(s[ii]!=' ')
{ s2+=s[ii];
ii--;
}
ii--;
while(s[ii]==' ') ii--;
if(ii==0) s1+=s[ii];
else{
while(ii!=-1)
{ if(s[ii]==' ') {ii--;continue;}
s1+=s[ii];
ii--;
}
}
rev(s1);
rev(s2);
int i,j,sb1,sb2,sum1,sum2,check=0;
if((s1[0]=='-' && s2[0]!='-') || (s1[0]!='-' && s2[0]=='-')){
temp+=s1+" is not equal to "+s2+" in any base 2..36\n";
continue;
}
if(s1[0]=='-') {s1.erase(0,1);c1=1;}
if(s2[0]=='-') {s2.erase(0,1);}
sb1=DI(s1);
sb2=DI(s2);
frr(i,sb1,37){
frr(j,sb2,37){
sum1=B(s1,i);
sum2=B(s2,j);
if(sum1==sum2){
check=1;
break;
}
}
if(check==1) break;
}
for(int x=i;x!=0;x/=10) ss+=x%10+'0';
for(int x=j;x!=0;x/=10) dd+=x%10+'0';
rev(ss);
rev(dd);
if(check==1) {
if(c1==1)
temp+="-"+s1+"(base "+ss+") = -"+s2+"(base "+dd+")\n";
else
temp+=s1+"(base "+ss+") = "+s2+"(base "+dd+")\n";
}
else {
if(c1==0)
temp+=s1+" is not equal to "+s2+" in any base 2..36\n";
else
temp+="-"+s1+" is not equal to -"+s2+" in any base 2..36\n";
}
s.clear();
}
cout<<endl<<temp;
}
Re: 343(What Base is This?)
plz help donno why my code gives WA.Checked all the posts nd got expected answers.
Code: Select all
#include<iostream>
using namespace std;
long long pow(int i,int j)
{
long long val=1;
int k;
for (k=1;k<=j;k++)
val=val*i;
return val;
}
int main()
{
char X[1000],Y[1000];
int l1,l2,maxx,maxy,i,j,k,FLAG;
long long N1,N2;
while (scanf("%s%s",X,Y)!=EOF)
{
l1=strlen(X);
l2=strlen(Y);
maxx=1,maxy=1;
for (i=0;i<l1;i++)
if (isdigit(X[i]))
{
if (X[i]-48>maxx)
maxx=X[i]-48;
}
else if (isalpha(X[i]))
{
if (X[i]-55>maxx)
maxx=X[i]-55;
}
for (i=0;i<l2;i++)
if (isdigit(Y[i]))
{
if (Y[i]-48>maxy)
maxy=Y[i]-48;
}
else if (isalpha(Y[i]))
{
if (Y[i]-55>maxy)
maxy=Y[i]-55;
}
maxx++;maxy++;
FLAG=0;
for (i=maxx;i<36 && FLAG==0;i++)
for (j=maxy;j<36 && FLAG==0;j++)
{
N1=0;N2=0;
for (k=l1-1;k>=0;k--)
if (isdigit(X[k]))
N1+=pow(i,l1-k-1)*(X[k]-48);
else if (isalpha(X[k]))
N1+=pow(i,l1-k-1)*(X[k]-55);
for (k=l2-1;k>=0;k--)
if (isdigit(Y[k]))
N2+=pow(j,l2-k-1)*(Y[k]-48);
else if (isalpha(Y[k]))
N2+=pow(j,l2-k-1)*(Y[k]-55);
if (N1==N2)
FLAG=1;
}
if (FLAG==1)
printf("%s (base %d) = %s (base %d)\n",X,--i,Y,--j);
else
printf("%s is not equal to %s in any base 2..36\n",X,Y);
}
}
Re: 343(What Base is This?)
I have passed all the test case. I really don't know why I got a WA.
PLEASE HELP
PLEASE HELP
Code: Select all
#include<stdio.h>
#include<ctype.h>
char v[2][20];
int base[2];
int tonum(char c){
if(isdigit(c))
return c-'0';
else
return toupper(c)-'A'+10;
}
long long translate(char *s, int base)
{
long long val = 0;
int t = 0;
while(*s){
val *= base;
t = tonum(*s);
if(t>=base)
return -1;
val += t;
s++;
}
return val;
}
int check()
{
int i;
int j;
long long t[2][37];
for(i=0;i<2;i++)
for(j=2;j<=36;j++)
t[i][j]=translate(v[i],j);
for(i=2;i<=36;i++)
for(j=2;j<=36;j++)
if(t[0][i]==t[1][j]&&t[0][i]!=-1){
base[0]=i;
base[1]=j;
return 1;
}
return 0;
}
int main()
{
while(scanf("%s%s",v[0],v[1])!=EOF){
if(check())
printf("%s (base %d) = %s (base %d)\n",
v[0],base[0],v[1],base[1]);
else
printf("%s is not equal to %s in any base 2..36\n",
v[0],v[1]);
}
return 0;
}