Any help will be appreciated
Code: Select all
Acc thanks to brianfry713 :D
Moderator: Board moderators
Code: Select all
Acc thanks to brianfry713 :D
Code: Select all
AC
}
Code: Select all
1 0
0 0
Code: Select all
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
/*Sample Input:
123 456
555 555
123 594
1234 5678910
9999999999 9999999999
9999999999 1
0 0
Sample Output:
No carry operation.
3 carry operations.
1 carry operation.
2 carry operations.
10 carry operations.
10 carry operations.
*/
int main( )
{
int i, index, S, B, length1, length2;
char temp1[ 15 ], temp2[ 15 ];
bool Judge1, Judge2, Flag1, Flag2;
while( ~scanf( "%s %s", temp1, temp2 ) ) {
index = 0;
Judge1 = true;
Judge2 = true;
Flag1 = false;
Flag2 = false;
length1 = strlen( temp1 );
length2 = strlen( temp2 );
for( i = 0; i < length1; i ++ ) if( ( temp1[ i ] - 48 ) != 0 ) { Flag1 = true; break; }
for( i = 0; i < length2; i ++ ) if( ( temp2[ i ] - 48 ) != 0 || Flag1 ) { Flag2 = true; break; }/*Avoid 000010 000000*/
if( Flag1 == false && Flag2 == false ) break;
S = length1 > length2 ? length2 : length1;/*Smaller*/
B = length1 > length2 ? length1 : length2;/*Bigger*/
for( i = 0; i < B; ) {
if( length1 - i - 1 < 0 ) Judge1 = false;
if( length2 - i - 1 < 0 ) Judge2 = false;
if( Judge1 && Judge2 ) {
if( temp1[ length1 - i - 1 ] + temp2[ length2 - i - 1 ] - 96 > 9 ) {
index ++;
i ++;
if( length1 - i - 1 < 0 ) Judge1 = false;
if( length2 - i - 1 < 0 ) Judge2 = false;
if( i < S - 1 ) temp1[ length1 - i - 1 ] ++;
else Judge1 ? temp1[ length1 - i - 1 ] ++ : temp2[ length2 - i - 1 ] ++;
}
else i ++;
}
else if( Judge1 ) {
if( temp1[ length1 - i - 1 ] - 48 > 9 ) {
index ++;
if( i < B - 1 ) {
i ++;
temp1[ length1 - i - 1 ] ++;
}
else i ++;
}
else break;
}
else {
if( temp2[ length2 - i - 1 ] - 48 > 9 ) {
index ++;
if( i < B - 1 ) {
i ++;
temp2[ length2 - i - 1 ] ++;
}
else i ++;
}
else break;
}
}
if( index == 0 ) printf( "No carry operation.\n" );
else printf( "%d carry operation%s", index, index == 1 ? ".\n" : "s.\n" );
}
return 0;
}
Code: Select all
#include<stdio.h>
#include<string.h>
int main()
{
static long int ar[100],br[100],i,j,sum,k,extra,cry,len1,len2,w,q;
char str1[100],str2[100];
while(scanf("%s%s",&str1,&str2)==2)
{
if((str1[0]=='0')&&(str2[0]=='0'))
break;
i=1;
j=1;
len1=strlen(str1);
len2=strlen(str2);
for(q=len1-1;q>=0;q--)
ar[i++]=str1[q]-48;
for(w=len2-1;w>=0;w--)
br[j++]=str2[w]-48;
i=i-1;
j=j-1;
sum=0;
cry=0;
for(k=1,extra=0;(k<=i&&k<=j);k++)
{
sum=ar[k]+br[k]+extra;
// res[k]=sum%10;
extra=sum/10;
if(extra>0)
cry=cry+1;
}
for(;k<=i;k++)
{
sum=ar[k]+br[k]+extra;
// res[k]=sum%10;
extra=sum/10;
if(extra>0)
cry=cry+1;
}
for(;k<=j;k++)
{
sum=ar[k]+br[k]+extra;
// res[k]=sum%10;
extra=sum/10;
if(extra>0)
cry=cry+1;
}
// if(extra!=0)
// { res[k]=extra;
// k=k+1;
// }
// for(p=k-1;p>=1;p--)
// printf("%lld",res[p]);
if(cry==0)
printf("No carry operation.\n");
else if(cry==1)
printf("%ld carry operation.\n",cry);
else
printf("%ld carry operations.\n",cry);
}
return 0;
}
Code: Select all
9999 9999
9 9999
0 0