I am continuosly getting WA in this ques not because of overflow...i hav made my own bigint class in c++ and it is running absolutely fine for all the test cases inputted by me..i hav covered all the corner cases upto my knowledge but still it is giving WA on submission....someone plz give me some tricky test cases so that i can debug more my prog to find the error...
Thanxxx
8 months later
Hey,Thanks!I corrected that and a few other error too..still getting WA!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
char a[110],b[110],c[110],d[110];
void add()
{
int i,j=strlen(a)-1,k=strlen(b)-1,t,r=0,u=0,s=max(strlen(a),strlen(b)),temp,flag;
c[s+1]='\0';
for(i=s;i>=0 && j>=0 && k>=0;i--)
{
t=a[j--]+b[k--]-48;
temp=t-48+u;
if((t-48+u)>9)
{
r=(t-48+u)%10;
u=(t-48+u)/10;
c[i]+=r;
//c[i-1]+=u;
}
else
{
c[i]+=t-48+u;
u=0;
}
if(i==1)
c[0]+=u;
//cout<<i<<" "<<a[j+1]<<" "<<b[k+1]<<" "<<c[i]<<" "<<r<<" "<<u<<" "<<temp<<endl;
}
while(j>=0)
c[i--]+=a[j--]-48;
}
void divide()
{
int k=0,i=0,r=0,t;
while(c[i]==48)
i++;
//cout<<"i t d[k] r\n";
//cout<<c<<endl;
for(;i<strlen(c);i++)
{
if(c[i]==48 && r==0)
{
d[k++]=48;
t=0;
}
else
{
t=(r*10+c[i]-48);
if(t==1 && i==strlen(c)-1)
d[k++]=48;
else
{
if(t<=1 && i<strlen(c)-1)
{
t=c[i]*10+c[++i]-480-48;
//if(k!=0)
d[k++]=48;
}
d[k++]=(t/2+48);
r=t%2;
}
}
//cout<<i<<" "<<t<<" "<<d[k-1]<<" "<<r<<endl;
}
d[k]='\0';
}
void subtract()
{
int d=strlen(a)-1,e=strlen(b)-1,s=max(strlen(a),strlen(b)),temp;
c[s]='\0';
int i;
for(i=s-1;i>=0 && d>=0 && e>=0;i--)
{
if(a[d]<b[e])
{
temp=d;
while(a[--temp]==48)
a[temp]=9+48;
a[temp]=a[temp]-1;
c[i]=a[d--]+10-b[e--]+48;
}
else
c[i]=a[d--]-b[e--]+48;
}
while(d>=0)
c[i--]=a[d--];
}
int main()
{
int l=10;
while(l--)
{
for(int t=0;t<105;t++)
c[t]=d[t]=48;
scanf("%s%s",&a,&b);
if(!strcmp(a,b))
printf("%s\n0\n",a);
else{
add();
divide();
int i=0;
while(d[i]==48)
i++;
while(d[i]!='\0')
printf("%c",d[i++]);
printf("\n");
for(int t=0;t<105;t++)
c[t]=d[t]=48;
subtract();
divide();
i=0;
while(d[i]==48)
i++;
while(d[i]!='\0')
printf("%c",d[i++]);
printf("\n");}
}
return 0;
}
2 months later
getting runtime error SIGSEGV. somebody help
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
class bigint{
private:
int len;
int *num;
public:
bigint(){
len=1;
num=new int[1];
num[0]=0;
}
bigint(int x){
num=new int[x];
len=x;
}
bigint(char *str){
int i;
len=strlen(str);
num=new int[len];
for(i=0;i<len;i++)
num[i]=((int)str[i])-48;
}
bigint( bigint & copy){
int i;
len=copy.len;
num=new int[len];
for(i=0;i<len;i++)
num[i]=copy.num[i];
}
void operator =(bigint & another){
int i;
len=another.len;
free(num);
num=new int[len];
for(i=0;i<len;i++)
num[i]=another.num[i];
}
void operator = ( char *s){
int i;
len=strlen(s);
num=new int[len];
for(i=0;i<len;i++)
num[i]=(int)(s[i])-48;
}
void get(){
char str[100000];
int i;
cin>>str;
len=strlen(str);
num=new int[len];
for(i=0;i<len;i++)
{
num[i]=(int)str[i]-48;
}
}
void print(){
int i;
for(i=0;i<len;i++)
cout<<num[i];
}
int operator < (bigint & another){
int i;
if(len>another.len)
return 0;
if(len<another.len)
return 1;
for(i=0;i<len;i++)
if(num[i]==another.num[i])
continue;
else if(num[i]>another.num[i])
return 0;
else
return 1;
return 0;
}
int operator ==(bigint & another)
{
int i;
if(len>another.len||len<another.len)
return 0;
for(i=0;i<len;i++)
if(num[i]!=another.num[i])
return 0;
return 1;
}
void clear(){
int i,j;
for(i=0;i<len && num[i]==0 ;i++);
len-=i;
for(j=0;j<len;j++)
num[j]=num[j+i];
if(len==0)
{
len=1;
num[0]=0;
}
}
char* operator + (bigint other){
int swapped=0,carry=0,i,j,k,resultlength=
len>other.len? len+1 : other.len+1;
bigint result(resultlength),temp(resultlength);
k=result.len-1;
//making the number having more length as the first number
if(len<other.len) {
swapped=1;
temp=(*this);
(*this)=other;
other=temp;
}
//adding until equal length
for(i=len-1,j=other.len-1;j>=0;j--,i--,k--)
{
result.num[k]=num[i]+other.num[j]+carry;
if(result.num[k]>9){
result.num[k]-=10;
carry=1;
}
else carry=0;
}
//remaining numbers in the first number...
for(;i>=0;i--,k--){
result.num[k]=carry+num[i];
if(result.num[k]>9){
result.num[k]-=10;
carry=1;
}
else carry=0;
}
if(carry==0){
for(i=0;i<result.len-1;i++)
result.num[i]=result.num[i+1];
result.len--;
}
else
result.num[0]=1;
result.clear();
// result.print(); cout<<endl;
char * resultstring=new char[result.len+1];
for(i=0;i<result.len;i++)
resultstring[i]=(char)(result.num[i]+48);
resultstring[i]='\0';
if(swapped==1){
bigint temp= (*this);
*this= other;
other= temp;
swapped=0;
}
return resultstring;
}
char* operator * (bigint other){
bigint result(other.len+len);
int carry,l,i,j,k,tempnum;
char * resultstring;
bigint tempresults[other.len]; //declaring an array of temp bigintegers
for(i=0;i<result.len;i++)
result.num[i]=0;
for(i=0;i<other.len;i++) //finding all the tempresults
{ carry=0;
tempnum=other.num[other.len-i-1];
//current multiplier
//cout<<"tempnum=" <<tempnum<<endl;
tempresults[i].len=len+i+1;
//allocating memory for tempbigints
tempresults[i].num=new int[tempresults[i].len];
for(k=tempresults[i].len-1,l=len-1; l>=0;k--){
if(k>len)
{
tempresults[i].num[k]=0;
continue;
}
else{
tempresults[i].num[k]=(tempnum*num[l--])+carry;
if(tempresults[i].num[k]>9){
carry=tempresults[i].num[k]/10;
// cout<<"carry="<<carry<<endl;
tempresults[i].num[k]%=10;
}
else
carry=0;
}
}
if(carry==0){
tempresults[i].len--;
for(j=0;j<tempresults[i].len;j++)
tempresults[i].num[j]=tempresults[i].num[j+1];
}
else
tempresults[i].num[0]=carry;
// tempresults[i].print();
}
for(i=0;i<other.len;i++)
result=result+tempresults[i];
resultstring=new char[result.len+1];
for(j=0;j<result.len;j++)
resultstring[j]=(char)(result.num[j]+48);
resultstring[j]='\0';
return resultstring;
}
char * operator - (bigint other){ //returns only the difference b/w two no.s
char *result;
int i,j,k,l,x,swapped=0;
bigint first(this->len);
bigint temp;
for(i=0;i<len;i++)
first.num[i]=this->num[i];
//(*this).print(); cout<<endl;
//other.print(); cout<<endl;
if(first<other) //keeping the greater no. as the first
{
swapped=1;
temp=first;
first=other;
other=temp;
}
result=new char[first.len+1]; //allocating memory for result
for(i=first.len-1,j=other.len-1,k=0; j>=0; i--,j--,k++){
if(first.num[i]>=other.num[j])
{
result[k]=(char)(first.num[i]-other.num[j]+48);
continue;
}
first.num[i]+=10;
result[k]=(char)(first.num[i]-other.num[j]+48);
l=i-1;
while(first.num[l]==0&&l>=0){
first.num[l]=9;
l--;
}
first.num[l]--;
}
while(i>=0){
result[k]=(char)(first.num[i]+48);
k++;
i--;
}
result[k]='\0';
//cout<<result<<" reversed to ";
//Now the result is in reverse order of length k.. so reverse the result..
for(i=0,j=k-1;i<j ; i++,j--){
l=result[i];
result[i]=result[j];
result[j]=l;
}
//cout<<result<<endl;
//now omitting the leading zeroes
for(i=0;i<k && result[i]==0 ;i++) ;
//the no. of leading zeroes is i 0001234
for(j=0;j<k-i+1;j++)
result[j]=result[j+i];
if(strlen(result)==0) return "0";
if(swapped==1){
bigint temp= (*this);
*this= other;
other= temp;
swapped=0;
}
return result;
}
char * operator /( bigint dividend){
bigint x,zero("0"),one("1"),nine("9"), divisor,current,s,remainder;
char *quotient=new char[len],*curr;
divisor=(*this);
int length=dividend.len,position=0,i,start=0,q=0,oldstart;
if(dividend==zero){cout<<"\nDivide error.."; exit(0);}
while(position<divisor.len){
start=position;
oldstart=start;
position+=length;
free(curr);
curr=new char[remainder.len+length+1];
for(i=0;i<remainder.len;i++)
{
curr[i]=(char)(remainder.num[i]+48);
}
for(;start<position;i++,start++){
curr[i]=(char)(divisor.num[start]+48);
}
curr[i]='\0';
//cout<<curr<<endl;
current=curr;
current.clear();
again:
if(current<dividend)
{
start=start-length;
position++; if(position>divisor.len)
{quotient[q++]='0';quotient[q]='\0';return quotient;}
free(curr);
curr=new char[remainder.len+length+2];
for(i=0;i<remainder.len;i++)
{
curr[i]=(char)(remainder.num[i]+48);
}
for(start=oldstart;start<position;i++,start++){
curr[i]=(char)(divisor.num[start]+48);
}
quotient[q++]='0';
curr[i]='\0';
current=curr;
current.clear();
if(current<dividend) goto again;
}
//cout<<"current=" ;current.print();cout<<endl;
bigint product;
for(x=one;x<nine||x==nine;++x)
{
product=x*dividend; product.clear();
if(current<product)
break;
}
x=x-one;
s=x*dividend;
//cout<<"s= "; s.print(); cout<<endl;
quotient[q++]=(char)(x.num[0]+48);
remainder=current-s;
remainder.clear();
//cout<<"R= ";remainder.print(); cout<<endl;
length=1;
}
quotient[q]='\0';
return quotient;
}
void operator ++(){
bigint one("1");
(*this)=(*this)+one;
}
~bigint(){
free(num);
}
};
int main(){
bigint a,b,two("2"),tt,total,more;
int t=10;
while(t--){
total.get();
more.get();
if(total<more)
{tt=total;
total=more;
more=tt;
}
a=total+more;
a=a/two;
b=a-more;
a.clear(); b.clear();
a.print(); cout<<endl;
b.print(); cout<<endl;
}
return 0;
}
9 months later
I am receiving WA all the time... Please help me knowing which test cases is failing, for me the output comes out to be proper. Please help.
#include<iostream>
#include<string.h>
#define MAX 110
using namespace std;
void print(char r[])
{
int l = strlen(r),first = 0;
if(l == 1 && r[0] == '0')
{
cout<<"0";
}
for(int i=0;i<l;i++)
{
if(r[i] - '0' == 0 && first == 0)
continue;
else
{
first = 1;
cout<<r[i];
}
}
}
void sub(char p[],char q[],char r[])
{
int lp = strlen(p);
int lq = strlen(q);
int c = 0,temp,j=0;
for(int i=0;i<lp;i++)
{
bool mm = false;
if((lq - 1 - i >= 0) && (( p[lp-1-i] - '0' == q[lq-1-i] - '0' && c == 0 ) || ( c && (p[lp-1-i] - '0' - c == q[lq-1-i] - '0')) ))
{
r[lp-1-i] = '0';
continue;
}
if(p[lp-1-i] - '0' > 0)
temp = p[lp-1-i] - '0' - c;
else
{
temp = 10 - c; mm = true;
}
if(mm) c=1; else c=0;
if(lq - 1 - i >= 0)
{
if(q[lq-1-i] - '0' == temp)
{
r[lp-1-i] = '0';
continue;
}
if(q[lq-1-i] >= temp + '0')
{
c = 1;
temp = temp + 10 - (q[lq-1-i] - '0');
}
else
temp = temp - (q[lq-1-i] - '0');
}
r[lp-1-i] = temp + '0';
}
r[lp] = '\0';
}
void divby2(char r[],char d[])
{
int len = strlen(r);
bool ntzero = false;
for(int i=0;i<len;i++)
if(r[i] != '0') {ntzero = true; break;}
if(!ntzero)
{
d[0]='0'; d[1]='\0';
return;
}
int a,b,k=0,rem=0,first = 0;
for(int i=0;i<len;i++)
{
if(r[i] - '0' == 0 && first == 0)
continue;
else first = 1;
a = rem*10 + r[i] - '0';
rem = 0;
if(a == 1)
{
a = a*10 + (r[i+1] - '0');
i++;
}
else if( a == 0)
{
d[k++] = 0 + '0';
continue;
}
d[k++] = a/2 + '0';
rem = a%2;
}
d[k] = '\0';
}
main()
{
char p[MAX],q[MAX],r[MAX],d[MAX],e[MAX];
int c = 10;
while(c--)
{
cin>>p; cin>>q;
sub(p,q,r);
//print(r);
divby2(r,d);
sub(p,d,e);
print(e);cout<<endl;
print(d);
if(c != 0)
cout<<endl;
}
return 0;
}
8 years later
I tried all the test cases mentioned above and my code works for them all but it still gives WA. Can someone help me figure out whats wrong with the code?
#include <iostream>
#include<string>
using namespace std;
int main() {
string a,b;
int i,j,cr,s,l1,l2;
for(i=0;i<10;i++){
cin>>a>>b;
cr=0;l1=a.size();l2=b.size();
string c,d;
for(j=0;j<l2;j++){
s=a[l1-1-j]+b[l2-1-j]-96+cr;
c.push_back(s%10+48);
cr=s/10;
}
for(j=0;j<l1-l2;j++){
s=a[l1-l2-1-j]-48+cr;
c.push_back(s%10+48);
cr=s/10;
}
if(cr)
c.push_back(cr);
cr=0;
if(c.size()==1)
d.push_back((c[0]-48)/2+48);
else if(c.size()>1){
s=c[c.size()-1]-48;
if(s>1)
d.push_back(s/2+48);
if(s%2)
cr=10;
for(j=c.size()-2;j>=0;j--){
s=c[j]-48+cr;
d.push_back(s/2+48);
if(s%2)
cr=10;
else
cr=0;
}
}
cout<<d<<endl;
c.clear();
d.clear();
for(j=0;j<l2;j++){
if(a[l1-1-j]==47){
a[l1-1-j]=57;
a[l1-2-j]--;
}
s=a[l1-1-j]-b[l2-1-j];
if(s<0){
s+=10;
a[l1-2-j]--;
}
c.push_back(s+48);
}
for(j=0;j<l1-l2;j++)
c.push_back(a[l1-l2-1-j]);
s=0;
for(j=c.size()-1;j>0;j--){
if(c[j]!='0')
break;
else
s++;
}
c.resize(c.size()-s);
cr=0;
if(c.size()==1)
d.push_back((c[0]-48)/2+48);
else if(c.size()>1){
s=c[c.size()-1]-48;
if(s>1)
d.push_back(s/2+48);
if(s%2)
cr=10;
for(j=c.size()-2;j>=0;j--){
s=c[j]-48+cr;
d.push_back(s/2+48);
if(s%2)
cr=10;
else
cr=0;
}
}
cout<<d<<endl;
}
return 0;
}
Suggested Topics
Topic | Category | Replies | Views | Activity |
---|---|---|---|---|
Help me please! | ProblemSet Archive | 4 | 44 | 28d |
Getting WA on the problem PIE continuously, what am I missing? spoj.com | ProblemSet Archive | 2 | 192 | Mar 8 |
AIBOHPHOBIA - LPS vs Direct Approach | ProblemSet Archive | 1 | 124 | Mar 31 |
What am i Missing | ProblemSet Archive | 1 | 173 | Feb 22 |
Beangame | ProblemSet Archive | 2 | 138 | Apr 9 |