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

Bitta
New poster
Posts: 4
Joined: Fri Jan 25, 2008 7:04 pm
Location: Dhaka,Bangladesh
Contact:

Re: 389 WA why?

Post by Bitta » Fri Jul 25, 2008 12:37 am

i am getting runtime error in 389-Basically speaking.please help me... :cry:
here is my code-

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char num[1000];
char d[1000];
char ans[1000];

long int convert_decimal(char *s,int base)
{
long int i,j,sum;


j=strlen(s)-1;
sum=0;
for(i=0;s;i++)
{ if(s>='0' && s<='9')
{ sum+=((s-'0')*(long int)(pow(base,j)));
j--;
}

else if(s>='A' && s<='Z')
{ sum+=((s-55)*(long int)(pow(base,j)));
j--;
}
}
return sum;
}

char* itob(long int n,int b)
{ int i,j,temp;


i=0;

do
{ j=n%b;
d[i++]=(j<=9)?(j+'0'):(j+'A'-10);
}while((n/=b)>0);

d='\0';

j=strlen(d)-1;
for(i=0;i<=j;i++,j--)
{ temp=d;
d=d[j];
d[j]=temp;
}

return d;
}


int main()

{ char s[1000];

char t[3];
char f[3];
int sum,i,j,k,p,l,c,from,to;

while(1)
{ gets(s);

j=0;
k=0;
l=0;
p=0;
for(i=0;s[i];i++)
{
if(s[i]==' ')
continue;

else if(((s[i]>='0' && s[i]<='9') || (s[i]>='A' && s[i]<='F')) && p==0)
{ p=1;
while(s[i]!=' ')
{ c=s[i];
num[j++]=c;
i++;

}

num[j]='\0';

}

else if(p==1)
{ p=2;
while(s[i]!=' ')
{ c=s[i];
f[k++]=c;
i++;
}

f[k]=NULL;

}

else if(p==2)
{
while(s[i]!=' ')
{ c=s[i];
t[l++]=c;
i++;
}

t[l]=NULL;
}

}

from=atoi(f);
to=atoi(t);



sum=convert_decimal(num,from);


strcpy(ans,itob(sum,to));
if(strlen(ans)>6)
printf(" ERROR\n");

else

{ printf("%7s",ans);
printf("\n");
}


}
return 0;
}

Bluefin
New poster
Posts: 20
Joined: Sat Jul 08, 2006 3:39 pm
Contact:

Re: 389 WA why?

Post by Bluefin » Sat Jul 26, 2008 3:43 pm

Hey, everybody, my code also gives WA, and I don't know why. Can anybody help? Thanks in advance

Code: Select all

#include <iostream>
#include <cstdlib>
#include <string.h>
#include <iomanip>
#include <math.h>

using namespace std;

bool k;
char answer[100];
int power(int base, int p);
int original(char ar[], int a);
void converted(int sum, int b);

