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

kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:

355 - WA passes all dataset in this forum,but....anyone help

Post by kbr_iut »

hey guyes my code passes all input set of this forum but I am still getting wA
I skipped leading 0's and used unsigned long long datatype..
pliz any one help...here is my code

Code: Select all

Removed
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 355 - The Bases Are Loaded

Post by Jan »

Don't use pow or other functions that use floating point calculations. Write manual functions if needed (only integer calculations are needed!).
Ami ekhono shopno dekhi...
HomePage

kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:

Re: 355 - jan vi, avoid pow(),but (still WA)

Post by kbr_iut »

code removed. now AC
Last edited by kbr_iut on Wed Apr 23, 2008 6:13 pm, edited 1 time in total.
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 355 - The Bases Are Loaded

Post by Jan »

There are no leading zeros. So, this is not a problem. The problem is your code doesn't work for 0. Just think that what should be the output of '2 3 0'. Your code is definitely wrong in this case.
Ami ekhono shopno dekhi...
HomePage

kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:

Re: 355 - The Bases Are Loaded

Post by kbr_iut »

jan vi , u r really u....thanx,now AC
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................

maruf
New poster
Posts: 17
Joined: Sat May 24, 2008 6:00 pm

Re: 355 - The Bases Are Loaded

Post by maruf »

getting WA.plz help

Code: Select all

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


int main()
{
	long  long int a,i,k,n,m;
	 long long int p,q;
	 long long int y[5000];
	char x[5000];
	while(scanf("%lld",&p)!=EOF)
	{
	
	scanf("%lld",&q);
	scanf("%s",x);
	
	n=strlen(x);
	int f=1;
	for(i=0;i<n;i++)
	{
		if(x[i]>='A' && x[i]<='F')
			m=x[i]-55;
		else
			m=x[i]-48;
		if(m>=p )
		{
			printf("%s is an illegal base %lld number\n",x,p);
			f=0;
		    break;
		}
	}
	if(f==1)
	{
	printf("%s base %lld = ",x,p);
	if(n==1 && x[0]==0)
		printf("0 base %lld\n",q);
	else
	{
	
	int sum=0;
	
	for(i=n-1,k=0;i>=0;k++)
	{
		if(x[i]>='A' && x[i]<='F')
			sum+=(x[i]-55)*pow(p,k);
		   
		else
			sum+=(x[i]-48)*pow(p,k);
		i=i-1;
	}


	p=sum;
    int	m=0;
	for( ;p!=0;p=p/q)
			y[m++]=p%q;
	for(i=m-1;i>=0;i--)
		{
			if(y[i]>=10 && y[i]<=15)
				printf("%c",y[i]+55);
			else
				printf("%lld",y[i]);
		}
	printf(" base %lld\n",q);
	}
	}
	}
	return 0;
}
lives for eternity......

Nishith csedu 1448
New poster
Posts: 4
Joined: Fri May 23, 2008 6:55 am
Location: Dhaka
Contact:

Re: 355 - The Bases Are Loaded

Post by Nishith csedu 1448 »

Maruf check this input..........

Code: Select all

2 10 0
output:

Code: Select all

0 base 2 = 0 base 10 
Hope you will get acc soon. :D :D
Sopno dhaka mon akash choar.............

maruf
New poster
Posts: 17
Joined: Sat May 24, 2008 6:00 pm

Re: 355 - The Bases Are Loaded

Post by maruf »

still WA...
should i use array for generating output??
lives for eternity......

sunahian
New poster
Posts: 2
Joined: Sat Jul 12, 2008 9:32 pm

355-wrong answer

Post by sunahian »

