Home > Back-end >  For a great god, about large addition...
For a great god, about large addition...

Time:10-07

This is the code:
#include
#include
Int main (void) {
Int [300]={0} s1 and s2 [300]={0};
Char a1 [300], a2 [300].
Int, length1, length2.
Gets (a1);
Gets (a2);
Length1=strlen (a1);
Length2=strlen (a2);
For (int I=length1-1, j=0; i>=0; I -)
{
S1=a1 [j] [I] - '0';
j++;
}
For (int I=length2-1, j=0; i>=0; I -)
{
S2 [j]=a2 [I] - '0';
j++;
}
Int length=length1 & gt; Length2? Length1: length;
for(int i=0; i{
S1=s1 + s2 [I] [I] [I];
If (s1 [I] <=10)
{
S1=s1 [I] - [I] 10;
S1 [I + 1] + +;
}
}
If (s1/length==0)
{
for(int i=0; i<=length; I++)
Printf (" % d ", s1 [I]);
}
The else
{
for(int i=0; iPrintf (" % d ", s1 [I]);
}
}
Finally the if - else statement length is s1, s2 in the length of the longest digits, according to the analysis, in the most extreme cases, 99999 + 99999, get the length of the number of digits to the length of the longest + 1, so the judge whether the number of the array subscript length of 0? If is zero, which showed that the additive after number as the length, the length of the otherwise length + 1,
Ideas should be no problem, but the result is the

CodePudding user response:

Approach is correct, no detail handle ~

1: int length=length1 & gt; Length2? Length1: length ; Should be changed to length2

2: if (s1 [I] & lt;= 10), greater than or equal to 10 to carry and changed to & gt;=

3: the for (int I=0; i<=length; I++), low on former ah ~ ~ need to start from the high output ~ to such as: the for (int I=length - 1. i>=0; I -)

CodePudding user response:

In addition to the LS said
If (s1/length & gt; 0) length++;//the last one and determine whether effective (although you also judgment, but the if (s1/length==0) in the else didn't modify the length)
Remove the if (s1/length==0) and the else code, finally directly
For (int I=length - 1. i>=0; I -) output is good

In addition, the above said, length may increase if one (s1/length & gt; 0) length++; So array s1 array length 301, best to prevent cross-border

CodePudding user response:

reference 1st floor coo135 response:
approach is correct, details didn't handle ~

1: int length=length1 & gt; Length2? Length1: length ; Should be changed to length2

2: if (s1 [I] & lt;= 10), greater than or equal to 10 to carry and changed to & gt;=

3: the for (int I=0; i<=length; I++), low on former ah ~ ~ need to start from the high output ~ to such as: the for (int I=length - 1. i>=0; I -)

Thank you, run through,

CodePudding user response:

refer to the second floor qybao response:

in addition to the LS saidIf (s1/length & gt; 0) length++;//the last one and determine whether effective (although you also judgment, but the if (s1/length==0) in the else didn't modify the length)
Remove the if (s1/length==0) and the else code, finally directly
For (int I=length - 1. i>=0; I -) output is good

In addition, the above said, length may increase if one (s1/length & gt; 0) length++; So array s1 array length 301, best to prevent cross-border

Subject requirement is 200, I set 300 is ok, but the details of the place is worthy of my study, thanks

CodePudding user response:

Which part of the output if judgment is the opposite of
If (s1/length==0)
{
For (int I=length - 1. i>=0; I -)
Printf (" % d ", s1 [I]);
}
The else
{
For (int I=length; i>=0; I -)
Printf (" % d ", s1 [I]);
}
  • Related