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

layesh
New poster
Posts: 5
Joined: Wed Jun 10, 2009 10:13 pm

355 F1 F1

Post by layesh »

can anyone tell me why it shows wrong answer

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

using namespace std;
char s1[10000000];


int main()
{
int m,t,i,k,a,b,p,q,w[10000];
long int l,j,n;
freopen("355.txt","r",stdin);
while(scanf("%d %d %s",&a,&b,&s1)==3)
{
m=strlen(s1);
i=m;
l=0;
for(k=0;k<m;k++)
{
n=pow(a,i-1);
if(((int(s1[k])-48)>=a && s1[k]!='A' && s1[k]!='B' && s1[k]!='C' && s1[k]!='D' && s1[k]!='D' && s1[k]!='F')||((int(s1[k])-55)>=a))
{
printf("%s is an illegal base %d number\n",s1,a);
l=0;
break;
}
if(s1[k]=='A')j=10*n;
else if(s1[k]=='B')j=11*n;
else if(s1[k]=='C')j=12*n;
else if(s1[k]=='D')j=13*n;
else if(s1[k]=='E')j=14*n;
else if(s1[k]=='F')j=15*n;
else j=(s1[k]-48)*(n);
i--;
l=l+j;
}
if(b==10&&l!=0)
printf("%s base %d = %ld base %d\n",s1,a,l,b);
else if(l!=0)
{
q=0;
while((l/b)>=1)
{
p=l%b;
l=l/b;
w[q]=p;
q++;
}
p=l%b;
w[q]=p;
printf("%s base %d = ",s1,a);
for(t=q;t>=0;t--)
{
if(w[t]==10)
printf("A");
else if(w[t]==11)
printf("B");
else if(w[t]==12)
printf("C");
else if(w[t]==13)
printf("D");
else if(w[t]==14)
printf("E");
else if(w[t]==15)
printf("F");
else
printf("%d",w[t]);
}
printf(" base %d\n",b);
}
}
return 0;

}

asif_khan_ak_07
New poster
Posts: 25
Joined: Fri Apr 17, 2009 8:24 am

Re: 355 - The Bases Are Loaded

Post by asif_khan_ak_07 »

I am getting WA for this problem.....i tried all the test cases given ......

Code: Select all

#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
	
//	freopen("355.txt","r",stdin);
	int c,b,n,len,x,fg,temp;
	long long ns;
	char in[12],base[20];
	char ans[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	while((scanf("%d %d %s",&c,&b,&in)==3)){
	ns=0;
	fg=0;
	len=strlen(in);
	for(int i=len-1;i>=0;i--){
		n=0;
		for(int j=0;j<c;j++){
			if(in[i]==ans[j]){
				n=1;
				temp=1;
				for(int l=0;l<len-i-1;l++)
					temp=temp*c;
				ns=ns+(j*temp);
				break;
			}
		
		}
	
		if(n==0){
			printf("%s is an illegal base %d number\n",in,c);
			fg=1;
			break;
		}
	
	}	
		
		if(fg==1)
			continue;
		x=0;
		int a;
		while(ns>=b){
			a=ns%b;
			ns/=b;
			base[x++]=ans[a];
		}
		a=ns;
		base[x]=ans[a];
		printf("%s base %d = ",in,c);
		for(int k=x;k>=0;k--)
			printf("%c",base[k]);

		printf(" base %d",b);
		printf("\n");


	}

return 0;
}

@mjad
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

Re: 355 -runtime error why?

Post by @mjad »

please help me, why run time error?
here is my code

Code: Select all

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