I have checked all the possible case. but I have gotten wrong answer.please any one can help me.
here is the code.

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
int main()
{
	long long int i,j,k,l,n,m,b[1000],t,flag,index,slag,p,u,v;

	char a[1000],c[1000];
	while(scanf("%lld%lld%s",&n,&m,a)==3) 
	{

		l=strlen(a);
		slag=0;


		for(i=0,j=0;i<l;i++)
		{
			 
			if(isdigit(a[i]))
			{
				b[j]=a[i]-48;
				j++;

			}
			else if(isalpha(a[i]))
			{
				b[j]=a[i]-55;
				j++;
			}

	

			else
			{
				slag=1;
				printf("%s is an illegal base %lld number\n",a,n);
				break;
			}
		}
		if(slag==1)
			continue;
		flag=0;




		for(i=0;i<j;i++)
		{
			if(b[i]>=n)
			{
				printf("%s is an illegal base %lld number\n",a,n); 
				flag=1;
			}
		}
		if(flag==1)
			continue;
		p=0;
		index=0;



		for(i=0;i<j;i++)
		{
			p=(p*n)+b[i];
			
		}
		
	

		for(;;)
		{
			u=p%m;
			v=p-u;
			p=v/m;
			if(u<=9)
			{
				c[index]=u+48;
				index++;
			}
			else if(u>=10)
			{
				c[index]=u+55;
				index++;
			}
			if(p<=0)
				break;
		}
		c[index]='\0';



		printf("%s base %lld = ",a,n);
		for(i=index-1;i>=0;i--)
			printf("%c",c[i]);
		printf(" base %lld\n",m);
	}
	return 0;
}


sunahian
New poster
Posts: 2
Joined: Sat Jul 12, 2008 9:32 pm

Re: 355-The Bases Are Loaded

Post by sunahian »

I have checked all the possible case. but I have gotten wrong answer.please any one can help me.
here is the code.

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
int main()
{
   long long int i,j,k,l,n,m,b[1000],t,flag,index,slag,p,u,v;

   char a[1000],c[1000];
   while(scanf("%lld%lld%s",&n,&m,a)==3) 
   {

      l=strlen(a);
      slag=0;


      for(i=0,j=0;i<l;i++)
      {
          
         if(isdigit(a[i]))
         {
            b[j]=a[i]-48;
            j++;

         }
         else if(isalpha(a[i]))
         {
            b[j]=a[i]-55;
            j++;
         }

   

         else
         {
            slag=1;
            printf("%s is an illegal base %lld number\n",a,n);
            break;
         }
      }
      if(slag==1)
         continue;
      flag=0;




      for(i=0;i<j;i++)
      {
         if(b[i]>=n)
         {
            printf("%s is an illegal base %lld number\n",a,n); 
            flag=1;
         }
      }
      if(flag==1)
         continue;
      p=0;
      index=0;



      for(i=0;i<j;i++)
      {
         p=(p*n)+b[i];
         
      }
      
   

      for(;;)
      {
         u=p%m;
         v=p-u;
         p=v/m;
         if(u<=9)
         {
            c[index]=u+48;
            index++;
         }
         else if(u>=10)
         {
            c[index]=u+55;
            index++;
         }
         if(p<=0)
            break;
      }
      c[index]='\0';



      printf("%s base %lld = ",a,n);
      for(i=index-1;i>=0;i--)
         printf("%c",c[i]);
      printf(" base %lld\n",m);
   }
   return 0;
}


I have checked all the possible case. but I have gotten wrong answer.please any one can help me.
here is the code.

[code]
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
int main()
{
long long int i,j,k,l,n,m,b[1000],t,flag,index,slag,p,u,v;

char a[1000],c[1000];
while(scanf("%lld%lld%s",&n,&m,a)==3) 
{

l=strlen(a);
slag=0;


for(i=0,j=0;i<l;i++)
{

if(isdigit(a[i]))
{
b[j]=a[i]-48;
j++;

}
else if(isalpha(a[i]))
{
b[j]=a[i]-55;
j++;
}



else
{
slag=1;
printf("%s is an illegal base %lld number\n",a,n);
break;
}
}
if(slag==1)
continue;
flag=0;




for(i=0;i<j;i++)
{
if(b[i]>=n)
{
printf("%s is an illegal base %lld number\n",a,n); 
flag=1;
}
}
if(flag==1)
continue;
p=0;
index=0;



for(i=0;i<j;i++)
{
p=(p*n)+b[i];

}



for(;;)
{
u=p%m;
v=p-u;
p=v/m;
if(u<=9)
{
c[index]=u+48;
index++;
}
else if(u>=10)
{
c[index]=u+55;
index++;
}
if(p<=0)
break;
}
c[index]='\0';



printf("%s base %lld = ",a,n);
for(i=index-1;i>=0;i--)
printf("%c",c[i]);
printf(" base %lld\n",m);
}
return 0;
}

