355 - The Bases Are Loaded

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

dpitts
New poster
Posts: 31
Joined: Tue Jun 17, 2003 10:10 pm

Hmm..

Post by dpitts » Mon Jun 13, 2005 7:22 am

I thought I handled all the special cases.

Code: Select all

#include <iostream>
using namespace std;
char lookup[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main(int argc, char *argv[])
{
        unsigned long long f, t;
        char num[256];
        while (cin >> f >> t >> num) {
                unsigned long long val = 0;
                bool illegal = false;
                bool hasNegative = num[0] == '-';
                for (int i = hasNegative ? 1 : 0; num[i]; i++) {
                        unsigned long long dig = num[i];
                        if (dig < '0' || (dig > '9' && dig < 'A')) {
                                illegal = true;
                                break;
                        }
                        if (dig <= '9')
                                dig -= '0';
                        else
                                dig = (dig & 31) + 9;
                        if (dig >= f) {
                                illegal = true;
                                break;
                        }
                        val = val * f + dig;
                }
                if (!val && hasNegative)
                        hasNegative = false;
                if (illegal) {
                        cout << num << " is an illegal base " << f  << " number" << endl;
                } else {
                        cout << num << " base " << f << " = ";
                        char d[512];
                        char *x = d+512;
                        *--x = 0;
                        do
                        {
                                *--x = lookup[val % t];
                                val /= t;
                        } while (val);
                        if (hasNegative)
                                *--x = '-';
                        cout << x << " base " << t << endl;
                }
        }
        return 0;
}
I still get WA though. I'm only 4 problems from being in the top 1,000, and it frustrates me that such a simple looking problem is causeing grief.

Fadia Ismael
New poster
Posts: 22
Joined: Wed Aug 17, 2005 3:04 pm

355- WA The Bases Are Loaded... please help

Post by Fadia Ismael » Wed Sep 14, 2005 6:18 pm

Hello every body, please, can any one tell me what is the wrong in this code, it gives me WA even I tried it with several tests..

And please, I also need test cases for this problem, please help me, here is the code:

Code: Select all

#include <iostream.h>

unsigned int largest(char a[]){
	int i, largest=-2,temp;
	for(i=0; a[i]!='\0'; i++){
		if(a[i]>='0' && a[i]<='9')
			temp=a[i]-48;
		else
			if(a[i]>='A' && a[i]<='F')
				temp=a[i]-55;
			else{
				largest=temp=-1;
				break;
			}
		if(temp>largest)
			largest=temp;
	}
	return largest;
}

unsigned long int deciValue(char a[],unsigned int b){
	int i;
	unsigned long int result=0;
	for(i=0; a[i]!='\0'; i++){
		if(a[i]>='0' && a[i]<='9')
			result = result*b + (a[i]-48);
		else
			if(a[i]>='A' && a[i]<='F')
				result = result*b + (a[i]-55);
	}
	return result;
}

void newValue(char temp[],unsigned long int n, unsigned int b){
	int i,j,t;
	char a[100000];
	if(n==0){
		temp[0]='0';
		temp[1]='\0';
		return;}
	for (i=0; n!=0; i++){
		t=n%b;
		if(t>=0 && t<=9)
			t=t+48;
		else 
			if (t>=10 && t<=15)
				t=t+55;
		a[i]=t;
		n=n/b;
	}
	i--;
	for (j=0; i>=0; i--,j++)
		temp[j]=a[i];
	temp[j]='\0';

}	


void main(){
	unsigned int b1, b2, lar;
	char number[100000];
	char result[100000];

	while(cin>>b1){
		cin>>b2>>number;
		if(b1<2 || b1>16 || b2<2 || b2>16)
			break;
		lar=largest(number);
		if(lar>=b1 )
			cout<<number<<" is an illegal base "<<b1<<" number"<<endl;
		else
			if(lar>=b2)
				cout<<number<<" is an illegal base "<<b2<<" number"<<endl;
			else{
				newValue(result,deciValue(number,b1), b2);
				cout<<number<<" base "<<b1<<" = "<<result<<" base "<<b2<<endl;
			}
	}
}
Thanks in advance...
It's lack of faith that makes people afraid of meeting challanges, and I believe in myself.

mohsincsedu
Learning poster
Posts: 63
Joined: Tue Sep 20, 2005 12:31 am
Location: Dhaka
Contact:

WA_355_The_base_are_loaded

Post by mohsincsedu » Thu Sep 22, 2005 7:35 pm

Hiiiiiii
May be it's a easy problem!!
but i got WA. Please give me the critical input :cry:

here is my coding:

Code: Select all

:

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

long long conv_ten_base(char *num,long b)
{
	long i,l,j,n;

	long long N;
	N = 0;
	l = strlen(num);
	for(i = l-1,j=0;i>=0;i--,j++)
	{
		n = (num[i]<='9')?(num[i]-'0'):(num[i]-'A'+10);
		N +=(n*pow(b,j));
	}
	return N;
}



char *conversion(long long n, int b)
{
	char num[50];
	int j,i = 0;

	do
	{
		j = n%b;
		num[i++] = (j<10)?(j+'0'):('A'+j-10);
	}while((n/=b)!=0);
	num[i] = '\0';

	int l = i;
	i = 0;
	char temp;
	while(i<l)
	{
		temp = num[i];
		num[i] = num[l-1];
		num[l-1] = temp;
		i++; l--;
	}
	return num;
}


int illegal_check(char *num, int n)
{
	int i,l,d;
	l = strlen(num);
	for(i = 0; i< l;i++)
	{
		d = (num[i]<='9')?(num[i]-'0'):(num[i]-'A'+10);
		if(d>=n)
			return 1;
	}
	return 0;
}

void main()
{
	long N, M;
	char num[50];
	long long number;
	while(scanf("%ld %ld",&N,&M)!=EOF)
	{
		scanf("%s",num);
		if(illegal_check(num,N))
			printf("%s is an illegal base %d number\n",num,N);
		else
		{
			number = conv_ten_base(num,N);
			printf("%s base %ld = %s base %ld\n",num,N,conversion(number,M),M);
		}
	}
}



asr
New poster
Posts: 3
Joined: Tue Sep 20, 2005 8:11 am
Location: IIIT-H

Post by asr » Thu Sep 22, 2005 10:10 pm

2 10 10101
5 3 126
15 11 A4C
16 10 FFFFFFFFFF
16 2 FFFFFFFFFF
2 10 0
10 3 1234567
2 15 1001102001
15 2 3014593F19
10 10 1234567890
16 16 FEDCBA0987
2 3 0
2 10 0
10 2 0
14 8 0

Output i got was :
10101 base 2 = 21 base 10
126 is an illegal base 5 number
A4C base 15 = 1821 base 11
FFFFFFFFFF base 16 = 1099511627775 base 10
FFFFFFFFFF base 16 = 1111111111111111111111111111111111111111 base 2
0 base 2 = 0 base 10
1234567 base 10 = 2022201111201 base 3
1001102001 is an illegal base 2 number
3014593F19 is an illegal base 15 number
1234567890 base 10 = 1234567890 base 10
FEDCBA0987 base 16 = FEDCBA0987 base 16
0 base 2 = 0 base 3
0 base 2 = 0 base 10
0 base 10 = 0 base 2
0 base 14 = 0 base 8

Artikali
Learning poster
Posts: 68
Joined: Wed Sep 21, 2005 5:27 pm

355 WA

Post by Artikali » Tue Nov 22, 2005 9:41 pm

i viewed all forums which consist of this problem and used all tests which given and my code solve all of them correctly, but i have WA please help me
here is my code, my compile is Visual C++

Code: Select all

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
#ifdef ONLINE_JUDGE
#define int64 long long
#else
#define int64 _int64
#endif

char rx[45];
char ch[45];
char sign=' ';
char harf[17]="0123456789ABCDEF";
int checkvalid(char *c,int bs)
{
	int i=0,max=0,k;
	for (i=0;i<strlen(c)-1;i++)
		if (max<c[i]) max=c[i];
	if ('0'<=max && max<='9') k=max-'0'; 
	else k=max-'A'+10;
	if (bs>k) return 1;  
	return 0;
}
int son(char c)
{
	if ('0'<=c && c<='9') return c-'0'; 
	return c-'A'+10;
}
int64 xdec(char *c,int bs)
{
	int i=strlen(c)-1;
	int64 d,k=1;
	if (c[0]=='-') {sign='-'; c[0]='0';}else sign=' ';  
	d=son(c[i])*k;
	i--;
	for (;i!=-1;i--){
		k*=bs;
		d+=son(c[i])*k;
	}
	if (sign=='-') c[0]='-';
   	return d;
}
void reverse()
{
	int i,j;
	for (i=0,j=strlen(rx)-1;i<j;i++,j--){
		rx[i]^=rx[j];
		rx[j]^=rx[i];
		rx[i]^=rx[j];
	}
}
void decx(int64 x,int bs)
{
	int i=0;
	if (x==0) {rx[i]='0';i++;sign=' ';} else
	while (x!=0){
		rx[i]=x%bs;
		x/=bs;
		rx[i]=harf[rx[i]];
		i++;
	}
	if (sign=='-') {
     rx[i]='-';
     i++;
     }
	rx[i]='\0';
	reverse();
}
void convert(int bs1,int bs2,char * c)
{
	int64 d;
	if (checkvalid(c,bs1)){
		d=xdec(c,bs1);
		decx(d,bs2);
		if (d!=0)printf("%s base %d = %s base %d\n",c,bs1,rx,bs2);
		else printf("%s base %d = %s base %d\n",rx,bs1,rx,bs2);

	}
	else cout<<c<<" is an illegal base "<<bs1<<" number"<<endl;
}
int main(){
	int bs1,bs2;
	//freopen("a.in","r",stdin);
	//freopen("a.out","w",stdout);
	while (scanf("%d%d%s",&bs1,&bs2,ch)==3) 
		convert(bs1,bs2,ch);
	return 0;
}

thanks

tosun_ewu
New poster
Posts: 4
Joined: Tue Aug 03, 2004 7:02 am

355 The Bases Are Loaded

Post by tosun_ewu » Thu Feb 02, 2006 10:53 am

Code: Select all


GOT ACC

Last edited by tosun_ewu on Tue Jun 26, 2007 1:57 am, edited 1 time in total.

acmmamun
New poster
Posts: 5
Joined: Tue Jul 19, 2005 1:02 pm
Location: Bangladesh
Contact:

Wait you will get accepted

Post by acmmamun » Thu Feb 02, 2006 12:34 pm

Just wait and see and you will be get accepted
There are nothing to say about me....

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun » Thu Feb 02, 2006 2:23 pm

Try with this input

Code: Select all

10 16 9999999999

ayeshapakhi
Learning poster
Posts: 60
Joined: Sun Apr 16, 2006 7:59 pm

355 getting WA everytime..........

Post by ayeshapakhi » Mon May 29, 2006 8:01 pm

please help..........

/* 355 not accepted*/
#include <stdio.h>
#include <math.h>
#include <string.h>

long toten(char a);
char tento(long a);
void rev(char a[]);

void main()
{
// freopen("input.txt","r",stdin);
long len,frbase,tobase,i,temp,intg,tenval;
char num[1000],final[1000],zero[10]="0";
bool tag;

while((scanf("%ld%ld%s",&frbase,&tobase,num))==3)
{
len=strlen(num);
tag=true;
if( frbase <2 || frbase >16 || tobase <2 || tobase>16) tag=false;
else
{
for(i=len-1; i>=0; i--)
{
temp=toten(num);
if( (num>='0' && num<='9')|| (num>='A'&& num<='F'))tag=true;
else tag=false;
if(temp>=frbase )tag=false;
break;
}
}
if(tag)
{
tenval=0;
for(i=len-1; i>=0; i--)
{
temp=toten(num);
tenval+=temp*(long)pow(frbase,len-1-i);
}
intg=(long)tenval;
for(i=0; ; i++)
{
if(intg==0) break;

final=tento(intg%tobase);
intg/=tobase;
}
if(tenval==0) strcpy(final,zero);
else
{
final='\0';
rev(final);
}
printf("%s base %ld = %s base %ld\n",num,frbase,final,tobase);
}
else printf("%s is an illegal base %ld number\n",num,frbase);
}
}
long toten(char a)
{
switch(a)
{
case 'A': return(10);
break;

case 'B': return(11);
break;

case 'C': return(12);
break;

case 'D': return(13);
break;

case 'E': return(14);
break;

case 'F': return(15);
break;

default : return (long)(a-48);
break;
}
}
char tento(long a)
{
switch (a)
{
case 15: return 'F';
break;

case 14: return 'E';
break;

case 13: return 'D';
break;

case 12: return 'C';
break;

case 11: return 'B';
break;

case 10: return 'A';
break;

default: return (char)(a+48);
break;
}
}
void rev(char a[])
{
long temp=strlen(a);
long i,j;
char newstr[1000];

for(i=0,j=temp-1; i<temp && j>=0; i++,j--) newstr=a[j];
newstr='\0';
strcpy(a,newstr);
}

*********
thanks for any help.....

Aengus
New poster
Posts: 12
Joined: Thu Oct 30, 2003 1:38 pm
Location: St. Petersburg, Russia
Contact:

355: Compile Error

Post by Aengus » Mon Jun 05, 2006 5:36 pm

This code is clearly compiled by my gcc 3.3.4, but I get "Compile Error" response from the Online Judge:

Code: Select all

#include <iostream>
#include <string>

using namespace std;

long long
my_a2i(string s, int base)
{
  long long res = 0;
  int sign = 1;
  unsigned i = 0;

  if (s[0] == '-')
  {
    sign = -1;
    i = 1;
  }

  for (; i < s.size(); i++)
  {
    char c = s[i];
    if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'Z'))
      return 0xFFFFFFFFFFFFll;

    int n = (c >= '0' && c <= '9') ? c - '0' : c - 'A' + 10;

    if (n < 0 || n >= base)
      return 0xFFFFFFFFFFFFll;

    res *= base;
    res += n;
  }

  return res*sign;
}

