424 - Integer Inquiry

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

Moderator: Board moderators

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

Re: 424 Got a WA...Pls help!!!!!

Post by lnr »

To theharshest.
Try this.
Input:

Code: Select all

46457567568
3564567756756
2342343
0
Output:

Code: Select all

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

424 Got a WA...Pls help!!!!!

Post by sazzadcsedu »

whats wrong with my code??
i got wA.

Code: Select all

          #include<stdio.h>
         #include<string.h> 
         
                int   main()
 
		{

               int  i,add=0;
			   int j=0,carry,sum,max;
			   char num[102];
			   int  res[105];


			    for(i=0;i<105;i++)
				{
				 res[i]=0;	
				}

				  max=0;
			   
		

             while(scanf("%s",num)==1)
		 
			 {
                 if(num[0]==48 && num[1]=='\0')

				 break;

                 

			          
                 sum=0;  
				 carry=0;
				 j=0;
			 for(i=strlen(num)-1;i>=0;i--)

			 {     
                   sum=res[j]+(num[i]-48)+carry;
				    
				    if(sum>9)
					{
				  
					   res[j]=(sum%10) ;
				       carry=sum/10; 
					 
					}
					else 
					{
						res[j]=sum ;
                        carry=0 ;
					}
							

					 j++;
			
                    
			 }           
			          while(carry>0)
					  {
                      res[j]=(carry+res[j]);

					  carry=res[j]/10;
					  res[j]=res[j]%10;
					   
					   j++;
				
					  }

                     if(max<j-1)
						max=j-1;
					   
				}      

				        
                 
                       
                       
                     

				

				        for(i=max;i>=0;i--)
					  {
						  printf("%d",res[i]);
					  }

				           
                      

                        return 0;
				   }
 
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
shiplu_1320
New poster
Posts: 32
Joined: Sat Dec 29, 2007 9:08 pm
Location: CSEDU , Dhaka
Contact:

Re: 424 Got a WA...Pls help!!!!!

Post by shiplu_1320 »

I think you have missed a newline.
Good luck :)
A learner......
the thing
New poster
Posts: 1
Joined: Sun Nov 01, 2009 9:07 am

424

Post by the thing »

my code of 424 is below.i ma gettin WA.help me PLz



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
//freopen("vli.txt","r",stdin);
int m,l,i,carry,ans[105],allow;
char str[101],temp[10];
for(i=0;i<105;i++)
{
ans=0;
}
while((scanf("%s",str))==1)
{
if(!(strcmp(str,"0"))) break;

m=strlen(str);
l=m-1;
carry=0;
for(i=104;i>=0;i--)
{
ans=ans+carry;
if(l>=0)
{
temp[0]=str[l];
temp[1]='\0';
ans=ans+atoi(temp);
}
carry=0;
if(ans>9)
{
carry=ans/10;
ans=ans%10;
}
l--;
}
}
allow=0;
for(i=0;i<105;i++)
{
if(ans==0)
{
if(allow) printf("%d",ans[i]);
}
else
{
printf("%d",ans[i]);
if(!allow) allow=1;
}

}
return 0;
}
shaon_cse_cu08
New poster
Posts: 50
Joined: Tue May 25, 2010 9:10 am
Contact:

Getting The Msg

Post by shaon_cse_cu08 »

Hey Can any one tel me why i m getting this msg :
(Compilation Error)
code.cpp: In function 'int main()':
code.cpp:23: error: 'strrev' was not declared in this scope
code.cpp:58: error: 'strrev' was not declared in this scope

My code in visual c++ is as follows:


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{

char str[101][101],str3[102];

int i,j,k,m,x=0,carry=0,max=0;


for(i=0;i<101&&gets(str);i++)
{
if(atoi(str)==0)
break;
if(strlen(str)>=max)
max=strlen(str);
}
j=i;
for(i=0;i<j;i++)
{
strrev(str);
for(k=strlen(str);k<max;k++)
{
str[k]='0';
}
str[k]='\0';
strrev(str);
}


for(k=max-1,m=0;k>=0,m<max;k--,m++)
{
x=0;
for(i=0;i<j;i++)
{

x+=(str[k]-'0')+carry;

carry=0;
}
if(x>9)
{
carry=x/10;
str3[m]=x%10+'0';
}
else
str3[m]=x+'0';
}

str3[max]=carry+'0';

if((str3[max]-'0')==0)
str3[max]='\0';
else
str3[max+1]='\0';

puts(strrev(str3));

return 0;
}
:cry:
I'll keep holding on...Until the walls come tumbling down...And freedom is all around ..... :x
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Re: 424 - Integer Inquiry RE

Post by sohel »

strrev() is not part of ANSI C.
You have to write your own string reverse function.
shaon_cse_cu08
New poster
Posts: 50
Joined: Tue May 25, 2010 9:10 am
Contact:

Re: 424 - Integer Inquiry RE

Post by shaon_cse_cu08 »

