389 - Basically Speaking

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

Post Reply
imranul
New poster
Posts: 12
Joined: Fri Jul 19, 2002 6:28 pm
Location: Bangladesh

389 - Basically Speaking

Post by imranul » Sun Jul 28, 2002 8:24 pm

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
:(
Life is like a box of Chocolates,
you never know what you're going to get...

Munir
New poster
Posts: 3
Joined: Mon Jul 08, 2002 6:55 pm
Location: Dhaka, BANGLADESH.
Contact:

Post by Munir » Sun Jul 28, 2002 10:29 pm

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?
Last edited by Munir on Sun Jul 28, 2002 11:24 pm, edited 1 time in total.

imranul
New poster
Posts: 12
Joined: Fri Jul 19, 2002 6:28 pm
Location: Bangladesh

Post by imranul » Mon Jul 29, 2002 7:10 am

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??? :lol:
Life is like a box of Chocolates,
you never know what you're going to get...

imranul
New poster
Posts: 12
Joined: Fri Jul 19, 2002 6:28 pm
Location: Bangladesh

Hellow.......anyone alive to help me on 389

Post by imranul » Wed Jul 31, 2002 6:34 am

PLease help me:(
Life is like a box of Chocolates,
you never know what you're going to get...

broderic
New poster
Posts: 34
Joined: Thu Jun 06, 2002 4:35 am
Location: Canada

Post by broderic » Wed Jul 31, 2002 8:53 pm

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)

imranul
New poster
Posts: 12
Joined: Fri Jul 19, 2002 6:28 pm
Location: Bangladesh

Post by imranul » Wed Jul 31, 2002 9:24 pm

Thank u very much....thanks a lot :P
Life is like a box of Chocolates,
you never know what you're going to get...

HKcow
New poster
Posts: 10
Joined: Fri Aug 30, 2002 2:34 pm

389, WA, pls help

Post by HKcow » Mon Sep 15, 2003 6:39 pm

Hi experts,

Writing a post here to seek help is my last step and i think i need to do so now :roll:
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]

Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan » Tue Sep 16, 2003 11:21 am

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.
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.

Iwashere
New poster
Posts: 20
Joined: Mon Aug 11, 2003 1:50 pm
Location: Singapore

Post by Iwashere » Sun Feb 15, 2004 8:28 am

Can somebody give me some input for this problem?

Thanks in advance.

WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

Post by WR » Tue Apr 27, 2004 10:14 am

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?

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry » Wed Apr 28, 2004 9:15 am

TLE can also mean maybe you're struck in a loop.. are you sure you handle input correctly?

WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

Post by WR » Wed Apr 28, 2004 11:11 am

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.

jagadish
Learning poster
Posts: 90
Joined: Mon Feb 16, 2004 8:53 pm
Location: Bangalore INDIA

Post by jagadish » Mon Jul 05, 2004 7:27 pm

my Ac code gives the same output. there are no negative numbers in the input
( -1100 doesnt make sense to me :) )
if u can think of it .. u can do it in software.

WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

Post by WR » Tue Jul 06, 2004 7:43 am

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??!!

jagadish
Learning poster
Posts: 90
Joined: Mon Feb 16, 2004 8:53 pm
Location: Bangalore INDIA

Post by jagadish » Tue Jul 06, 2004 7:47 pm

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
if u can think of it .. u can do it in software.

Post Reply

Return to “Volume 3 (300-399)”