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

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

Moderator: Board moderators

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

Postby ahah2002 » Fri Oct 27, 2006 4:55 am

I got WA for 424, but I don't know what is wrong of my code, pls help...

#include <iostream>
#include <string>
using namespace std;
int main(){
string str[110];
string str2[110];
string result;
char* ch;
char p;
int i,j,k;
int maxl;
int x10,x1;
int n;

i=0;
do{

cin>>str[i];
if (str[i][0]=='0' && (str[i].length()==1)){
break;
}
i++;
}while (1);

maxl=str[0].length();
for (j=0;j<i;j++){
if (str[j].length()>maxl){
maxl=str[j].length();
}
for (k=0;k<str[j].length();k++){
str2[j]+=str[j][str[j].length()-1-k];
}
}

n=0;
x10=0;
x1=0;
for (j=0;j<maxl;j++){
for (k=0;k<i;k++){
if (j<str2[k].length()){
p=str2[k][j];
ch=&p;
n+=atoi(ch);
}
}
n+=x10;
x1=n%10;
x10=n/10;
result[j]='0'+x1;
if (j==maxl-1){
if (n>=10){
result[++j]='0'+x10;
}
}

n=0;
}


for(i=j;i>=0;i--){
cout<<result[i];
}


return 0;
}
ahah2002
New poster
 
Posts: 5
Joined: Mon Sep 18, 2006 4:09 pm

Postby Wei-Ming Chen » Sun Oct 29, 2006 9:21 am

Your output existed a space before the answer.

If the input is
Code: Select all
5
5
5
0


The output is
Code: Select all
15


But your output is
Code: Select all
 15
Wei-Ming Chen
Experienced poster
 
Posts: 122
Joined: Sun Nov 13, 2005 10:25 am
Location: Taiwan

Postby emotional blind » Mon Oct 30, 2006 1:25 pm

input:
Code: Select all
99
99
99
99
99
99
99
99
99
99
99
99
99
0


Your output:
Code: Select all
 <87

:o
User avatar
emotional blind
A great helper
 
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh

i am getting wrong answer

Postby chinmoy kanti dhar » Sun Jul 15, 2007 10:31 pm

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[i]-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[i]!=0)b=1;
if(b==1)
printf("%ld",a[i]);}
if(b==1)
printf("\n");m=0; //break;
for(i=0;i<3000;i++)a[i]=0;
}

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

Postby hridoy » Wed Jan 09, 2008 8:18 pm

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];
}

























hridoy
New poster
 
Posts: 21
Joined: Tue May 08, 2007 10:30 am
Location: Dhaka

424 (getting CE) pliz anyone help

Postby kbr_iut » Sun Apr 06, 2008 1:14 pm

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...............................
User avatar
kbr_iut
Experienced poster
 
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH

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

Postby Jan » Sun Apr 06, 2008 1:49 pm

You can't use 'strrev()'. Use your own.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

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

Postby theharshest » Mon Aug 04, 2008 11:26 pm

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"
User avatar
theharshest
New poster
 
Posts: 20
Joined: Thu Jan 17, 2008 10:47 pm
Location: India

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

Postby lnr » Fri Oct 17, 2008 8:37 pm

To theharshest.
Try this.
Input:
Code: Select all
46457567568
3564567756756
2342343
0

Output:
Code: Select all
3611027666667
User avatar
lnr
Experienced poster
 
Posts: 134
Joined: Sat Jun 30, 2007 2:52 pm
Location: (DU,CSE)Dhaka,Bangladesh

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

Postby sazzadcsedu » Tue Dec 30, 2008 11:18 pm

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;
               }
 
sazzadcsedu
Experienced poster
 
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.

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

Postby shiplu_1320 » Wed Dec 31, 2008 12:16 am

I think you have missed a newline.
Good luck :)
A learner......
shiplu_1320
New poster
 
Posts: 32
Joined: Sat Dec 29, 2007 9:08 pm
Location: CSEDU , Dhaka


Return to Volume IV

Who is online

Users browsing this forum: No registered users and 1 guest