Now This is WA....
What could possibly be wrong....plz Help :(

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void str_rev(char str[]);
int main()
{

char str[101][101],str3[102];

int i,j,k,m,x=0,carry=0,max=0;


for(i=0;i<101;)
{
gets(str);
if(atol(str)<0)
continue;
else
{
if(strcmp(str,"0")==0)
break;
if(strlen(str)>=max)
max=strlen(str);
i++;
}
}
j=i;
for(i=0;i<j;i++)
{
str_rev(str);
for(k=strlen(str);k<max;k++)
{
str[k]='0';
}
str[k]='\0';
str_rev(str);
}


for(k=max-1,m=0;k>=0,m<max;k--,m++)
{
x=0;
for(i=0;i<j;i++)
{

x+=(str[i][k]-'0')+carry;

carry=0;
}
if(x>9)
{
carry=x/10;
str3[m]=x%10+'0';
}
else
str3[m]=x+'0';
}

str3[max]=carry+'0';

if((str3[max]-'0')==0)
str3[max]='\0';
else
str3[max+1]='\0';

str_rev(str3);
puts(str3);

printf("\n");

return 0;
}

void str_rev(char str[])
{
char a;
int i,j,n;

n=strlen(str);

for(i=0,j=n-1;i<n/2&&j>=0;i++,j--)
{
a=str[i];
str[i]=str[j];
str[j]=a;
}

str[n]='\0';

}
I'll keep holding on...Until the walls come tumbling down...And freedom is all around ..... :x
naheed
New poster
Posts: 8
Joined: Sun Oct 25, 2009 7:20 pm

424-Integer Inquiry. COMPILATION ERROR!!!!

Post by naheed »

Why CE in this Java program?????Runs farely well in Netbeans,JCreator..No error.PLZ HELP..

Code: Select all

import java.util.*;
import java.math.*;

public class sum {
    public static void main(String[] args) {
    	 try{
        	BigInteger sum;
        	BigInteger zero=new BigInteger("0");
        	sum=zero;
        	Scanner in=new Scanner(System.in);

       		while(true){

        	BigInteger i;
        	i=in.nextBigInteger();
          	if(i.equals(zero))
              	break;
        	else
        	    sum=sum.add(i);
        	}
         	System.out.println(sum);
        }catch(Exception e){

        }
    }
}
naheed
New poster
Posts: 8
Joined: Sun Oct 25, 2009 7:20 pm

424-Integer Inquiry. COMPILATION ERROR!!!!

Post by naheed »

Why CE in my first java submission??? SMONE PLZ help!

Code: Select all

import java.util.*;
import java.math.*;

public class sum {
    public static void main(String[] args)throws IOException {
    	 try{
        	BigInteger sum;
        	BigInteger zero=new BigInteger("0");
        	sum=zero;
        	Scanner in=new Scanner(System.in);
       
       		while(true){
            
        	BigInteger i;
        	i=in.nextBigInteger();
          	if(i.equals(zero))
              	break;
        	else
        	    sum=sum.add(i);
        	}
         	System.out.println(sum);
        }catch(Exception e){
        
        }
    }
}
shaon_cse_cu08
New poster
Posts: 50
Joined: Tue May 25, 2010 9:10 am
Contact:

Re: 424 - Integer Inquiry RE

Post by shaon_cse_cu08 »

No Reply No Reply...... :( :( :(
I'll keep holding on...Until the walls come tumbling down...And freedom is all around ..... :x
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Re: 424 - Integer Inquiry RE

Post by sohel »

You can generate your own test cases and compare the outputs using gmark's site.
http://uvatoolkit.com/problemssolve.php
programmin
New poster
Posts: 2
Joined: Mon Jun 28, 2010 11:51 pm

Re: 424-Integer Inquiry. COMPILATION ERROR!!!!

Post by programmin »

Did you try changing "public class sum" to "public class Main" before submitting?
Sadasidha08
New poster
Posts: 3
Joined: Mon Apr 05, 2010 8:09 pm

Re: 424 - Integer Inquiry RE

Post by Sadasidha08 »

ur strings length is very small make those 500
dushyant89
New poster
Posts: 2
Joined: Fri Sep 17, 2010 12:03 pm

Re: 424 - Integer Inquiry RE

Post by dushyant89 »

whats wrong here............??????
im getting correct output for all test cases known ever.....
please check it.........

#include<iostream>
using namespace std;
int main()
{
char ch[110][110];
int i=0,rem=0,j,k,max=0,disp,a;
char str[200];
while(cin>>ch)
{
if(strlen(ch)==1 && ch[0]=='0')
break;
if(strlen(ch)>max)
max=strlen(ch);
i++;
}
for(k=max-1;k>=0;k--)
{ a=rem;
for( j=0;j<i;j++)
{disp=max-strlen(ch[j]);
if((k-disp)>=0)
a=a+ch[j][k-disp]-48;
}
rem=a/10;
str[k]=a%10 +48;
}
if(rem>0)
cout<<rem;
cout<<str;
return 0;

}
dushyant89
New poster
Posts: 2
Joined: Fri Sep 17, 2010 12:03 pm

Re: 424 - Integer Inquiry RE

Post by dushyant89 »

was getting wrong answer on submitting on uva......
please help friends.........waiting for u.......
Post Reply

Return to “Volume 4 (400-499)”