[/code]

zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

355 - Clarification

Post by zyxw »

I got AC after 6 WA :)
I would like to make few clarifications so tat it might help others:

1. As said by others, there are no negative inputs.

2. The problem states that, the number to be converted may contain invalid digits.
But, it may contain invalid characters too!

Example:

Code: Select all

10 2 A4
Here A4 is invalid, as a number with base 10 can have digits from '0' to '9' only.
I didn't consider this case initially.

3. Another reason behind WA (for me) was precision error.
Then I used 'int' to take input for both the bases.
For pow() function, i used like this:

Code: Select all

(long long int) pow( (double)base1, i) );
I am not totally useless, because I can still be used as a bad example :P

athanhcong
New poster
Posts: 1
Joined: Tue Oct 28, 2008 6:07 pm

Help me! Why Time Limit Exceeded

Post by athanhcong »

This is the first time I submit my code to UVA.
After many times Wrong Answer, I change my input from
scanf("%d %d %s", &oriBase, &newBase, &oriValue);
to
while (scanf("%d %d %s", &oriBase, &newBase, &oriValue))
And get TLE not WA, enjoy, but I can't find why's my code TLE.
Please help me. here my code.

Code: Select all

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int max= 255;

int check(char* num, int fromBase)
{
    int i, temp;
    /* ok= 1;*/
    int numlen= strlen(num);
    for (i= 0; i< numlen; i++)
    {
        if ((num[i]>= '0') && (num[i]<= '9'))
            num[i]= num[i]- '0';
        else if ((num[i]>= 'A') && (num[i]<= 'F'))
            num[i]= num[i]- 'A' + 10;
        else
            return 0;
        if (num[i]>= fromBase) return 0;
    }
    return 1;
}

unsigned long toBase10(char num[max], int numlen, int fromBase) {
    unsigned long result= 0; int i;
    /*printf("%s %d\n", num, numlen);*/

    for (i= 0; i< numlen; i++)
        result= result* fromBase + num[i];
    return result;
}
void toBase(unsigned long num, int base, char* result) {
    char temp[max];
    int i= 0, j;
    do
    {
        temp[i++]= num % base;
        num= num / base;
    }while (num!= 0);
    /*            */
    i--;
    for (j= 0; j<= i; j++)
    {
        if ((temp[i -j] >= 10))
            result[j]= temp[i-j] - 10 + 'A';
        else
            result[j]= temp[i-j] + '0';
    }
    result[j]= '\0';
}

int main()
{
    int oriBase, newBase;
    char oriValue[max], newValue[max];
    int oriNumLen;
    unsigned long base10Value;

    while (scanf("%d %d %s", &oriBase, &newBase, &oriValue))
    {
    /*printf("%d %d %s\n", oriBase, newBase, oriValue);*/
    oriNumLen= strlen(oriValue);
    printf("%s", oriValue);
    if (check(oriValue, oriBase))
    {
        base10Value= toBase10(oriValue, oriNumLen, oriBase);
        /*printf("\n%lu <- Base 10 Value\n", base10Value);*/
        toBase(base10Value, newBase, newValue);
        printf(" base %d = %s base %d\n", oriBase, newValue, newBase);
    }

    else
        printf (" is an illegal base %d number\n", oriBase);
    }
    return 0;
}

L I M O N
Learning poster
Posts: 58
Joined: Wed Dec 31, 2003 8:43 am
Location: Dhaka, Bangladesh
Contact:

Re: 355 - The Bases Are Loaded

Post by L I M O N »

Try to fix your bug here http://www.youngprogrammer.com

Fluffymoo
New poster
Posts: 7
Joined: Wed Oct 15, 2008 5:05 pm

Re: 355 - The Bases Are Loaded

Post by Fluffymoo »

Try to change
while (scanf("%d %d %s", &oriBase, &newBase, &oriValue))
to
while (scanf("%d %d %s", &oriBase, &newBase, &oriValue)==3)

Jehad Uddin
Learning poster
Posts: 74
Joined: Fri May 08, 2009 5:16 pm

Re: 355 The Bases Are Loaded

Post by Jehad Uddin »

prob no 355.Can anybody help me,i m getting WA for this code.

Deleted after ac...

Post Reply

Return to “Volume 3 (300-399)”