Page 5 of 5
355 F1 F1
Posted: Wed Jun 10, 2009 10:55 pm
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;
}
Re: 355 - The Bases Are Loaded
Posted: Mon Aug 31, 2009 9:20 am
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;
}
Re: 355 -runtime error why?
Posted: Thu Aug 05, 2010 5:55 am
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
Re: 355 -runtime error why?
Posted: Tue Aug 24, 2010 7:23 am
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.
Re:
Posted: Tue Aug 24, 2010 7:53 am
by kiddo
mf wrote:Try
Correct outputs:
Code: Select all
1000000000 base 16 = 68719476736 base 10
FEDCBA9876 base 16 = 1094624909430 base 10
great test cases, finally ACC.
Thx...
Re: 355 - The Bases Are Loaded
Posted: Tue Nov 29, 2011 11:42 am
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");
}
}
}
Re: 355 - The Bases Are Loaded
Posted: Fri Jan 06, 2012 11:22 pm
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.
Re: 355 - The Bases Are Loaded
Posted: Sun Aug 19, 2012 7:41 am
by uvasarker
I am getting WA. Please help me.
Re: 355 - The Bases Are Loaded
Posted: Mon Aug 20, 2012 6:26 am
by aerofoil.kite
Try this:
AC output:
Code: Select all
0 base 2 = 0 base 10
0000 base 10 = 0 base 10
Re: 355 - The Bases Are Loaded
Posted: Mon Aug 20, 2012 9:32 am
by uvasarker
Lots of Thanks Brother.......
355 WA
Posted: Thu Jul 25, 2013 12:42 pm
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;
}
}
Re: 355 WA
Posted: Fri Jul 26, 2013 12:44 am
by brianfry713
Try input:
Output should be:
Re: 355 WA
Posted: Fri Aug 02, 2013 1:09 am
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
Re: 355 - The Bases Are Loaded
Posted: Tue Mar 18, 2014 9:47 pm
by brianfry713
Try input: 10 10 A
Output should be: A is an illegal base 10 number
Re: 355 WA
Posted: Mon Mar 31, 2014 11:26 pm
by Shihab
AC