string
my_i2a(long long n, int base)
{
  string res;

  if (n == 0) return string("0");

  string sign;
  if (n < 0)
  {
    sign = "-";
    n = -n;
  }

  while (n)
  {
    int d = n % base;
    char c = d < 10 ? '0' + d : 'A' + d - 10;

    res += c;

    n /= base;
  }

  for (int i = 0, j = res.size()-1; i < j; i++, j--)
    swap(res[i], res[j]);

  return sign + res;
}

int
main()
{
  int from, to;
  string num;

  while (cin >> from >> to >> num)
  {
    long long n = my_a2i(num, from);

    if (n > 0x10000000000ll)
    {
      cout << num << " is an illegal base " << from << " number\n";
      continue;
    }

    cout << num << " base " << from << " = " << my_i2a(n, to) << " base " << to << endl;
  }

  return 0;
}

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko » Mon Jun 05, 2006 10:51 pm

This is what it says:
Here are the compiler error messages:

04629681_24.c: In function `class string my_i2a(long long int, int)':
04629681_24.c:62: implicit declaration of function `int swap(...)'

Aengus
New poster
Posts: 12
Joined: Thu Oct 30, 2003 1:38 pm
Location: St. Petersburg, Russia
Contact:

Post by Aengus » Mon Jun 05, 2006 10:57 pm

