Topic links: http://acm.zzuli.edu.cn/problem.php?id=2624
Title Description
H is very small like algorithm, in particular the various additions. Yes addition contains a variety of such binary full adder, semi-additive and the like.
Full adders: two input data bits are added, a result output and a carry bit, adder has a carry input.
Half adder: the sum of the two input data bits, a result output and a carry bit, there is no carry input of the adder. XOR operation is half adder. C, '^' is the exclusive-OR operator
, but commonly are decimal addition, people used in decimal, and the default is every decimal digit. A few days ago, I heard a small H ADD star has a new addition, then went ADD star. He found in ADD star's world, each number can be different every hex, this strange and cumbersome number is called "Add a few." For example, "0687 ......" represents the least significant bit is on the 7-ary number, the second bit is the inverse of octal, 3 bit binary number is 6, No. 4 is 10 decimal number, and the like. Each bit is binary 0 or d (decimal), or an integer in the range [2,9] interval. In theory this should be included into the tabulation infinite number of digits, into the tabulation is not given, it is considered to be decimal.
Full adders: two input data bits are added, a result output and a carry bit, adder has a carry input.
Half adder: the sum of the two input data bits, a result output and a carry bit, there is no carry input of the adder. XOR operation is half adder. C, '^' is the exclusive-OR operator
, but commonly are decimal addition, people used in decimal, and the default is every decimal digit. A few days ago, I heard a small H ADD star has a new addition, then went ADD star. He found in ADD star's world, each number can be different every hex, this strange and cumbersome number is called "Add a few." For example, "0687 ......" represents the least significant bit is on the 7-ary number, the second bit is the inverse of octal, 3 bit binary number is 6, No. 4 is 10 decimal number, and the like. Each bit is binary 0 or d (decimal), or an integer in the range [2,9] interval. In theory this should be included into the tabulation infinite number of digits, into the tabulation is not given, it is considered to be decimal.
Entry
Firstly, a row of N bits into a first tab (0 <N <= 20), ending with a carriage return. Subsequently two rows, each row is given not more than one N-bit non-negative number of Add.
Export
Add the number of output and two in a row.
Sample input
Copy
7170687
03236
445
Sample output
Copy
4104
prompt
For example, corresponds to a tab into "0687", how to calculate "3236 + 445" mean? First we have calculated the lowest level: 6 + 5 = 11; 7 because the least significant bit is a binary, 4 and so we get a carry. Penultimate 2 is: 4 + 3 + 1 (carry) = 8; because this is the octal digit, so we get 0 and a carry. Bit 3: 4 + 2 + 1 (carry) = 7; 6 because this bit is a decimal, so we get 1 and a carry. The first four are: 3 + 1 (carry) = 4; because this bit is a decimal, so we get 7. Finally, we get: 3236 + 445 = 4104.
In fact, the deformation is large integer addition, I am in another blog post and there was a template, you can reference
https://www.cnblogs.com/shuitiangong/p/12063528.html
#include<set> #include<map> #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cctype> #include<string> #include<vector> #include<climits> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define mst(a) memset(a, 0, sizeof(a)) #define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n") using namespace std; typedef long long ll; typedef pair<int, int> P; const double eps = 1e-7; const int INF = 0x3f3f3f3f; const ll ll_INF = 0x3f3f3f3f3f3f3f; const int maxn = 1e3+10; char ls[20]; char num1[maxn], num2[maxn], res[maxn]; void add() { //高精度整数加法(改) mst(res); int len1 = strlen(num1); int len2 = strlen(num2); int len3 = strlen(ls); reverse(num1, num1+len1); reverse(num2, num2+len2); int len, carry; len = carry = 0; for (int i = 0; i<len1 || i<len2; ++i) { carry += i<len1 ? num1[i] - '0' : 0; carry = I + <LEN2 num2 [I] -? ' 0 ' : 0 ; IF (LS [LEN3-I- . 1 !] = ' 0 ' ) { // non ~ 9 0 2 is the binary res [len] = % with Carry (LS [LEN3-I- . 1 ] - ' 0 ' ) + ' 0 ' ; with Carry / = (LS [LEN3-I- . 1 ] - ' 0 ' ); } the else { // 0 10 to press into the prepared RES [len] with Carry =% 10 + ' 0 '; With Carry / = 10 ; } ++ len; } IF (with Carry) // determined last is not a carry- RES [len ++] with Carry + = ' 0 ' ; RES [len] = 0 ; for ( int I = len - . 1 ; I> 0 ; Inc. (www.i-levelmedia.com)) { // exclude the influence of the pilot's attention is not directly 0 i> = 0, the result is exactly equal to 0 if the error occurs IF (RES [I] == ' 0 ' ) RES [ I] = 0 ; the else BREAK ; } reverse(res, res+strlen(res)); } int main(void) { scanf("%s %s %s", ls, num1, num2); add(); printf("%s\n", res); return 0; }