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

chinmoy kanti dhar
New poster
Posts: 19
Joined: Fri Jun 22, 2007 6:17 pm
Location: bangladesh

why do i get wrong answer,help me please 424

Post by chinmoy kanti dhar »

#include<stdio.h>
#include<string.h>
void main()
{
long b,n,m=0,i,p,j,q,k,a[3000]={0};
char c[300];
//freopen("c:\\123.txt","w",stdout);
while(scanf("%s",c)==1)
{
p=strlen(c);k=1;n=0;
if(strcmp(c,"0")!=0)
{
for(i=p-1;i>=0;i--)
{
a[k]=a[k]+(c-48)+n;
if(a[k]>9)
{a[k]=a[k]%10;n=1;}
else n=0;
k++; if(k>m)m=k;
}
if(i<0&&n==1)
{
a[k]=1+a[k];
if(a[k]>9)while(1)
{a[k]=a[k]%10;k++;a[k]=a[k]+1;if(a[k]<9)break;}
else k++;
if(k>m)m=k;
}
}
else
{
b=0;
for(i=m;i>=1;i--)
{if(a!=0)b=1;
if(b==1)
printf("%ld",a);}
if(b==1)
printf("\n");m=0; break;
for(i=0;i<3000;i++)a=0;
}

}
}
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Search the board first. Don't open a new thread if there is one already. Read the following thread before posting anything...

http://online-judge.uva.es/board/viewtopic.php?t=17684
Ami ekhono shopno dekhi...
HomePage
chinmoy kanti dhar
New poster
Posts: 19
Joined: Fri Jun 22, 2007 6:17 pm
Location: bangladesh

i am getting wrong answer

Post by chinmoy kanti dhar »

here is the code:-

#include<stdio.h>
#include<string.h>
void main()
{
long b,n,m=0,i,p,j,q,k,a[3000]={0};
char c[300];
//freopen("c:\\123.txt","w",stdout);
while(scanf("%s",c)==1)
{
p=strlen(c);k=1;n=0;
if(strcmp(c,"0")!=0)
{
for(i=p-1;i>=0;i--)
{
a[k]=a[k]+(c-48)+n;
if(a[k]>9)
{a[k]=a[k]%10;n=1;}
else n=0;
k++; if(k>m)m=k;
}
if(i<0&&n==1)
{
a[k]=1+a[k];
if(a[k]>9)while(1)
{a[k]=a[k]%10;k++;a[k]=a[k]+1;if(a[k]<9)break;}
else k++;
if(k>m)m=k;
}
}
else
{
b=0;
for(i=m;i>=1;i--)
{if(a!=0)b=1;
if(b==1)
printf("%ld",a);}
if(b==1)
printf("\n");m=0; //break;
for(i=0;i<3000;i++)a=0;
}

}
}
nahid
New poster
Posts: 18
Joined: Wed Oct 04, 2006 8:59 pm
Location: DHAKA,BANGLADESH
Contact:

424 - Integer Inquiry

Post by nahid »

i've solved the problem 424 on visual c++. And run this code on visual C++ and Turbo C. For both compiler i've got expected output. But in case of submission i'm having RE for C. and CE for C++. what might be the problem? if u need to see my code, i'll post it. :roll: :roll: :roll: :roll:

OK, here is my code


Code: Select all

Code Removed After AC
Last edited by nahid on Tue Jan 01, 2008 11:01 pm, edited 3 times in total.
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

Yes, it would help if you post your code.
Detecting CE without looking at the code can be a hard task :P
nahid
New poster
Posts: 18
Joined: Wed Oct 04, 2006 8:59 pm
Location: DHAKA,BANGLADESH
Contact:

posted code

Post by nahid »

I've posted the code
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

You have to use:

int main() {
<code>
return 0;
}

to avoid compile error.
nahid
New poster
Posts: 18
Joined: Wed Oct 04, 2006 8:59 pm
Location: DHAKA,BANGLADESH
Contact:

Got AC, but.....

Post by nahid »

Thanks sohel vai. i've got AC. :D :D :D
Could u pls explain, why it is RE for C. another thing, on the descriptin of the problem, here didn't mention about new line after the output. is that rule, to print a new line after all the output (if it doesn't mention anything about newline)???? :roll: :roll: :roll:
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

Yes, usually each output is on a separate line unless specified otherwise.
hridoy
New poster
Posts: 21
Joined: Tue May 08, 2007 10:30 am
Location: Dhaka
Contact:

Post by hridoy »

WHy am I getting WA???
Can anyone plz explain...

Code: Select all

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
                                                      
void reverse(char *s1)
{
	int n,i,j;
	char s2[1000];
	n=strlen(s1);
	for(i=n-1,j=0;i>=0;i--)
	{
		if(j==0&&s1[i]=='0')
			continue;
		s2[j]=s1[i];
		j++;
	}
	s2[j]='\0';
	strcpy(s1,s2);
}

