views:

68

answers:

1

My code is quite simple and straightforward. I get "wrong answer" on submission though. I have no clue why that happens! Here is the code...

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

void sum(string num)
{
  int i,len=num.length();
  int j=len-1;
  int carry=0;
  string answer;
  int s=0;
  for(i=0,j;i<len;i++,j--)
  {
    s = (num[i]-'0')+(num[j]-'0')+carry;
if (s>10&&j!=0)
{ 
      carry = s/10;
  s = s%10; 
    } 
else if (s>10&&j==0)
{ 
      carry=s/10; s=s%10; answer+=s+'0';answer+=carry+'0'; break;
    }   
answer+=s+'0';
  }
  int sz=answer.size();for(int j=sz-1;j>=0;j--) cout<<answer[j];
  }

  int main(int argc,char **argv)
  {
    int n;cin>>n;
    for(int i=0;i<n;i++)
  {
  string no;cin>>no;
  sum(no);cout<<endl;
  }
}
A: 

I haven't analysed your code, but here are few tips about SPOJ:

  1. Re-read the description carefully, see if you haven't missed anything.
  2. Check for edge cases, see if your output makes any sense.
  3. Be careful about whitespace. Check your output for extraneous newlines/spaces at the end.
PiotrLegnica