Oh, thanks! So, it just needs

Code: Select all

#include <algorithm>
It's curious that my compiler was quite satisfied with this code without that include.[/code]

Ferdous
New poster
Posts: 26
Joined: Sun Dec 14, 2003 1:17 pm
Location: Dhaka, Bangladesh
Contact:

Problem 355 (The Bases Are Loaded)

Post by Ferdous » Thu Jul 27, 2006 8:33 am

Quoted from problem description:
Each input line will consist of three values. The first value will be a positive integer indicating the base of the number. The second value is a positive integer indicating the base we wish to convert to. The third value is the actual number (in the first base) that we wish to convert. This number will have letters representing any digits higher than 9 and may contain invalid ``digits". It will not exceed 10 characters. Each of the input values on a single line will be separated by at least one space.
In my solutin, I assigned the array size of input string (the third value of one line of input) to MAX_LEN+1 (10+1) and all other array sizes large enough to fit and not dependent on MAX_LEN. I got WA.

Then just in a desperate attempt, I changed MAX_LEN to 13 and it got AC!

Doesn't that mean the inconsistency between the input set and the problem description? The third value might be more than 10 characters long!? If that is not the case, what could be the problem with my solution?

Any idea?


With best regards.
Ferdous
I am destined to live on this cruel world........