unsigned long int dicimal(char a[100],int b);
int convert(unsigned long int n,int b,char o[100],int c);
int main()
{
	char num[100],t[100];
	//freopen("355input.txt","r",stdin);
	int cr_base,con_base;
	unsigned long int di_value;
	while(scanf("%d%d%s",&cr_base,&con_base,num)==3)
	{
		//cin>>cr_base>>con_base>>num
		//cin>>cr_base;

		strcpy(t,num);

		di_value=dicimal(num,cr_base);
		if(di_value==0)
			cout<<t<<" is an illegal base "<<cr_base<<" number"<<endl;
		else if(cr_base==con_base||con_base==10)
			cout<<t<<" base "<<cr_base<<" = "<<di_value<<" base "<<con_base<<endl;
		else
			cr_base=convert(di_value,con_base,t,cr_base);

	}
	return 0;
}
unsigned long int dicimal(char a[100],int b)
{
	int i=0,len;
	len=strlen(a)-1;
	unsigned long int sum=0;
	for(i=0;i<=len;i++)
	{
		if(a[len-i]-48>=b&&a[len-i]<='9')
			return 0;
		else if(a[len-i]-54>b)
			return 0;
		if(a[len-i]>=65&&a[len-i]<=70){
			a[len-i]-=55;
			sum+=(a[len-i])*(unsigned long)pow(b,i);
		}
		else if(a[len-i]!='0'&&a[len-i]<=57)
		{
			a[len-i]-=48;
			sum+=(a[len-i])*(unsigned long)pow(b,i);
		}
	}
	return sum;
}

int convert(unsigned long int n,int b,char o[100],int c)
{
	char a[100];
	int i=0,j;
	cout<<o<<" base "<<c<<" = ";
	while(n)
	{
		a[i++]=(n%b)+48;
		//cout<<a[i];
		n/=b;
	}
	a[i++]='\0';
	for(j=i-1;j>=0;j++)
		cout<<a[j];
	cout<<" base "<<b<<endl;
	//strrev(a);
	//cout<<o<<" base "<<c<<" = "<<a<<" base "<<b<<endl;
	return 0;
}
if i use strrev,why give me CE
please help me

kiddo
New poster
Posts: 4
Joined: Tue Aug 24, 2010 6:33 am

Re: 355 -runtime error why?

Post by kiddo »

@mjad wrote: if i use strrev,why give me CE
please help me
Because strrev isn't ANSI Standard.
Make your own strrev function to reverse the string.

kiddo
New poster
Posts: 4
Joined: Tue Aug 24, 2010 6:33 am

Re:

Post by kiddo »

mf wrote:Try

Code: Select all

16 10 1000000000
16 10 FEDCBA9876
Correct outputs:

Code: Select all

1000000000 base 16 = 68719476736 base 10
FEDCBA9876 base 16 = 1094624909430 base 10

great test cases, finally ACC.
Thx...

varagrawal
New poster
Posts: 3
Joined: Sun Apr 24, 2011 10:33 pm

Re: 355 - The Bases Are Loaded

Post by varagrawal »

My Java code satisfies all the conditions and O/Ps the correct answer for all given cases. Can anyone please tell me what is wrong with it?

Code: Select all

import java.math.*;
import java.util.*;
import java.io.*;

public class Main{
	public static void main(String[] args)
	throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		Scanner sc = new Scanner(br);
		
		int originalBase, newBase, offset;
		
		BigInteger num = new BigInteger("0");
		String ans = "0", input;
		
		while(sc.hasNext()){
		
			originalBase = sc.nextInt();
			newBase = sc.nextInt();
			
			input = sc.next();
			
			offset = 0;
			for(int i=0;i<input.length();i++){
				if(input.charAt(i) != '0') break;
				else offset++;
			}
			input = input.substring(offset);
			
			if( input.equals("") ) input = "0" ;
			
			try{
				num = new BigInteger(input, originalBase);
			}
			catch(NumberFormatException e){
				System.err.print(input + " is an illegal base " + originalBase + " number");
				if(sc.hasNext()) System.out.print("\n");
				continue;
			}
			
			ans = num.toString(newBase);
			ans = ans.toUpperCase();
			
			System.out.print(input + " base " + originalBase + " = " + ans + " base " + newBase);
			if(sc.hasNext()) System.out.print("\n");
		}
	}	
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 355 - The Bases Are Loaded

Post by brianfry713 »

varagrawal,
System.err.print should be System.out.println

