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
ansiemens
New poster
Posts: 6 Joined: Tue Aug 09, 2005 11:24 am
Location: New York, NY
Post
by ansiemens » Tue Aug 09, 2005 3:21 pm
kai wrote: 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.)
but g++ 3.4.2 can work||
i change
Code: Select all
#include<string>
#include<iostream>
#include<sstream>
using namespace std;
int main() {
string input;
int loc_x,loc_neg;unsigned long d;
cout.setf(ios::uppercase);
while(cin>>input) {
loc_neg=input.find("-",0);
if(loc_neg!=string::npos) break;
else {
loc_x=input.find("0x",0);
if(loc_x!=string::npos) {
istringstream(input) >> hex >> d;
cout << dec << d << endl;
}
else {
istringstream(input) >> dec >> d;
cout << "0x" << hex << d << endl;
}
}
}
return 0;
}
but i got wa now
can anyone give me some test data?[/code]
mohsincsedu
Learning poster
Posts: 63 Joined: Tue Sep 20, 2005 12:31 am
Location: Dhaka
Contact:
Post
by mohsincsedu » Thu Sep 22, 2005 7:57 pm
I got run time error:)
but why..?
Here is my coding:
Code: Select all
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char table[] = "0123456789ABCDEF";
long hex_to_dec(char num[])
{
long number = 0;
long i,j;
long len;
len = strlen(num);
for(i = len-1; i>=0; i--)
{
for(j = 0;j<16;j++)
{
if(table[j]==num[i])
break;
}
number +=j*pow(16,len-i-1);
}
return number;
}
char *dec_to_hex(long num)
{
char number[15];
long temp;
long i,j,k;
i = 0;
while(num > 0)
{
temp = num%16;
num /= 16;
number[i++] = table[temp];
}
number[i] = '\0';
char m[15];
k = strlen(number);
for(i = k-1,j=0;i>=0;i--,j++)
m[j] = number[i];
m[j] = '\0';
return m;
}
int main()
{
char *num;
char temp[16];
int i,j,k;
long number;
while(gets(num)!=NULL)
{
if(num[0]=='-')
break;
if(num[0]=='0'&&num[1]=='x')
{
k = strlen(num);
for(i=2,j=0;i<k;i++,j++)
temp[j] = num[i];
temp[j] = '\0';
number = hex_to_dec(temp);
printf("%ld\n",number);
}
else
{
number = atol(num);
num = dec_to_hex(number);
printf("0x%s\n",num);
}
}
return 0;
}
Roby
Experienced poster
Posts: 101 Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:
Post
by Roby » Tue Nov 22, 2005 6:43 am
Maybe your table declaration that makes your program got RTE.
Code: Select all
char table[] = "0123456789ABCDEF";
Since I've coded with C/C++ for a long time, I never declare an array without its maximum capacity (I mean the index) and I know it would make some errors if I compile it with some compiler or editor. So, never declare an array without its capacity. Better you declare like this:
Code: Select all
#define MAX 20
char table[MAX] = { "0123456789ABCDEF" };
And here are some input for you test:
Hope it helps you
mamun
A great helper
Posts: 286 Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:
Post
by mamun » Tue Nov 22, 2005 12:58 pm
char table[] = "0123456789ABCDEF";
Actually this is OK. Compiler allocates memory for unallocated array when initialized. Your problem is in main()
char *num;
where you need to allocate memory like
char *num=new char [20];
Also in dec_to_hex(long num) fuction
char number[15];
Here increase the size, at least to 16.
samin_yasar
New poster
Posts: 5 Joined: Sat Mar 28, 2009 6:15 pm
Post
by samin_yasar » Tue Oct 06, 2009 6:50 pm
hello, below is a very simple code of this problem.it provides the correct output for smaller numbers but not for big numbers.can
any one tell me why it is happening?
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
char num[100],*p;
while(1)
{
gets(num);
n = atoi(num);
if(n<0)break;
if(num[1]=='x')
{
sscanf(num,"%X",&n);
printf("%d\n",n);
}
else
{
printf("0x%X\n",n);
}
}
return 0;
}
mahade hasan
Learning poster
Posts: 87 Joined: Thu Dec 15, 2011 3:08 pm
Location: University of Rajshahi,Bangladesh
Post
by mahade hasan » Fri May 04, 2012 9:43 am
cut got AC!!
Last edited by
mahade hasan on Tue Jun 05, 2012 7:15 pm, edited 1 time in total.
we r surrounded by happiness
need eyes to feel it!
brianfry713
Guru
Posts: 5947 Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA
Post
by brianfry713 » Fri May 04, 2012 9:16 pm
You're not printing a newline at the end.
Check input and AC output for thousands of problems on
uDebug !
gr81
New poster
Posts: 46 Joined: Wed Sep 26, 2012 7:52 pm
Post
by gr81 » Sat Oct 20, 2012 12:59 pm
Is there any special test case, which is failing my submitted programs...
here is output with my program..
Input
4
7
44
0x80685
0x9f9f
1234567
0
0x0
0XFFFFFFFF
23342342234
-1
Output
0x4
0x7
0x2C
525957
40863
0x12D687
0x0
0
-1
0x6F4FE05A
Help...
gr81
New poster
Posts: 46 Joined: Wed Sep 26, 2012 7:52 pm
Post
by gr81 » Sat Oct 20, 2012 1:05 pm
code AC, terminated digit should be -ve number, i was considering -1.
laituanksa245
New poster
Posts: 20 Joined: Tue Jan 10, 2012 4:23 pm
Location: Vietnam
Post
by laituanksa245 » Fri Dec 14, 2012 5:21 pm
Why does Uvatoolkit answer 1867505754 for the input "0x56F4FE05A" ?
Shouldn't the answer be 23342342234 ?
By the way, what is wrong with my code ? I keep getting WA
Last edited by
laituanksa245 on Sat Dec 15, 2012 2:09 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947 Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA
Post
by brianfry713 » Fri Dec 14, 2012 8:04 pm
Yes 0x56F4FE05A equals 23342342234, but the problem statement requires the decimal value of the input to be less than 2147483648.
Don't print a blank line at the end of the output.
Check input and AC output for thousands of problems on
uDebug !
laituanksa245
New poster
Posts: 20 Joined: Tue Jan 10, 2012 4:23 pm
Location: Vietnam
Post
by laituanksa245 » Sat Dec 15, 2012 2:09 am
@brianfry713: Thanks very much
kier.guevara
New poster
Posts: 30 Joined: Thu Jul 19, 2012 11:24 pm
Post
by kier.guevara » Thu Feb 21, 2013 7:04 am
I'm getting WA with this easy problem..Are there special cases for this prob?
Last edited by
kier.guevara on Thu Feb 21, 2013 3:03 pm, edited 1 time in total.
shuvokr
Learning poster
Posts: 66 Joined: Tue Oct 02, 2012 8:16 pm
Location: Bangladesh
Post
by shuvokr » Thu Feb 21, 2013 12:21 pm
Try this I/O :::
Input ::
Output ::
But your program's output is 23342284890 which is wrong.
Sorry for my poor English.
lbv
Experienced poster
Posts: 128 Joined: Tue Nov 29, 2011 8:40 am
Post
by lbv » Thu Feb 21, 2013 12:51 pm
kier.guevara wrote:
I'm getting WA with this easy problem..Are there special cases for this prob?
From the problem statement:
"Hexadecimal numbers always starts with a `0x' and all other numbers are to be considered as decimal numbers."
Try for example:
Input
Output
shuvokr wrote:
Try this I/O :::
0x56F4FE05A would be invalid, and I don't think there any test cases like that. From the input specification:
"The decimal value of this number will be less than 2^31."