User avatar
algoJo
New poster
Posts: 37
Joined: Sun Dec 17, 2006 9:02 am

Post by algoJo » Fri Mar 02, 2007 4:57 pm

it seems to be Ok, but the judges said WA...
can anyone give me some I/O set or some hints?

Code: Select all


#include<string.h>
#include<stdio.h>
#define MAX 200
char arr[MAX];
int I=MAX-1;

long long int standar(char line[],long long int X){
	int len,nom;
	long long int sum=0,temp,x,y;
	int i,k;
	len=strlen(line);
	nom=len-1;
	for(i=0;i<len;i++)
	{
		y=1;
		if(line[i]>='0'&&line[i]<='9')
		{
		x=line[i]-'0';
		for(k=0;k<nom;k++) y*=X;
		x*=y;
		sum+=x;
		nom--;
		}
		else
		{
			x=line[i]-'A';
			x+=10;
			for(k=0;k<nom;k++) y*=X;
			x*=y;
			sum+=x;
			nom--;
		}
	}
return sum;
}

void compute(long long int G,long long int N){
	long long int temp;
	char L;
	while(G)
	{
		temp=G%N;
		if(temp>=10)
		{
			temp%=10;
			L=temp+'A';
			arr[I]=L;
		}
		else arr[I]=temp+'0';
		G/=N;
		I--;
	}
}

