343 - What Base Is This?
Moderator: Board moderators
-
- Learning poster
- Posts: 99
- Joined: Sun Apr 06, 2003 5:53 am
- Location: Dhaka, Bangladesh
- Contact:
First of all,
You should change your output format. I believe
instead of printing "12(base2)" you should print "12 (base 2)"
And for some strange reasons the AC output for
0 0
seems to be
0 (base 2) = 0 (base 2)
I got accepted after I initialized your max to be '1'
[ Although as far as I am concerned I think the correct output should be 0 (base 1) = 0 (base 1) ]
You should change your output format. I believe
instead of printing "12(base2)" you should print "12 (base 2)"
And for some strange reasons the AC output for
0 0
seems to be
0 (base 2) = 0 (base 2)
I got accepted after I initialized your max to be '1'
[ Although as far as I am concerned I think the correct output should be 0 (base 1) = 0 (base 1) ]
Where's the "Any" key?
343 Compile Error
Compile Error
Thanks
Code: Select all
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#define lenr 9
using namespace std;
int findminbase(char * c)
{
int i=0,max=0;
while (c[i]!='\0'){
if (max<c[i]) max=c[i];
i++;
}
if (('0'<=max)&&(max<='9')) return max-'0'+1;
return max-'A'+11;
}
int son(char c)
{
if (('0'<=c)&&(c<='9')) return c-'0';
return c-'A'+10;
}
long xdec(char *c,int ds)
{
int d=0,k=1;
d+=k*son(c[strlen(c)-1]);
for (int i=strlen(c)-2; i>=0;i--){
k=k*ds;
d+=k*son(c[i]);
}
return d;
}
int main(){
char a[lenr],b[lenr];
while (scanf("%s%s",a,b)==2){
for (int i=findminbase(a);i<36;i++)
for(int j=findminbase(b);j<36;j++)
if (xdec(a,i)==xdec(b,j)){
cout<<a<<" (base "<<i<<") = "<<b<<" (base "<<j<<")"<<endl;
i=37; j=37;
}
if (i!=37) cout<<a<<" is not equal to "<<b<<" in any base 2..36"<<endl;
}
return 0;
}
-
- Experienced poster
- Posts: 161
- Joined: Tue Oct 25, 2005 8:38 pm
- Location: buet, dhaka, bangladesh
this line got compile error in my compiler, the reason is very clear. variable 'i' is not declared !!! 'i' is declared in the for loop, so 'i' is recognized only in the scope of the for loop, not outside anywhere. so 'i' must be declared in the beginning of the main function. it is always a very bad practice to declare any variable in a loop.if (i!=37) cout<<a<<" is not equal to "<<b<<" in any base 2..36"<<endl;
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
----------------
the world is nothing but a good program, and we are all some instances of the program
-
- New poster
- Posts: 4
- Joined: Mon Jun 27, 2005 5:27 pm
343 WA
Please help me!
Can anyone give me some samples?
Here is my code.
Can anyone give me some samples?
Here is my code.
Code: Select all
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count(char input[],int base,int len){
int i=len-1,j=1;
int result;
result=(input[i]-'0');
if(input[i]-'0'>=base) return -1;
for(i=len-2;i>=0;i--){
if(input[i]-'0'>=base) return -1;
result+=(input[i]-'0')*base;
base*=base;
}
return result;
}
int main(){
char x[100],y[100];
int i,j,k,flag,num1,num2,len_x,len_y;
while(scanf("%s%s",&x,&y)!=EOF){
len_x=strlen(x);
len_y=strlen(y);
for(i=0;i<strlen(x);i++){
if(x[i]>='A'&&x[i]<='Z') x[i]=x[i]-'A'+'9'+1;
}
for(i=0;i<strlen(y);i++){
if(y[i]>='A'&&y[i]<='Z') y[i]=y[i]-'A'+'9'+1;
}
for(i=2;i<=36;i++){
num1=count(x,i,len_x);
if(num1==-1) continue;
flag=0;
for(j=2;j<=36;j++){
num2=count(y,j,len_y);
if(num2==-1) continue;
if(num1==num2){
for(k=0;k<len_x;k++){
if(x[k]>'9') x[k]=x[k]+'A'-'9'-1;
}
for(k=0;k<len_y;k++){
if(y[k]>'9') y[k]=y[k]+'A'-'9'-1;
}
printf("%s (base %d) = %s (base %d)\n",x,i,y,j);
flag=1;
break;
}
}
if(flag==1) break;
}
if(flag==0){
for(k=0;k<len_x;k++){
if(x[k]>'9') x[k]=x[k]+'A'-'9'-1;
}
for(k=0;k<len_y;k++){
if(y[k]>'9') y[k]=y[k]+'A'-'9'-1;
}
printf("%s is not equal to %s in any base 2..36\n",x,y);
}
}
return 0;
}
343 - what base is this RE, why??
can you tell me, why am i getting re??
can you make sure, is there negetive inputs for this prob?
thanks in advance
can you make sure, is there negetive inputs for this prob?
thanks in advance
Code: Select all
/*
* 343 what base is this?
* submission 1 RE
* coded at 12:02 on 29th dec 05
*
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
//cut heading zero
void cut_zero(char *array) {
int len,i;
while(array[0]=='0' && strlen(array)!=1) {
len=strlen(array);
for(i=1;i<len;i++)
array[i-1]=array[i];
array[len-1]='\0';
}
}
//function to determine the value of a single part
int return_value(char ch) {
if(ch>='0' && ch<='9')
return (ch-'0');
if(ch>='A' && ch<='Z')
return (ch-'A'+10);
return -1;
}
//change base - a 'input' of base 'n' is turned into a decimal value
long change_base(char *input, int n) {
int len;
int i,j,temp;
long value=0;
len=strlen(input);
for(i=len-1,j=0;i>=0;i--,j++) {
temp=return_value(input[i]);
if(temp>=n || temp==-1) return -1;
value+=(long)(temp*pow(n,j));
}
return value;
}
int main() {
char a[20],b[20];
int i,j;
long value1,value2;
char flag;
while(scanf("%s %s", a, b) ==2) {
flag=1;
cut_zero(a);
cut_zero(b);
for(i=2;i<=36 && flag;i++)
for(j=2;j<=36 && flag;j++) {
value1=change_base(a,i);
value2=change_base(b,j);
if(value1==-1 || value2==-1) continue;
if(value1==value2) {
printf("%s (base %d) = %s (base %d)\n",a,i,b,j);
flag=0;
}
}
if(flag==1) printf("%s is not equal to %s in any base 2..36\n",a,b);
}
return 0;
}
fahim
#include <smile.h>
#include <smile.h>
-
- Experienced poster
- Posts: 154
- Joined: Sat Apr 17, 2004 9:34 am
- Location: EEE, BUET
Nothing really serious,
. Change
to

Code: Select all
char a[20],b[20];
Code: Select all
char a[50],b[50];
You should never take more than you give in the circle of life.
343 - RTE
Why this is RTE?
#include <stdio.h>
#include <math.h>
#include <string.h>
int min(char *s);
unsigned decimal(char *s, int r);
int min(char *s) {
int max = 0;
for (int i = 0; s != '\0'; i++) {
if (s > max) {
max = s;
}
}
if (max <= '9') {
return max - '0' + 1;
} else {
return max - 'A' + 11;
}
}
unsigned decimal(char *s, int r) {
unsigned sum = 0;
int i,j;
for (i = strlen(s) - 1, j = 0; i >= 0; i--,j++) {
if (s <= '9') {
sum += (s - '0') * (pow(r,j));
} else {
sum += (s - 'A' + 10) * (pow(r,j));
}
}
return sum;
}
void main() {
char X[25], Y[25];
int min_x, min_y, i, j;
unsigned temp;
//bool flag;
//freopen("H:\\contest\\in.txt","r",stdin);
while (scanf("%s%s",X,Y) == 2) {
min_x = min(X);
min_y = min(Y);
//printf("%d %d\n",min_x,min_y);
/*if (min_x == 1) {
min_x = 2;
}
if (min_y == 1) {
min_y = 2;
}*/
for (i = min_x; i <= 36; i++) {
temp = decimal(X,i);
for (j = min_y; j <= 36; j++) {
if (decimal(Y,j) == temp) {
break;
}
}
if (j <= 36) {
break;
}
}
if (i <= 36) {
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);
}
}
}
#include <stdio.h>
#include <math.h>
#include <string.h>
int min(char *s);
unsigned decimal(char *s, int r);
int min(char *s) {
int max = 0;
for (int i = 0; s != '\0'; i++) {
if (s > max) {
max = s;
}
}
if (max <= '9') {
return max - '0' + 1;
} else {
return max - 'A' + 11;
}
}
unsigned decimal(char *s, int r) {
unsigned sum = 0;
int i,j;
for (i = strlen(s) - 1, j = 0; i >= 0; i--,j++) {
if (s <= '9') {
sum += (s - '0') * (pow(r,j));
} else {
sum += (s - 'A' + 10) * (pow(r,j));
}
}
return sum;
}
void main() {
char X[25], Y[25];
int min_x, min_y, i, j;
unsigned temp;
//bool flag;
//freopen("H:\\contest\\in.txt","r",stdin);
while (scanf("%s%s",X,Y) == 2) {
min_x = min(X);
min_y = min(Y);
//printf("%d %d\n",min_x,min_y);
/*if (min_x == 1) {
min_x = 2;
}
if (min_y == 1) {
min_y = 2;
}*/
for (i = min_x; i <= 36; i++) {
temp = decimal(X,i);
for (j = min_y; j <= 36; j++) {
if (decimal(Y,j) == temp) {
break;
}
}
if (j <= 36) {
break;
}
}
if (i <= 36) {
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);
}
}
}
-
- New poster
- Posts: 2
- Joined: Sun Jun 04, 2006 3:39 pm
- Location: Bangladesh
- Contact:
343 wrong answer
hi, i'm a new comer in acm solving : please help
problem: getting wrong answer although my code is filling the requirement of sample i/o; Code given bellow:
#include<stdio.h>
#include<string.h>
#include<math.h>
//allowable digits
char digits[] =
{'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'};
//no of digits
//base coversion and return equivalent decimal value;
long toBase(char *number, int base);
int main(){
char number1[100],number2[100];
char max_digit;
long num1,num2;
int len_num1, len_num2, i, j, base_num1, base_num2, matchFlag;
while(scanf("%s%s",number1,number2) == 2){
//if given inputs are same
//if(strcmp(number1,number2) == 0){
//printf
//
//
// continue;
//}
len_num1 = strlen(number1);
len_num2 = strlen(number2);
//checking for nondigit number
/*i = 0;
flag_num1 = 0;
while(number1 >= '0' && number1 <= '9') i++;
if(i != len_num1) flag_num1 = 1;
i = 0;
flag_num2 = 0;
while(number2 >= '0' && number1 <= '9') i++;
if(i != len_num2) flag_num2 = 1;*/
//lowest base for a number = highest digit in the number + 1
max_digit = number1[0];
for(i = 0; i < len_num1; i++)
if(max_digit < number1)
max_digit = number1;
//finding the max digit's index from digits[]
i = 0;
while(max_digit != digits){ i++;}
base_num1 = i + 1;
max_digit = number2[0];
for(i = 0; i < len_num2; i++)
if(max_digit < number2)
max_digit = number2;
i = 0;
while(max_digit != digits){ i++;}
base_num2 = i + 1;
//comparing numbers
for(i = base_num1; i <= 36; i++){
matchFlag = 0;
num1 = toBase(number1, i);
for(j = base_num2; j <= 36; j++){
num2 = toBase(number2, j);
if(num1 == num2){
printf("%s (base %d) = %s (base %d)\n",number1,i,number2,j);
matchFlag = 1;
break;
}
}
if(matchFlag == 1) break;
}
if(matchFlag == 0){
printf("%s is not equal to %s in any base 2..36\n",number1,number2);
}
}
return 0;
}
long toBase(char *number, int base){
int len = strlen(number);
int i, j, k, tempNumber = 0;
for(i = len, j = 0; i > 0; i--, j++){
k = 0;
//finding digits
while(number[j] != digits[k]) {k++ ;}
//having value
tempNumber += k * int(pow(base, i - 1));
}
return tempNumber;
}
please reply
problem: getting wrong answer although my code is filling the requirement of sample i/o; Code given bellow:
#include<stdio.h>
#include<string.h>
#include<math.h>
//allowable digits
char digits[] =
{'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'};
//no of digits
//base coversion and return equivalent decimal value;
long toBase(char *number, int base);
int main(){
char number1[100],number2[100];
char max_digit;
long num1,num2;
int len_num1, len_num2, i, j, base_num1, base_num2, matchFlag;
while(scanf("%s%s",number1,number2) == 2){
//if given inputs are same
//if(strcmp(number1,number2) == 0){
//printf
//
//
// continue;
//}
len_num1 = strlen(number1);
len_num2 = strlen(number2);
//checking for nondigit number
/*i = 0;
flag_num1 = 0;
while(number1 >= '0' && number1 <= '9') i++;
if(i != len_num1) flag_num1 = 1;
i = 0;
flag_num2 = 0;
while(number2 >= '0' && number1 <= '9') i++;
if(i != len_num2) flag_num2 = 1;*/
//lowest base for a number = highest digit in the number + 1
max_digit = number1[0];
for(i = 0; i < len_num1; i++)
if(max_digit < number1)
max_digit = number1;
//finding the max digit's index from digits[]
i = 0;
while(max_digit != digits){ i++;}
base_num1 = i + 1;
max_digit = number2[0];
for(i = 0; i < len_num2; i++)
if(max_digit < number2)
max_digit = number2;
i = 0;
while(max_digit != digits){ i++;}
base_num2 = i + 1;
//comparing numbers
for(i = base_num1; i <= 36; i++){
matchFlag = 0;
num1 = toBase(number1, i);
for(j = base_num2; j <= 36; j++){
num2 = toBase(number2, j);
if(num1 == num2){
printf("%s (base %d) = %s (base %d)\n",number1,i,number2,j);
matchFlag = 1;
break;
}
}
if(matchFlag == 1) break;
}
if(matchFlag == 0){
printf("%s is not equal to %s in any base 2..36\n",number1,number2);
}
}
return 0;
}
long toBase(char *number, int base){
int len = strlen(number);
int i, j, k, tempNumber = 0;
for(i = len, j = 0; i > 0; i--, j++){
k = 0;
//finding digits
while(number[j] != digits[k]) {k++ ;}
//having value
tempNumber += k * int(pow(base, i - 1));
}
return tempNumber;
}
please reply

