424 - Integer Inquiry
Moderator: Board moderators
-
- New poster
- Posts: 19
- Joined: Fri Jun 22, 2007 6:17 pm
- Location: bangladesh
why do i get wrong answer,help me please 424
#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;
}
}
}
#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;
}
}
}
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
http://online-judge.uva.es/board/viewtopic.php?t=17684
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 19
- Joined: Fri Jun 22, 2007 6:17 pm
- Location: bangladesh
i am getting wrong answer
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;
}
}
}
#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;
}
}
}
424 - Integer Inquiry
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:](./images/smilies/icon_rolleyes.gif)
OK, here is my code
![:roll:](./images/smilies/icon_rolleyes.gif)
![:roll:](./images/smilies/icon_rolleyes.gif)
![:roll:](./images/smilies/icon_rolleyes.gif)
![:roll:](./images/smilies/icon_rolleyes.gif)
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.
posted code
I've posted the code
Got AC, but.....
Thanks sohel vai. i've got AC.
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:](./images/smilies/icon_rolleyes.gif)
![:D](./images/smilies/icon_biggrin.gif)
![:D](./images/smilies/icon_biggrin.gif)
![:D](./images/smilies/icon_biggrin.gif)
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:](./images/smilies/icon_rolleyes.gif)
![:roll:](./images/smilies/icon_rolleyes.gif)
![:roll:](./images/smilies/icon_rolleyes.gif)
WHy am I getting WA???
Can anyone plz explain...
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];
}
-
- Experienced poster
- Posts: 103
- Joined: Tue Mar 25, 2008 11:00 pm
- Location: IUT-OIC, DHAKA, BANGLADESH
- Contact:
424 (getting CE) pliz anyone help
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...............................
It is more tough to become a good person.
I am trying both...............................
Re: 424 Got a WA...Pls help!!!!!
You can't use 'strrev()'. Use your own.
Ami ekhono shopno dekhi...
HomePage
HomePage
424 [wa]
i have try it for so long time
plz tell me why??
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;
}
Re: 424 [wa]
Search the board first. Don't open a new thread if there is one already.
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 20
- Joined: Thu Jan 17, 2008 10:47 pm
- Location: India
What's wrong in the code..?? :(
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"