The judge reads from standard output and put a newline at the end of your output.
Check input and AC output for thousands of problems on uDebug!

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Location: Dhaka, Bangladesh
Contact:

Re: 355 - The Bases Are Loaded

Post by uvasarker »

I am getting WA. Please help me.

Code: Select all

]
RMVD
Last edited by uvasarker on Mon Aug 20, 2012 9:33 am, edited 1 time in total.

aerofoil.kite
New poster
Posts: 12
Joined: Tue Dec 06, 2011 8:59 pm
Location: Bangladesh
Contact:

Re: 355 - The Bases Are Loaded

Post by aerofoil.kite »

Try this:

Code: Select all

2 10 0
10 10 0000
AC output:

Code: Select all

0 base 2 = 0 base 10
0000 base 10 = 0 base 10

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Location: Dhaka, Bangladesh
Contact:

Re: 355 - The Bases Are Loaded

Post by uvasarker »

Lots of Thanks Brother.......

cstdio
New poster
Posts: 1
Joined: Thu Jul 25, 2013 11:26 am

355 WA

Post by cstdio »

Hi guys,
I went through almost all tricky testcases on this forum.. still WA.. plz help

Code: Select all

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
using namespace std;

string conv(int x,int y,string s){
int sz=s.size(),i,val,rem;
string ans="";
char c;
long long int num=0;

for(i=0;i<sz;i++){
    val= (s[i]>='A'&&s[i]<='z')? s[i]-'A'+10: s[i]-'0';
    if(val>x||val<0) return("invalid");
    num=num*x + val;
}

while(num>0){
    rem=num%y;
    c= (rem<10)?rem+'0': rem-10+'A';
    ans+=c;
    num/=y;
}

if(ans=="") return("0");
reverse(ans.begin(),ans.end());
return(ans);
}

main(){
int x,y;
string s;
while(cin>>x>>y>>s){
    string ans=conv(x,y,s);
    if(ans=="invalid") cout<<s<<" is an illegal base "<<x<<" number\n";
    else cout<<s<<" base "<<x<<" = "<<ans<<" base "<<y<<endl;
}
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 355 WA

Post by brianfry713 »

Try input:

Code: Select all

2 10 2
Output should be:

Code: Select all

2 is an illegal base 2 number
Check input and AC output for thousands of problems on uDebug!

sdipu
New poster
Posts: 23
Joined: Sun May 19, 2013 1:50 am

Re: 355 WA

Post by sdipu »

Here is some IO that could help:

Code: Select all

2 8 0
16 10 1000000000
16 10 FEDCBA9876
2 10 2
2 10 10101
5 3 126
15 11 A4C
2 16 101001230101
16 8 53F094125F5
16 2 53F094125F5
10 8 8650286024
15 13 59A566
5 10 3441011134
7 10 3226143411
8 9 3006355015
output:

Code: Select all

0 base 2 = 0 base 8
1000000000 base 16 = 68719476736 base 10
FEDCBA9876 base 16 = 1094624909430 base 10
2 is an illegal base 2 number
10101 base 2 = 21 base 10
126 is an illegal base 5 number
A4C base 15 = 1821 base 11
101001230101 is an illegal base 2 number
53F094125F5 base 16 = 123741120222765 base 8
53F094125F5 base 16 = 1010011111100001001010000010010010111110101 base 2
8650286024 base 10 = 100346161710 base 8
59A566 base 15 = B71686 base 13
3441011134 base 5 = 7750794 base 10
3226143411 base 7 = 134971047 base 10
3006355015 base 8 = 1034758324 base 9
Check out UVA Arena - a software build for UVA solvers @ http://dipu-bd.github.io/UVA-Arena/

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 355 - The Bases Are Loaded

Post by brianfry713 »

Try input: 10 10 A
Output should be: A is an illegal base 10 number
Check input and AC output for thousands of problems on uDebug!

Shihab
New poster
Posts: 33
Joined: Thu Jun 13, 2013 1:19 pm

Re: 355 WA

Post by Shihab »

AC

Post Reply

Return to “Volume 3 (300-399)”