Page 11 of 22

Posted: Sun Apr 15, 2007 8:49 pm
by Spykaj
You must use long long int instead of long.

Change:

Code: Select all

while(a!=0||b!=0)
{
scanf("%ld %ld", &a, &b); 
to:

Code: Select all

while(scanf("%lld %lld",a,b)&&(a!=0||b!=0))

Posted: Mon Apr 16, 2007 4:49 pm
by ranacse05
LZ try long long int.your vc++ compilar will not support it but judge's pc'll suport it bcoz they use GCC/G++ compilar

Posted: Mon Apr 23, 2007 6:12 am
by legnaleurc
I have Accepted with C, but when I try with C++, I got Wrong Answer.

Code: Select all

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

int main()
{
	string a,b;
	int i,count,len;
	
	while(cin >> a >> b)
	{
		if(a == "0" && b == "0")
			break;
		count = 0;
		if(a.length() > b.length())
			len = b.length();
		else
			len = a.length();
		for(i = 0; i < len; i++)
		{
			if((a[a.length()-1-i]-'0' + b[b.length()-1-i]-'0') >=10)
			{
				if(a.length() > b.length())
				{
					if((a.length()-2-i)>=0)
					{
						a[a.length()-2-i]++;
					}
				}
				else
				{
					if((b.length()-2-i)>=0)
					{
						b[b.length()-2-i]++;
					}
				}
				count++;
			}
		}
		if(a.length() > b.length())
		{
			while((a[a.length()-1-i]-'0')>=10)
			{
				if(a.length()-2-i>=0)
				{
					a[a.length()-2-i]++;
				}
				count++;
				i++;
			}
		}
		else
		{
			while((b[b.length()-1-i]-'0')>=10)
			{
				if(b.length()-2-i>=0)
				{
					b[b.length()-2-i]++;
				}
				count++;
				i++;
			}
		}
		if(count == 1)
			cout << "1 carry operation." << endl;
		else if(count > 1)
			cout << count << " carry operations." << endl;
		else
			cout << "No carry operation." << endl;
	}
	return 0;
}

I have tried this inputs:

Code: Select all

123 456
555 555
123 594
123         777

4235 0

999
989 
0 4336
5210357 1245874 
9090 10 
119 789 
1 999             
999 1
0            0 
but all clear:

Code: Select all

No carry operation.
3 carry operations.
1 carry operation.
2 carry operations.
No carry operation.
3 carry operations.
No carry operation.
3 carry operations.
1 carry operation.
2 carry operations.
3 carry operations.
3 carry operations.
Any idea? :cry:

answer

Posted: Mon Apr 23, 2007 4:34 pm
by ranacse05
why use string?the range fit in to long long int :wink:

Posted: Tue Apr 24, 2007 9:06 am
by legnaleurc
Well, I used same method(char*) in C and Accepted.
I just wonder about why WA in C++.....

Replay

Posted: Tue Apr 24, 2007 7:36 pm
by ranacse05
Try to use long long

wa with this code .. please help

Posted: Wed Jun 06, 2007 7:16 am
by balkrishna
getting wrong answer with this code

Code: Select all

#include<stdio.h>
#include<string.h>
#define MAXDIGITS 20
int check(char a[],char b[])
{
if(a[0]=='0' && b[0]=='0')
return 0;
else 
return 1;
}

char *adjuststr(char a[],char b[],int m,int n)
{
int i;
int dif;
if(m>n){
dif=m-n;
for(i=m;i>dif;i--)
{
b[i-1]=b[i-dif-1];
}
b[m]='\0';
for(i=0;i<dif;i++)
{
b[i]='0';
}
}
else
{

dif=n-m;

for(i=n;i>dif;i--)
{
a[i-1]=a[i-dif-1];
}
a[n]='\0';
for(i=0;i<dif;i++)
{
a[i]='0';
}
}
return a,b;
}
int main()
{
  int carry=0;
  int counter=0;
  int i;
  char num1[MAXDIGITS],num2[MAXDIGITS];
  scanf("%s %s",num1,num2);
while(check(num1,num2)!=0){
  int len1=strlen(num1),len2=strlen(num2);
      if(len1==len2)
	{counter=0;
i=len1-1;
while(num1[i]!='\0' && num2[i]!='\0' && i>=0){
	  if(((num1[i]-'0')+(num2[i]-'0')+carry)>9)
	    counter++;

	  carry=((num1[i]-'0')+(num2[i]-'0')+carry)/10;      
	  i--;
	  }
if(counter==0)
printf("No carry operations\n");
else
 printf("%d carry operations\n",counter);
	}
else {
adjuststr(num1,num2,len1,len2);
len1=strlen(num1);
i=len1-1;
counter=0;
while(num1[i]!='\0' && num2[i]!='\0' && i>=0){
	  if(((num1[i]-'0')+(num2[i]-'0')+carry)>9)
	    counter++;
	  carry=((num1[i]-'0')+(num2[i]-'0')+carry)/10;      
	  i--;
}
if(counter==0)
printf("No carry operations\n");
else
 printf("%d carry operations\n",counter);

}
i=len1-1;
scanf("%s %s",num1,num2);
}
  return 0;
}

Posted: Wed Jun 06, 2007 10:16 am
by Jan
Try the cases.

Input:

Code: Select all

1 999
0 999
999 0
0 0
Output:

Code: Select all

3 carry operations.
No carry operation.
No carry operation.
You are missing full stops. And be carefull about
1 carry operation.
No carry operation.
3 carry operations.

still wa

Posted: Wed Jun 06, 2007 5:58 pm
by balkrishna
thanks for your suggestion of inputs.
they still give the appropriate outputs but judges verdict is still wa....
this code too is not accepted.
#include<stdio.h>
#include<string.h>
#define MAXDIGITS 20
int check(char a[],char b[])
{
if(strcmp(a,"0")==0 && strcmp(b,"0")==0)
return 0;
else
return 1;
}

char *adjuststr(char a[],char b[],int m,int n)
{
int i;
int dif;
if(m>n){
dif=m-n;
for(i=m;i>dif;i--)
{
b[i-1]=b[i-dif-1];
}
b[m]='\0';
for(i=0;i<dif;i++)
{
b='0';
}
}
else
{

dif=n-m;

for(i=n;i>dif;i--)
{
a[i-1]=a[i-dif-1];
}
a[n]='\0';
for(i=0;i<dif;i++)
{
a='0';
}
}
return a,b;
}
int main()
{
int carry=0;
int counter=0;
int i;
char num1[MAXDIGITS],num2[MAXDIGITS];
scanf("%s %s",num1,num2);
while(check(num1,num2)!=0){
int len1=strlen(num1),len2=strlen(num2);
if(len1==len2)
{
counter=0;
i=len1-1;
while(num1!='\0' && num2!='\0' && i>=0){
if(((num1-'0')+(num2-'0')+carry)>9)
counter++;

carry=((num1-'0')+(num2-'0')+carry)/10;
i--;
}
if(counter==0)
{
printf("No carry operation.\n");
}
else if(counter==1)
{
printf("1 carry operation.\n");
}
else
{
printf("%d carry operations.\n",counter);
}
}
else
{
adjuststr(num1,num2,len1,len2);
len1=strlen(num1);
i=len1-1;
counter=0;
while(num1!='\0' && num2!='\0' && i>=0){
if(((num1[i]-'0')+(num2[i]-'0')+carry)>9)
counter++;
carry=((num1[i]-'0')+(num2[i]-'0')+carry)/10;
i--;
}
if(counter==0)
{
printf("No carry operation.\n");
}
else if(counter==1)
{
printf("%d carry operation.\n",counter);
}
else
{
printf("%d carry operations.\n",counter);
}
}
i=len1-1;
scanf("%s %s",num1,num2);
}
return 0;
}

Posted: Wed Jun 06, 2007 11:51 pm
by Jan
In my compiler your code fails for the above set.

WA in my code, :S

Posted: Thu Aug 02, 2007 10:04 pm
by hjuarez
I dont know what is bad in my code, can anybody tell me what is wrong? the judge tell me WA.

Code: Select all

/* @BEGIN_OF_SOURCE_CODE */

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

int getdigit(int index, int number)
{
   int digit;
   
   if( number == 0 && index == 0 )
      return 0;
   
   digit = number / (int)pow((double)10, (double)index);

   if (digit == 0) return 0;

   return digit % 10;
}

int main(int argc, char *argv[])
{
   unsigned int x, y;
   int len, len2, digit1, digit2;
   char num[10];
   char num2[10];
   
   while( cin>>x>>y && !(x==0 && y==0) )
   {
      sprintf(num, "%d", x);
      sprintf(num2, "%d", y);
      len = strlen(num);
      len2 = strlen(num2);
      
      int max = len2 > len ? len2 : len;
      int carry = 0;
      int sum = 0;
      int inc = 0;
      
      for(int i=0; i<max; i++)
      {
         digit1 = len == max ? getdigit(i, x) : 0;
         digit2 = len2 == max ? getdigit(i, y) : 0;
         
         sum = digit1 + digit2 + inc;
         inc = 0;
         inc += (sum > 9);
         carry += inc;
      }
      
      switch(carry)
      {
         case 0: cout<<"No carry operation.\n"; break;
         case 1: cout<<"1 carry operation.\n"; break;
         default: cout<<carry<<" carry operations.\n"; break;
      }
   }      
      
   return 0;
}

/* @END_OF_SOURCE_CODE */

I get WA.. For the following Code 10035

Posted: Wed Aug 15, 2007 3:35 pm
by Sayeef
I get WA for this program...

Code: Select all

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

int main()
{

	int count=0,chk,j,i;
	while(1)
	{
		int res=0;
		count=0;chk=0;
		char s1[20]={0},s2[20]={0};
		cin>>s1;
		cin>>s2;
		if(!(strcmp(s1,"0"))&&!(strcmp(s2,"0")))
				break;
		for(i=0,j=0;(s2[i]||s1[j]);i++,j++)
		{
			res=0;
			if((s2[i]>=48&&s2[i]<58)&&(s1[j]>=48&&s1[j]<58))
				res=s2[i]+s1[j]+chk-96;
			else if(s2[i]>=48&&s2[i]<58)
				res=(s2[i]-48)+chk;
			else if(s1[j]>=48&&s1[j]<58)
				res=(s1[j]-48)+chk;

			chk=(int)(res/10);
			if(chk)
				count++;
		}

		if(count==1)
			cout<<"1 carry operation."<<endl;
		else if(count>1)
			cout<<count<<' '<<"carry operations."<<endl;
		else
			cout<<"No carry operation."<<endl;

	}

return 0;
}

Posted: Wed Aug 15, 2007 4:11 pm
by Jan
Try the cases.

Input:

Code: Select all

119 789
6322168 361892
254312334 8387
277931 56502860
586509 66138934
10 61849637
92 9359840
0 0
Output:

Code: Select all

2 carry operations.
3 carry operations.
3 carry operations.
2 carry operations.
4 carry operations.
No carry operation.
1 carry operation.
Hope these help.

!!!!!!!!!!!!!

Posted: Sun Dec 30, 2007 7:24 pm
by apurba

Code: Select all


remove ur code after acc............

Posted: Mon Mar 10, 2008 10:19 pm
by scott1991
why does my code output the first answer wrongly and then the rest correct as far as i can see. Cheers for any help.

Code: Select all

#include <stdio.h>
#include <math.h>

int main()
{
	long long int a, b, carry=0,temp;
	int length;
	while(scanf("%lld%lld",&a,&b)==2)
	{
		if(a==0&&b==0)
		{
			return 0;
		}
		if(a<b)
		{ 
			temp=a;a=b;b=temp;
		}
		length=int(log10(a*1.0)+2);
		while(length>0)
		{
		if(a%10+b%10>=10)
		{
			if(length==1)
			{
				length++;
			}
			carry++;
			a=int(a/10);
			b=int(b/10);
			b+=1;
		}
		else
		{
			a=int(a/10);
			b=int(b/10);
		}
		length--;
		}
		if (carry==0)
		{
			printf("No carry operation.\n");
		}
		else if(carry==1)
		{
			printf("%lld carry operation.\n",carry);
		}
		else
		{
			printf("%lld carry operations.\n",carry);
		}
		carry=0;
}
	return 0;
}