Page 1 of 7
389 - Basically Speaking
Posted: Sun Jul 28, 2002 8:24 pm
by imranul
Why does my program give WA again and again and again.......
Code: Select all
/* @JUDGE_ID:XXXXXX 389 C++ */
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char str[100];
long from,to;
long num;
long power(long b,long n)
{
long i,p=1;
for(i=0;i<n;i++)
p=p*b;
return p;
}
void str_rev(char *s)
{
long i,j;
j=strlen(s)-1;
for(i=0;i<j;i++,j--)
{
s[i]=s[i]^s[j];
s[j]=s[i]^s[j];
s[i]=s[i]^s[j];
}
}
void todecimal()
{
long i,j;
long l=strlen(str);
num=0;
for(i=l-1,j=0;i>=0;i--,j++)
{
if(isdigit(str[i]))
num=num+(long)((str[i]-'0')*power(from,j));
else if(str[i]>='A' && str[i]<='F')
num=num+(long)((str[i]-'A'+10)*power(from,j));
}
}
void tobase()
{
char s[100];
long i=0;
long d;
do
{
d=num%to;
if(d>=10)
{
s[i]=(d-10)+'A';
}
else
{
s[i]=d+'0';
}
num/=to;
i++;
}
while(num);
s[i]='\0';
str_rev(s);
if(strlen(s)<=7)
printf("%7s\n",s);
else
printf("%7s\n","ERROR");
}
void main()
{
while(3==scanf("%s%ld%ld",str,&from,&to))
{
if(from==to)
{
printf("%7s\n",str);
}
else
{
todecimal();
tobase();
}
}
}
/* @END_OF_SOURCE_CODE */
please help me

Posted: Sun Jul 28, 2002 10:29 pm
by Munir
Why & how life is like a box of Chocolates?
No reason to see ur whole program;u r printing number more than 7 digits when the two bases r equal.
P.S. U know who is "Bad Luck",right?
Posted: Mon Jul 29, 2002 7:10 am
by imranul
The problem description clearly says:
The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features:
It will have a 7-digit display.
Its buttons will include the capital letters A through F in addition to the digits 0 through 9.
It will support bases 2 through 16.
So, no input can be more than 7 digits, but I also tried with that option:
Code: Select all
if(from==to)
{
if(strlen(str)<=7)
printf("%7s\n",str);
else
printf("%7s\n","ERROR");
}
Still it gave WA.
And another interesting thing: I tried my program with JUDGE input used for ACM regional and the output was OK.
Why & how life is like a box of Chocolates?
->because u never know what u are gonna get from it
PS. and of course I know Bad Luck....but why???