void summation(char *sum, char *s1, char *s2)
{
	char c1, c2;
	int m,n,i,j,carry=0,s,p;
	m=strlen(s1);
	n=strlen(s2);
	p=(m>n)? m:n;
	for(i=p-1,j=0;i>=0;i--)
	{
		if(m<n)
		{
			c1=s1[m-1];
			m--;
			if(m<0)
				c1='0';
			c2=s2[i];
		}
		else
		{
			c1=s2[n-1];
			n--;
			if(n<0)
				c1='0';
			c2=s1[i];
		}
		s=c1+c2-2*'0'+carry;
		sum[j]=s%10+'0';
		carry=s/10;
		j++;
	}
	if(carry!=0)
		sum[j++]=carry+'0';
	sum[j]='\0';
	reverse(sum);
}

main()
{
	int i,n,j;
	char s[105][105],*sum;
	for(i=0;;i++)
	{
		cin >> s[i];
		if(s[i][0]=='0'&&s[i][1]=='\0')
			break;
	}
	n=i;
	
	for(i=0;i<(n-1);i++)
	{
		sum=new char[105];
		summation(sum,s[i],s[i+1]);
		strcpy(s[i+1],sum);
		if(i!=(n-2))
			delete [] sum;
	}
	j=i-1;
	n=strlen(sum);
	for(i=0;i<n;i++)
		cout << sum[i];
}

























kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:

424 (getting CE) pliz anyone help

Post by kbr_iut »

AC code deleted.
Last edited by kbr_iut on Fri Oct 24, 2008 6:07 pm, edited 2 times in total.
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

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

Post by Jan »

You can't use 'strrev()'. Use your own.
Ami ekhono shopno dekhi...
HomePage
gr40210
New poster
Posts: 1
Joined: Fri May 09, 2008 9:46 am

424 [wa]

Post by gr40210 »

i have try it for so long time
plz tell me why??

Code: Select all

#include <iostream>
#include <cstring>

using namespace std;

void stringaddintfront(string &nstring,int c)
{
   char self = c % 10 + '0';
   string addstr;
   addstr.push_back(self);
   nstring = addstr + nstring;

} 

int main(int argc, char *argv[])
{
    string in1,in;
    cin >> in1;
    
    int carry;
    string newstring;
    
    while(1)
    {
        
       if( cin.eof() ) break;
    
        while( cin >> in && in != "0" )
        {
         newstring = "";       
         carry=0; 
        
         //add digit 12+1345 >> 0012 + 1345 
         // or  add digit 1234+13 >> 1234 + 0013 
         int did = abs((int)in.length() - (int)in1.length());
         if( in.length() > in1.length() )
          for(int i=0; i<did; i++)
           in1 = "0" + in1;
         else if( in1.length() > in.length() )
          for(int i=0; i<did; i++)
           in = "0" + in;     
        
         for(int i=in.length()-1; i>=0; i--)
         {
           int s = (in[i] - '0') + (in1[i] - '0') + carry ;
           carry =  s / 10 ;
           
           stringaddintfront(newstring,s % 10); 
         }
        
           if(carry != 0)
             stringaddintfront(newstring,carry); 

           in1 = newstring;
         }
    
         cout << in1 << endl;
         in1 = "";
    
    }
    //system("PAUSE");
    return 0;
}
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 424 [wa]

Post by Jan »

Search the board first. Don't open a new thread if there is one already.
Ami ekhono shopno dekhi...
HomePage
theharshest
New poster
Posts: 20
Joined: Thu Jan 17, 2008 10:47 pm
Location: India

What's wrong in the code..?? :(

Post by theharshest »

Code: Select all

#include<iostream>
#include<string>
#include<vector>
#include<cmath>

using namespace std;

int main()
{
 string str,ts;
 vector<string> s;
 int size;
 
 cin>>str;
 
 while(str!="0")
 {
  s.push_back(str);
  cin>>str;               
 }
 
 size=s[0].size();
 for(int i=1;i<s.size();i++)
 {
  size=max(size,int(s[i].size()));        
 }
 
 for(int i=0;i<s.size();i++)
 {
  ts.clear();
  if(s[i].size()<size)
  {
   for(int j=0;j<size-s[i].size();j++)
   ts.push_back('0');
   
   for(int k=size-s[i].size(),p=0;k<size;k++,p++)
   ts.push_back(s[i][p]);
   
   s[i]=ts;                    
  }        
 }
 
// cout<<s[0]<<endl<<s[1]<<endl<<s[2]<<endl;
 
 int sum=0;
 string final;
 bool carry=0;
 
  for(int i=size-1;i>=0;i--)
  {
   if(carry==0)
   sum=0;
   else
   sum=1;
   
   for(int j=0;j<s.size();j++)
   {        
   sum+=int(s[j][i])-48;
   }
   if(sum/10==0)
   {
    final.push_back(sum);
    carry=0;
   }
   else
   {
    final.push_back(sum/10);
    carry=1;    
   }
  } 
  
  for(int i=final.size()-1;i>=0;i--)
  cout<<final[i];  
}
"if u r goin thru hell, keep goin"
Post Reply

Return to “Volume 4 (400-499)”