void print(char line[],long long int asal,long long int N){
	int i;
	printf("%s base %lld = ",line,asal);
	for(i=I+1;i<MAX;i++)
		printf("%c",arr[i]);
	printf(" base %lld\n",N);
}

int Check(char line[],long long int asal){
	char L;
	int l,len,i;
	len=strlen(line);
	if(asal<=10)
	{
		L=asal-1+'0';
		for(i=0;i<len;i++)
			if(line[i]>='0'&&line[i]<=L);
			else return 0;
		return 1;
	}
	else
	{
		asal--;
		asal%=10;
		L=asal+'A';
		for(i=0;i<len;i++)
			if(line[i]>='0'&&line[i]<='9'&&line[i]>='A'&&line[i]<=L);
			else return 1;
	}
	return 1;
}

void main(){
	char line[MAX];
	long long int asal,N,i;
	long long int G;
	while(scanf("%lld %lld %s",&asal,&N,line)==3)
	{
		I=MAX-1;
		if(!strcmp(line,"0")) 	printf("%s base %lld = 0 base %lld\n",line,asal,N);
		else
		if(Check(line,asal))
		{
			memset(arr,'0',sizeof(arr));
			G=standar(line,asal);
			compute(G,N);
			print(line,asal,N);
		}
		else printf("%s is an illegal base %lld number\n",line,asal);
	}
}

User avatar
jainal cse du
New poster
Posts: 23
Joined: Thu Jul 27, 2006 2:43 pm
Location: University of Dhaka,Bangladesh

[size=24]I am't uderstanding why judge esponse is WA [/size]

Post by jainal cse du » Sat Mar 17, 2007 2:07 pm

removed
Last edited by jainal cse du on Sun Mar 18, 2007 12:16 pm, edited 1 time in total.

Post Reply

Return to “Volume 3 (300-399)”