Hellow.......anyone alive to help me on 389
Posted: Wed Jul 31, 2002 6:34 am
by imranul
PLease help me:(
Posted: Wed Jul 31, 2002 8:53 pm
by broderic
If from==to, you're just printing out the number you read in.
what if you are given "000000"? the output should be "0", but
you write out "000000".
Broderick
(btw, i switched your code and got ac)
Posted: Wed Jul 31, 2002 9:24 pm
by imranul
Thank u very much....thanks a lot

389, WA, pls help
Posted: Mon Sep 15, 2003 6:39 pm
by HKcow
Hi experts,
Writing a post here to seek help is my last step and i think i need to do so now
I have prepared a long test case and I still can't find any error, could anyone help me to figure out why my code still get WA? THX!
[cpp]
#include <stdio.h>
#include <string.h>
int x2ten(char input[], int from)
{
int weight = 1, output = 0;
for ( int i = strlen(input) - 1; i >= 0; i--, weight*=from )
if ( input
>= 'A' )
output += weight * ( input - 55 );
else
output += weight * ( input - 48 );
return output;
}
bool ten2x(int base10, int to, char output[8])
{
int i1 = 1;
int i2 = to;
int i3 = i2 * to;
int i4 = i3 * to;
int i5 = i4 * to;
int i6 = i5 * to;
int i7 = i6 * to;
int i8 = i7 * to;
if ( base10 >= i8 )
return false;
output[0] = base10 / i7 + 48;
output[1] = base10 / i6 % i2 + 48;
output[2] = base10 / i5 % i2 + 48;
output[3] = base10 / i4 % i2 + 48;
output[4] = base10 / i3 % i2 + 48;
output[5] = base10 / i2 % i2 + 48;
output[6] = base10 % i2 + 48;
output[7] = '\0';
for ( to = 0; (output[to] == '0') && (to < 6); to++ )
if ( output[to] == '0' )
output[to] = ' ';
for ( to = 0; to < 7; to++ )
if ( output[to] > 57 )
output[to] += 7;
return true;
}
void main()
{
char input[100], output[8];
int from, to, i, j, base10;
while ( scanf( "%s %d %d", input, &from, &to ) == 3 )
{
for ( i = 0; (input=='0') && (i < strlen(input)); i++ );
if ( i == strlen(input) )
{
input[0] = '0';
input[1] = '\0';
}
else
if ( i != 0 )
{
j = 0;
for ( ; input != '\0'; i++, j++ )
input[j] = input;
input[j] = 0;
}
if ( from == to )
printf("%7s\n", input);
else
{
if ( from != 10 )
base10 = x2ten(input, from);
else
sscanf(input, "%d", &base10);
if ( to == 10 )
printf("%7d\n", base10);
else
if ( ten2x(base10, to, output) )
puts(output);
else
puts(" ERROR");
}
}
}
[/cpp]
Posted: Tue Sep 16, 2003 11:21 am
by Joseph Kurniawan
Code: Select all
else
if ( ten2x(base10, to, output) )
puts(output);
Just me being curious :
The variable output are all local variables. Then, is it possible in the main function the output variable to be identified? In the program, I can see the processing of the variable 'output' (but again the variable output in the ten2x function is local i.e. only known by ten2x) but there's no variable passing between main and ten2x.
Posted: Sun Feb 15, 2004 8:28 am
by Iwashere
Can somebody give me some input for this problem?
Thanks in advance.
Posted: Tue Apr 27, 2004 10:14 am
by WR
input:
Code: Select all
1111000 2 10
1111000 2 16
2102101 3 10
2102101 3 15
12312 4 2
1A 15 2
1234567 10 16
ABCD 16 15
0 2 15
ABCD 16 16
000000F 16 10
1A 15 15
0000000 16 10
0000000 16 16
0000000 10 10
0 16 10
00 16 16
000 10 10
output:
Code: Select all
120
78
1765
7CA
ERROR
11001
12D687
D071
0
ABCD
15
1A
0
0
0
0
0
0
I'm pretty sure my program works, at least it does on my pc.
What could be the reason for Time Length Exceeded at the OJ?
Posted: Wed Apr 28, 2004 9:15 am
by Larry
TLE can also mean maybe you're struck in a loop.. are you sure you handle input correctly?
Posted: Wed Apr 28, 2004 11:11 am
by WR
Thanks for the quick response!
No, I'm not sure that I handle the input correctly.
The program should be correct, as I used practically the same routines for problem 355 and got accepted.
edited: June, 2nd
Nearly everything deleted. Sample data inserted instead.
Perhaps somebody could verify the following data?????
input:
Code: Select all
-12 10 10
-12 10 2
12345678 10 10
1111000 2 10
1111000 2 16
2102101 3 10
2102101 3 15
12312 4 2
1A 15 2
1234567 10 16
ABCD 16 15
0 2 15
ABCD 16 16
000000F 16 10
1A 15 15
0000000 16 10
0000000 16 16
0000000 10 10
0 16 10
00 16 16
000 10 10
65536 10 2
64 10 2
1111111 2 16
output:
Code: Select all
-12
-1100
1234567
120
78
1765
7CA
ERROR
11001
12D687
D071
0
ABCD
15
1A
0
0
0
0
0
0
ERROR
1000000
7F
The 3rd output line could also be ERROR. I tried both versions, both got WA.
Posted: Mon Jul 05, 2004 7:27 pm
by jagadish
my Ac code gives the same output. there are no negative numbers in the input
( -1100 doesnt make sense to me

)
Posted: Tue Jul 06, 2004 7:43 am
by WR
Well, whether negative numbers make sense or not, you never know with those problem setters!
But if your code has been accepted and gives the same results, what could be the problem with my program??!!
Posted: Tue Jul 06, 2004 7:47 pm
by jagadish
WR wrote:
But if your code has been accepted and gives the same results, what could be the problem with my program??!!
its diffcult to say without seeing your program
try some stronger inputs like
0000000000000000000000000000 2 15
1111111111111111111111111111 2 16
0111111111111111111111111111 2 16