-
- Experienced poster
- Posts: 161
- Joined: Tue Oct 25, 2005 8:38 pm
- Location: buet, dhaka, bangladesh
try your program with the sample i/o below:
http://online-judge.uva.es/board/viewto ... hlight=343
if you use the "search" option, you can find all the previous posts about any problem.
http://online-judge.uva.es/board/viewto ... hlight=343
if you use the "search" option, you can find all the previous posts about any problem.
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
----------------
the world is nothing but a good program, and we are all some instances of the program
-
- New poster
- Posts: 2
- Joined: Sun Jun 04, 2006 3:39 pm
- Location: Bangladesh
- Contact:
Thanks
thanks
I got AC. But why they don't use base 1 whereas only one number (0) there!!!!!!!!!!!!!!!!

Problem in 343
Can anyone tell me what the problem in my following code.
I am not understanding the hidden error.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define sz 200
int minimum_base(char base[]);
unsigned long anyBase_decimal(char ch[],int base);
void main()
{
char base1[sz],base2[sz];
int min_base1,min_base2,i,j;
unsigned long num1,num2,p;
while(scanf(" %s %s",base1,base2)==2)
{
min_base1 = minimum_base(base1);
min_base2 = minimum_base(base2);
for(i=min_base1;i<=36;i++)
{
p=0;
num1 = anyBase_decimal(base1,i);
for(j=min_base2;j<=36;j++)
{
num2 = anyBase_decimal(base2,j);
if(num1==num2)
{
printf("%s (base %d) = %s (base %d)\n",base1,i,base2,j);
p=1;
break;
}
}
if(p==1)break;
}
if(p!=1)
printf("%s is not equal to %s in any base 2..36\n",base1,base2);
}
}
int minimum_base(char base[])
{
int n,max=1,p,i;
p= strlen(base);
for(i=0;i<p;i++)
{
if(base>=48 && base<=57)
n = (int)base - 48;
else
n = (int)base - 55;
if(n>max)max = n;
}
return (max+1);
}
unsigned long anyBase_decimal(char ch[],int base)
{
unsigned long num;
char *start,*end;
start=(char*)malloc(sz);
start=ch;
while(*start)
{
//Move past the non number char
while( *start && !isdigit(*start) && *start!='-'
&& !(*start>='A' && *start<='A'+base-11) )
start++;
if(*start)
{
//Store decimal equivalent of (base) base number
//Store remaining string to (end)
num=strtol(start,&end,base);
start=end;
//printf("%ld\n",num);
}
}
return num;
}
I am not understanding the hidden error.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define sz 200
int minimum_base(char base[]);
unsigned long anyBase_decimal(char ch[],int base);
void main()
{
char base1[sz],base2[sz];
int min_base1,min_base2,i,j;
unsigned long num1,num2,p;
while(scanf(" %s %s",base1,base2)==2)
{
min_base1 = minimum_base(base1);
min_base2 = minimum_base(base2);
for(i=min_base1;i<=36;i++)
{
p=0;
num1 = anyBase_decimal(base1,i);
for(j=min_base2;j<=36;j++)
{
num2 = anyBase_decimal(base2,j);
if(num1==num2)
{
printf("%s (base %d) = %s (base %d)\n",base1,i,base2,j);
p=1;
break;
}
}
if(p==1)break;
}
if(p!=1)
printf("%s is not equal to %s in any base 2..36\n",base1,base2);
}
}
int minimum_base(char base[])
{
int n,max=1,p,i;
p= strlen(base);
for(i=0;i<p;i++)
{
if(base>=48 && base<=57)
n = (int)base - 48;
else
n = (int)base - 55;
if(n>max)max = n;
}
return (max+1);
}
unsigned long anyBase_decimal(char ch[],int base)
{
unsigned long num;
char *start,*end;
start=(char*)malloc(sz);
start=ch;
while(*start)
{
//Move past the non number char
while( *start && !isdigit(*start) && *start!='-'
&& !(*start>='A' && *start<='A'+base-11) )
start++;
if(*start)
{
//Store decimal equivalent of (base) base number
//Store remaining string to (end)
num=strtol(start,&end,base);
start=end;
//printf("%ld\n",num);
}
}
return num;
}
-
- Learning poster
- Posts: 56
- Joined: Tue Jun 13, 2006 5:18 pm
- Location: (CSE, SUST) Sylhet, Bangladesh
- Contact:
343 Why WA, Why? I am tired of..
I am disapointing about this problem. I tried almost all test case whick i got from the board but for those test case my code is giving correct answer, but it's still getting WA!
Wheres wrong with my code? Anybody help plz..
here is my code
Wheres wrong with my code? Anybody help plz..
here is my code
Code: Select all
Removed after got AC
Last edited by Mushfiqur Rahman on Sat Mar 03, 2007 3:06 pm, edited 1 time in total.
-
- New poster
- Posts: 11
- Joined: Sat Jul 22, 2006 8:45 pm
- Location: Sylhet, Bagladesh
- Contact: