10473 - Simple Base Conversion

All about problems in Volume 104. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Lon
New poster
Posts: 7
Joined: Sat Jan 31, 2004 12:55 pm

10473 wrong answer!!!

Post by Lon » Sat Jun 05, 2004 6:07 am

This ia my code!!
Can anyone tell me what wong with my code!!
thank you!!

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
void main()
{
long int v,i;
double count,m,z;
char tran[12];
while(gets(tran))
{
z=count=v=0;
if(!strcmp(tran,"-1"))
break;
if(tran[0]=='0'&&tran[1]=='x')
{
for(i=strlen(tran)-1;i>=2;i--)
{
m=pow(16,count);
if(tran=='F')
m=m*15;
else if(tran=='E')
m=m*14;
else if(tran=='D')
m=m*13;
else if(tran=='C')
m=m*12;
else if(tran=='B')
m=m*11;
else if(tran=='A')
m=m*10;
else
m=m*((double)tran-'0');
count++;
z+=m;
}
printf("%.lf\n",z);
}
else
{
v=atoi(tran);
printf("0x%lX\n",v);
}
}
}
my name is Lon

sidky
New poster
Posts: 50
Joined: Wed Nov 06, 2002 1:37 pm
Location: Planet Earth, Universe
Contact:

Post by sidky » Tue Jul 20, 2004 5:19 pm

Just consider this case:

0xB
0xC

whose output should be:

11
12

coolzero
New poster
Posts: 8
Joined: Fri Nov 12, 2004 11:33 am

10473 : Output Limit Exceed

Post by coolzero » Fri Nov 12, 2004 11:36 am

Hi.

I don't have any infinite loops in my code.. I don't understand the error...

Heres my code :

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
                                                                                
int main() {
                                                                                
        char line[1000];
        char aux[1000];
        char *lixo;
        int i;
                                                                                
        scanf("%s\n",line);
        do {
                                                                                
                if (atoi(line) != -1) {
                        if (line[0] == '0' && line[1] == 'x') {
                                strcpy(aux, line);
                                i = strtol(aux, &lixo, 16);
                                printf("%d\n", i);
                        }
                        else
                                printf("0x%X\n", atoi(line));
                                                                                
                                                                                
                        scanf("%s\n",line);
                }
                                                                                
        } while(atoi(line)!=-1);
                                                                                
        return 0;                                                                                
}
Input tested :
4
7
44
0x80685
0xABC
2748
0x1
0xABCDEF
123456
0x123456789
291
0x123
291
0xB
0xC
-1

Output :

0x4
0x7
0x2C
525957
2748
0xABC
1
11259375
0x1E240
2147483647
0x123
291
0x123
11
12

Can anyone see the problem?

Tanks

User avatar
CodeMaker
Experienced poster
Posts: 183
Joined: Thu Nov 11, 2004 12:35 pm
Location: AIUB, Bangladesh

Post by CodeMaker » Mon Nov 15, 2004 6:32 am

Well u have a BIIIIIIIIIIIG infinite loop..... :-?

well ur code really give me hard time brother....I should have seen it earlier....when I could not find any logical error....I get back to the problem description.....and u know it always help..... :)

and what I found is that the programme will be terminated by a negative decimal value not necessarily with -1 all the time.... :roll:

so u should do: atoi(line)>=0 instead of doing atoi(line)!=-1 in both of the places....and ur code got Accepted when I changed it......R U HAPPY NOW?

Goodluck.....see u later on...... :wink:
Jalal : AIUB SPARKS

coolzero
New poster
Posts: 8
Joined: Fri Nov 12, 2004 11:33 am

Post by coolzero » Mon Nov 15, 2004 11:19 am

HI.

Tanks for replying.

In fact, i haven't notest that problem of the decimal integer :) Tanks

I've tested it but now I got Wrong Answer :(

Code: Select all

(...)
if (atoi(line) >= 0) {
                        if (line[0] == '0' && line[1] == 'x') {
(...)

        } while(atoi(line)>=0);
(...)
I think that the judge doesn't like me :cry:

User avatar
CodeMaker
Experienced poster
Posts: 183
Joined: Thu Nov 11, 2004 12:35 pm
Location: AIUB, Bangladesh

Post by CodeMaker » Mon Nov 15, 2004 5:31 pm

Hey....come on..... :roll: dont be so frustrated....

how did u submit ur code.....do u use somekind of mail service like yahoo or hotmail? may be thats the reason why u r getting WA.....

i just copy-paste ur code in the judge submitter and just changed the sign to (>=0) from (!=-1) and it got Acc again right now.

always use the submit-o-matic software in the main window " submit your code here " u will see it, never mail ur code and becareful about the correct problem number......10473

there cant be any other reason for ur WA...ur code is ok and got AC twice :lol: (shall I submit it for u? :wink: )
Jalal : AIUB SPARKS

coolzero
New poster
Posts: 8
Joined: Fri Nov 12, 2004 11:33 am

Post by coolzero » Mon Nov 15, 2004 6:37 pm

I'll try again...

Because of me the reputation of country is going down! :(

coolzero
New poster
Posts: 8
Joined: Fri Nov 12, 2004 11:33 am

Post by coolzero » Mon Nov 15, 2004 6:40 pm

ID Date (UTC) Status CPU Memory Author Source Problem / Title
3077610 2004/11/15 16:38:38.860 Wrong Answer 0:00.051 64 53936 C 10473 - Simple Base Conversion
3076771 2004/11/15 09:16:31.100 Wrong Answer 0:00.051 64 53936 C 10473 - Simple Base Conversion
3076766 2004/11/15 09:14:10.539 Wrong Answer 0:00.055 392 53936 C 10473 - Simple Base Conversion

I think I'me getting crazy :(

[c]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main() {

char line[1000];
char aux[1000];
char *lixo;
int i;

scanf("%s\n",line);
do {

if (atoi(line) >= 0) {
if (line[0] == '0' && line[1] == 'x') {
strcpy(aux, line);
i = strtol(aux, &lixo, 16);
printf("%d;\n", i);
}
else
printf("0x%X;\n", atoi(line));


scanf("%s\n",line);
}

} while(atoi(line)>=0);

return 0;

}
[/c]

User avatar
CodeMaker
Experienced poster
Posts: 183
Joined: Thu Nov 11, 2004 12:35 pm
Location: AIUB, Bangladesh

Post by CodeMaker » Mon Nov 15, 2004 8:26 pm

:oops: Hello Brother.....weeeell I got wrong answer this time :roll:

:D But I also found the error in ur new code....

8) well I told u to change >=0, not to put extra ' ;' in ur printf();

have a look u have extra semi colons in ur printf function, just before the '\n' and thats the reason for ur WA.....I dont know why u did it....but u know it happens sometimes, when things get wrong. But no matter never lose hope. I m in good form now-a-days. sometime it also happens to me.

your country will be proud of u if u dont lose confidence and keep trying no matter what happens...... :D

I hope u get Ac this time, and if not, I will be back :wink:
Jalal : AIUB SPARKS

coolzero
New poster
Posts: 8
Joined: Fri Nov 12, 2004 11:33 am

Post by coolzero » Mon Nov 15, 2004 8:36 pm

:evil:

I put the semicolon just to see if there was any garbage on the printf's and I forget to take it off!!


Accepted now :)

Tanks

User avatar
CodeMaker
Experienced poster
Posts: 183
Joined: Thu Nov 11, 2004 12:35 pm
Location: AIUB, Bangladesh

Post by CodeMaker » Tue Nov 16, 2004 4:24 am

:D You got Acc....Happy to hear that...

Goodluck, things may work just fine from now
Jalal : AIUB SPARKS

User avatar
Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria

Post by Sedefcho » Thu Feb 24, 2005 6:03 pm

For anyone who may need some
sample I/O on problem 10473.

The dots in the sample input
below represent ( should be replaced by )
spaces.

The last two lines of the sample input contain the
decimal and the hexadecimal representation of
the number N = 2^31 - 1

INPUT

Code: Select all

..0..
...0x0....
1
2
3
..4...
10
11
12
13
14
15
...16
17
44
..64
...0x80685...
0x1
0x2
0x3
0x4
...0x10..
0x11
0xA
0xB
...0xF...
0xFF
....................2147483647....
....0x7FFFFFFF...
-5

OUTPUT

Code: Select all

0x0
0
0x1
0x2
0x3
0x4
0xA
0xB
0xC
0xD
0xE
0xF
0x10
0x11
0x2C
0x40
525957
1
2
3
4
16
17
10
11
15
255
0x7FFFFFFF
2147483647


User avatar
jaracz
Learning poster
Posts: 79
Joined: Sun Sep 05, 2004 3:54 pm
Location: Poland

Post by jaracz » Mon Jun 13, 2005 11:28 pm

it's horrible easy to solve this using C/C++

here's the part that shows advantage of C++;D

Code: Select all

        if(strlen(line)>=3 && line[0] == '0' && line[1] == 'x') //then num is in hexdecimal system
        {
            line[0] = line[1] = ' ';
            sscanf(line,"%x",&num);
            printf("%d\n",num);
        }
        else //decimal system
        {
            sscanf(line,"%d",&num);
            printf("0x%X\n",num);
        }
Regards
keep it real!

ansiemens
New poster
Posts: 6
Joined: Tue Aug 09, 2005 11:24 am
Location: New York, NY

10473 compiler error ? WHY

Post by ansiemens » Tue Aug 09, 2005 11:29 am

Here is my code
I can compile it in GCC of Freebsd and devc
but why compiler error?

Code: Select all

#include<string>
#include<iostream>
#include<sstream>
#include<iomanip>
using namespace std;

int main() {
    string input;
    int loc_x,loc_neg,d;
    while(cin>>input) {
        loc_neg=input.find("-",0);
        loc_x=input.find("0x",0);
        if(loc_neg!=string::npos) {
            if(loc_x!=string::npos) {
                istringstream(input) >> hex >> d;
                cout << dec << d << endl;
            }
            else break;
        }
        else {
            if(loc_x!=string::npos) {
                istringstream(input) >> hex >> d;
                cout << dec << d << endl;
            }
            else {
                istringstream(input) >> d;
                cout << "0x" << hex << uppercase << d << endl;
            }
        }
    }
    return 0;
}

kai
New poster
Posts: 5
Joined: Mon Aug 08, 2005 2:36 pm
Location: Japan

Post by kai » Tue Aug 09, 2005 2:06 pm

I've got the following error with g++-2.95:

tmp.cc: In function `int main()':
tmp.cc:27: `uppercase' undeclared (first use this function)
tmp.cc:27: (Each undeclared identifier is reported only once
tmp.cc:27: for each function it appears in.)

Post Reply

Return to “Volume 104 (10400-10499)”