int main()
{
    char ar[100];
    int a, b, sum;
    
    while(cin >> ar >> a >> b)
    {
        sum = original(ar,a);
        memset(answer,'\0',100);
        k = true;
        if(sum == 0)
            cout << setw(7) << '0' << endl;
        else
        {
            converted(sum,b);
        
            if(k)
                cout << setw(7) << answer << endl;
            else
                cout << setw(7) << "ERROR" << endl;
        }
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

int original(char ar[], int a)
{
    int length = strlen(ar), tmp, sum = 0;
    
    for(int i = 0, j = length - 1; i < length, j >= 0; i++, j--)
    {
        if(ar[i] >= '0' && ar[i] <= '9')
            tmp = ar[i] - 48;
        else
            tmp = ar[i] - 55;
        sum += tmp * power(a,j);
    }
    return sum;
}

void converted(int sum, int b)
{
    int p = int(floor(log(sum) / log(b)));
    
    for(int i = p, j = 0; i >= 0; i--, j++)
    {
        if(j == 7)
        {
            k = false;
            break;
        }
        int tmp_1 = power(b,i), tmp_2 = sum / tmp_1;
        
        if(tmp_2 >= 0 && tmp_2 <= 9)
            answer[j] = tmp_2 + 48;
        else
            answer[j] = tmp_2 + 55;
        sum %= tmp_1;
    }
}

int power(int base, int p)
{
    int sum = 1;
    for(int i = 0; i < p; i++)
        sum *= base;
    return sum;
}
"It's nice to be important, but it's more important to be nice"

http://bluefintuna.wordpress.com/

yuyotuiie
New poster
Posts: 1
Joined: Wed Aug 13, 2008 3:09 am
Contact:

very god!

Post by yuyotuiie » Fri Aug 15, 2008 9:43 pm

bump!!haha

-----------------------------------------------
welcome to our wow power leveling webpage,and here you may have wow powerleveling at every levels, the wow power leveling web for wow power leveling,WOrld of warcraft Power Leveling , Cheap wow gold here,buy WoW Power Leveling,

User avatar
newton
Experienced poster
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:

Re: 389 - 8 months? WA

Post by newton » Sat Aug 16, 2008 7:58 am

yuyotuiie

what would u like to say?

lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: 389 - CE

Post by lnr » Mon Oct 06, 2008 4:41 pm

I ran your code and got accepted.

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

389:- WA

Post by sazzadcsedu » Fri Feb 13, 2009 11:14 pm

PLZ HELP SOME.
GOT wa.

Code: Select all

  #include<stdio.h>
                          #include<ctype.h> 
                          #include<math.h>
                          #include<string.h>



                             int power(int a,int b)

							 { 

							  int i,res=1;


								  if(b==0)

								   return 1;

								
								 else 
									 
								 {
								    for(i=1;i<=b;i++)

									{ 

									 res=a*res;

									}
                                    
                                  return res;

								 }

							 }





                             int  main()


						 {   
                              int num,y,len,i,rem,b1,b2,j;
							  char str[10000];
							  char resstr[10000]; 
                              char revans[10000];
                           

								

                              while(scanf("%s %d %d",str,&b1,&b2)==3)
							  {
                               //gets(str);

							   len=strlen(str);
							       
							        
							   
	//******************** CONVERTTING NUMBER TO DECIMAL SYSTEM************************
							   
							   
							          num=0;

                                       y=0;


									   
								   for(i=len-1;i>=0;i--)

								   {  
									   if(isdigit(str[i]))  //test whether it is digit

									   num=num+ ((str[i]-48)*power(b1,y)); 

                                      else 

                                        num=num+ ((str[i]-55)*power(b1,y));


                                        y++;
							 
                                    //printf("this is hexadecimal");
								   }
                                      //printf("%d\n\n",num);

				
									  
//*********************************   @@@@@@@@@@@     *****************************
                           
						 
//************************* decimal to another base*******************

                                    i=0;

									   while(num>0)
							 {   

							
								 
                                    rem=num%b2;
                                //printf(" num %d\n",num);
                                 if(rem==10)
									 resstr[i]='A';
								 else if(rem==11)

									 resstr[i]='B';
								 else if (rem==12)

									    resstr[i]='C';

								 else if (rem==13)
									     resstr[i]='D';

								 else if (rem==14)
									      resstr[i]='E';
								 else if (rem==15)
									     resstr[i]='F';

								 else 

									 resstr[i]=rem+48;



								//printf("%x",num);
                                num=num/b2;
                                    i++;
								 
                             }

							    resstr[i]='\0';
                             //FLAG=0;
                                  
                                 if(strlen(resstr)>7)
									 printf("  ERROR\n");

								 else
                                 {

                                   //puts(hxstr);
									 j=0;
								for(i=strlen(resstr)-1;i>=0;i--)

							  {

									revans[j]=resstr[i];
									j++;
                                 //printf("%c",resstr[i]);
                              }
								  
								   revans[j]='\0';
							 //printf("this is decimal");
								   printf("%7s",revans);
                                 printf("\n");
							 
								 }

                            }

							return 0;
						}
						 
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

389:- WA

Post by sazzadcsedu » Fri Feb 13, 2009 11:18 pm

PLZ HELP SOMEone.
GOT wa.

Code: Select all

  #include<stdio.h>
                          #include<ctype.h> 
                          #include<math.h>
                          #include<string.h>



                             int power(int a,int b)

							 { 

							  int i,res=1;


								  if(b==0)

								   return 1;

								
								 else 
									 
								 {
								    for(i=1;i<=b;i++)

									{ 

									 res=a*res;

									}
                                    
                                  return res;

								 }

							 }





                             int  main()


						 {   
                              int num,y,len,i,rem,b1,b2,j;
							           char str[10000];
							           char resstr[10000]; 
                              char revans[10000];
                           

								

                              while(scanf("%s %d %d",str,&b1,&b2)==3)
							  {
                               //gets(str);

							   len=strlen(str);
							       
							        
							   

							   
							   
							          num=0;

                                       y=0;


									   
								   for(i=len-1;i>=0;i--)

								   {  
									   if(isdigit(str[i]))  

									   num=num+ ((str[i]-48)*power(b1,y)); 

                                      else 

                                        num=num+ ((str[i]-55)*power(b1,y));


                                        y++;
							 
                                 
								   }
                              

                                i=0;

									   while(num>0)
							 {   

							
								 
                                    rem=num%b2;
                           
                                 if(rem==10)
									 resstr[i]='A';
								 else if(rem==11)

									 resstr[i]='B';
								 else if (rem==12)

									    resstr[i]='C';

								 else if (rem==13)
									     resstr[i]='D';

								 else if (rem==14)
									      resstr[i]='E';
								 else if (rem==15)
									     resstr[i]='F';

								 else 

									 resstr[i]=rem+48;



								//printf("%x",num);
                                num=num/b2;
                                    i++;
								 
                             }

							    resstr[i]='\0';
                 
                                  
                                 if(strlen(resstr)>7)
									 printf("  ERROR\n");

								 else
                                 {

                              
									 j=0;
								for(i=strlen(resstr)-1;i>=0;i--)

							  {

									revans[j]=resstr[i];
									j++;
                         
                              }
								  
								   revans[j]='\0';

								   printf("%7s",revans);
                                 printf("\n");
							 
								 }

                            }

							return 0;
						}
						 
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

qwerty
New poster
Posts: 21
Joined: Sun Feb 08, 2009 5:26 pm
Location: Mumbai,India

PE 389

Post by qwerty » Sat Oct 31, 2009 1:39 am

AC
Last edited by qwerty on Sat Oct 31, 2009 1:19 pm, edited 1 time in total.

qwerty
New poster
Posts: 21
Joined: Sun Feb 08, 2009 5:26 pm
Location: Mumbai,India

Re: 389 WA

Post by qwerty » Sat Oct 31, 2009 1:43 am

sazzadcsedu try this case

Code: Select all

      0 2 3

Angeh
Experienced poster
Posts: 108
Joined: Sat Aug 08, 2009 2:53 pm

Re: 389 - CE

Post by Angeh » Thu Feb 04, 2010 11:31 pm

use strcasecmp();
>>>>>>>>> A2
Beliefs are not facts, believe what you need to believe;)

oamsath
New poster
Posts: 1
Joined: Sat Feb 20, 2010 3:47 pm

Re: 389 - 8 months? WA

Post by oamsath » Sun Feb 21, 2010 1:11 am

Help me plz ,,,

more than two months with this problem not solved,,,

I've tried every test case to exist ... and still WA

Code: Select all

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

unsigned long long int pow ( int x , int y )
{
	unsigned long long int total = 1 ;
	for ( int i = 0 ; i < y ; i ++ )
		total *= x ;
	return total ;
}

string reverse(string str)
{
	string tmp;
	for(int i=str.size()-1;i>=0;i--)
		tmp.append(1,str.at(i));
	return tmp;
}

string int2str ( int x )
{
	string y ;
	while ( x > 0 )
	{
		y.append(1,x%10+'0') ;
		x = x / 10 ;
	}
	y = reverse ( y ) ;
	return y ;
}

string base2dec ( int b1 , string input )
{
	unsigned long long int total = 0 ;
	int step = 0 ;

	for ( int i = input.size()-1 ; i >= 0 ; i -- )
	{
		int value ;
		if ( input.at(i) >= '0' && input.at(i) <= '9' )
			value = input.at(i) - '0' ;
		else
			value = input.at(i) - 'A' + 10 ;
		total += value*pow(b1,step) ;
		step ++ ;
	}
	return int2str(total) ;
}

string dec2base ( int b2 , string input )
{
	int total = atoi(input.c_str()) ;
	string output ;
	while ( total > 0 )
	{
		int ndigit = total%b2 ;
		char cdigit ;
		if ( ndigit < 10 )
			cdigit = ndigit + '0' ;
		else
			cdigit = ndigit - 10 + 'A' ;
		output.append(1,cdigit) ;
		total /= b2 ;
	}
	return reverse ( output ) ;
}

int main ()
{
  	int b1 , b2 ;
	string input ;
	while ( cin >> input >> b1 >> b2 )
	{
		if ( b1 != 10 )
			input = base2dec ( b1 , input ) ;
		if ( b2 != 10 )
			input = dec2base ( b2 , input ) ;
		if ( input.size() > 7 )
			cout << "  ERROR" << endl ;
		else
		{
			int spaces = 7 - input.size() ;
			for ( int i = 0 ; i < spaces ; i ++ )
				cout << " " ;
			cout << input << endl ;
		}
	}
	return 0 ;
}

qwerty
New poster
Posts: 21
Joined: Sun Feb 08, 2009 5:26 pm
Location: Mumbai,India

Re: 389 - 8 months? WA

Post by qwerty » Thu Feb 25, 2010 12:22 am

oamsath wrote:Help me plz ,,,

more than two months with this problem not solved,,,

I've tried every test case to exist ... and still WA
try the input

Code: Select all

      0 10 12
hope it helps

aaa111
New poster
Posts: 14
Joined: Sat Nov 21, 2009 2:55 pm

Re: 389 - 8 months? WA

Post by aaa111 » Fri Apr 09, 2010 5:49 pm

why am i getting WA??

Code: Select all

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>

void Base_Converter(char *number,int base1,int base2);
void rev_str(char *str);

int main()
{
int base1,base2;
char number[9];

while(scanf("%s %d %d",number,&base1,&base2)!=EOF)
    {
    Base_Converter(number,base1,base2);
    }
return 0;
}

void Base_Converter(char *number,int base1,int base2)
{
long int i,num,sum;
char j[2],conv_num[9];

sum=0;
num=1;

rev_str(number);

for(i=0;number[i]!='\0';i++)
    {
    if((number[i]>='0')&&(number[i]<='9'))
        num=(int)number[i]-48;
    else if((number[i]>='A')&&(number[i]<='F'))
        num=(int)number[i]-55;
    sum+=(int)(num*pow(base1,i));
    
    }

i=0;
if(sum==0)
    printf("%7d",sum);
else
{
while(sum!=0)
    {
    num=(sum%base2);
    if(num>9)
        j[0]=(char)(num+'7'); 
    else
        j[0]=(char)(num+'0');
    conv_num[i]=j[0];
    sum=sum/base2;
    i++;
    }
}
conv_num[i]='\0';

i=0;
while(conv_num[i]!='\0')
      i++;

rev_str(conv_num);

if(i<=7)
   printf("%7.7s\n",conv_num);
else
   printf("  Error\n");
}


void rev_str(char *str)
{
int len,i,j;
char ch;

len=strlen(str);
j=len-1;

if(len>1)
   {
   len=len/2;
   }

for(i=0;i<len;i++)
    {
    ch=str[i];
    str[i]=str[j];
    str[j]=ch;
    j--;
    }
}


sirius
New poster
Posts: 4
Joined: Sat Aug 09, 2008 6:51 am

Re: 389 - 8 months? WA

Post by sirius » Mon Apr 12, 2010 5:50 am

@aaa111

For input: 0 10 12
Your program give output : " 0 ",
And for input: FF 16 2
Your program give output: " Error".

While the output should be " 0" and " ERROR" :D
Had trying to fix this issue, your code will be accepted. Good luck! :)

aaa111
New poster
Posts: 14
Joined: Sat Nov 21, 2009 2:55 pm

Re: 389 - 8 months? WA

Post by aaa111 » Mon Apr 12, 2010 5:52 pm

Thanks a lot sirius.I got ACC.

Post Reply

Return to “Volume 3 (300-399)”