HDU 1047 Integer Inquiry: string method for adding large numbers, hduinquiry

Source: Internet
Author: User

HDU 1047 Integer Inquiry: string method for adding large numbers, hduinquiry

This question is the addition of large numbers.

However, it should be noted that the output of HDU's kendad is incorrect several times.

There is another special case, that is, the input case of empty data.

# Include <stdio. h> # include <vector> # include <string. h> # include <algorithm> # include <iostream> # include <string> # include <limits. h> # include <stack> # include <queue> # include <set> # include <map> using namespace std; void plusABtoA (string & a, string & B) {string c; int n = (int). size (), m = (int) B. size (), carry = 0; for (int I = n-1, j = s-1; I> = 0 | j> = 0 | carry; I --, j --) {int an = I> = 0? A [I]-'0': 0; int bn = j> = 0? B [j]-'0': 0; carry = an + bn + carry; c + = char (carry % 10 + '0'); carry/= 10 ;} reverse (c. begin (), c. end (); a = c ;}int main () {int N; string a, B; cin >>> N; while (N --) {cin >>; if (a = "0") // pay attention to special cases {cout <a <endl; if (N) cout <endl; // note that kenay outputs <span style = "white-space: pre"> </span> continue;} while (cin> B & B! = "0") {plusABtoA (a, B) ;}cout <a <endl; if (N) cout <endl; // note the kenay output} return 0 ;}




[Cainiao] ACM question: Integer Inquiry

Your code has a problem in determining the highest bit.
For example
9
90
999
0

Your code output 98
The correct answer should be 1098

Sorry, my code is directly pasted with the AC code below.
Acm.hdu.edu.cn/showproblem.php? Pid = 1, 1047
At the beginning, we entered the number of examples.
Just drop the first three lines.
I don't have a compiler around, but I guess you can't avoid the previous Zero output after you change it.
For example
0009
9
9
You will output the excess 0

This is my code

# Include <stdio. h>
# Include <string. h>
Char s [200];
Int t [200];
Int n, l, I, j, ll;
Bool first = 1;
Int main ()
{
// Scanf ("% d", & n );
// While (n --)
{
// If (first) first = 0; else

Memset (t, 0, sizeof t );
Ll = 1;
While (scanf ("% s", s) = 1)
{
L = strlen (s );
If (l = 1 & s [0] = 48) break;
For (I = L-1, j = 0; I> = 0; I --, j ++)
{
T [j] + = s [I]-48;
If (t [j]> 9)
{
T [j]-= 10;
T [j + 1] ++;
}
}
While (t [j]> 0)
{
If (t [j]> 9)
{
T [j]-= 10;
T [j + 1] ++;
}
J ++;
}
If (j> ll) ll = j;
}
While (j> 1 & t [J-1] = 0) j --;
For (I = ll-1; I> = 0; I --) printf ("% d", t [I]);
Printf ("\ n ");
}
}... Remaining full text>

Why is my sum of simple large numbers on oj incorrect?

This is what I wrote when I was a beginner in high precision, AC
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Int a [1001] = {0}, B [1001] = {0}, c [1001] = {0}, n;
Int I, ka, kb, k, j = 0;
Char a1 [1002], b1 [1002];
Scanf ("% d", & n );
While (n --)
{
J ++;
Scanf ("% s", a1, b1 );
Memset (a, 0,1001 * sizeof (int ));
Memset (B, 0,1001 * sizeof (int ));
Memset (c, 0,1001 * sizeof (int ));
Ka = strlen (a1 );
Kb = strlen (b1 );
If (ka> = kb)
K = ka;
Else
K = kb;
For (I = 0; I <ka; I ++)
A [I] = a1 [ka-i-1]-'0 ';
For (I = 0; I <kb; I ++)
B [I] = b1 [kb-i-1]-'0 ';
For (I = 0; I <k; I ++)
{
C [I] = a [I] + B [I] + c [I];
C [I + 1] = c [I]/10;
C [I] = c [I] % 10;
}
If (c [k])
K ++;
Printf ("Case % d: \ n", j );
For (I = 0; I <ka; I ++)
Printf ("% c", a1 [I]);
Printf ("+ ");
For (I = 0; I <kb; I ++)
Printf ("% c", b1 [I]);
Printf ("= ");
For (k --; k> = 0; k --)
Printf ("% d", c [k]);
Printf ("\ n ");
If (n)
Printf ("\ n ");
}
Return 0;
}
To write high precision, you must be clear-minded and have many errors. I have not changed some of them yet... The changes are as follows:
# Include "stdio. h"
# Include <string. h>
Void main ()
{
Int t, n = 0, alen, blen, clen = 0, c [1002] = {0}, I;
Char a [1, 1002], B [2, 1002];
Scanf ("% d", & t );
While (t --)
{
For (I = 0; I <1002; I ++)
C [I] = 0;
Clen = 0;
Scanf ("% s", & a, & B );
Alen = strlen ();
Blen = strlen (B );
While (alen> 0 | blen> 0)
{
If (alen> 0 & blen> 0)
{C [clen] + = a [alen-1]-'0' + B [blen-1]-'0 ';
If (c [clen]> 9)
{
C [clen]-= 10;
C [clen + 1] ++;
}
Clen ++;
Alen --;
Blen --;
}
Else if (alen> 0)
For (... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.