fork(2) download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string.h>
  5.  
  6. using namespace std;
  7.  
  8. typedef int BigN[10009];
  9.  
  10. BigN a;
  11. BigN b;
  12. BigN result;
  13. bool a_flag, b_flag;
  14.  
  15. void print(BigN &num)
  16. {
  17. for(int i = num[0]; i > 0; --i)
  18. cout << num[i];
  19. cout << endl;
  20. }
  21.  
  22. void read_big(string &str1, string &str2)
  23. {
  24.  
  25. if( str1[0] == '-')
  26. a_flag = true;
  27. if( str2[0] == '-')
  28. b_flag = true;
  29.  
  30. if(!a_flag)
  31. {
  32. a[0] = str1.size();
  33. for(int i = 1; i <= a[0]; ++i)
  34. a[i] = str1[a[0]-i] - '0';
  35. }
  36. else
  37. {
  38. a[0] = str1.size();
  39. for(int i = 1; i < a[0]; ++i)
  40. a[i] = str1[a[0]-i] - '0';
  41. }
  42.  
  43. while( (a[0] > 1) && ( a[a[0]] == 0) )
  44. --a[0];
  45.  
  46.  
  47. if(!b_flag)
  48. {
  49. b[0] = str2.size();
  50. for(int i = 1; i <= b[0]; ++i)
  51. b[i] = str2[b[0]-i] - '0';
  52. }
  53. else
  54. {
  55. b[0] = str2.size();
  56. for(int i = 1; i < b[0]; ++i)
  57. b[i] = str2[b[0]-i] - '0';
  58. }
  59.  
  60. while( (b[0] > 1) && ( b[b[0]] == 0) )
  61. --b[0];
  62.  
  63.  
  64. }
  65.  
  66. int compare(void)
  67. {
  68. int sign = 0;
  69. if ( a[0] < b[0] )
  70. {
  71. sign = -1;
  72. }
  73. else if ( a[0] == b[0] )
  74. {
  75. int len = a[0];
  76. for( int i = len; i > 0; --i)
  77. {
  78. if ( a[i] < b[i] )
  79. {
  80. sign = -1;
  81. return sign;
  82. }
  83. }
  84. }
  85. return sign;
  86. }
  87.  
  88. void add( BigN a, BigN b)
  89. {
  90. int len = a[0] > b[0] ? a[0] : b[0];
  91. int carry = 0;
  92. for( int i = 1; i <= len; ++i )
  93. {
  94. int newdig = a[i] + b[i] + carry;
  95. if( newdig > 9 )
  96. {
  97. result[i] = newdig % 10;
  98. carry = 1;
  99. }
  100. else
  101. {
  102. result[i] = newdig;
  103. carry = 0;
  104. }
  105. ++result[0];
  106. }
  107.  
  108. if(carry)
  109. result[++result[0]] = carry;
  110. }
  111.  
  112.  
  113. void sub(BigN a, BigN b)
  114. {
  115. int len = a[0] > b[0] ? a[0] : b[0];
  116. int j = 1;
  117. for(int i = 1; i <= len; ++i)
  118. {
  119. if( a[i] < b[i] )
  120. {
  121. int k = i;
  122. while(a[++k] == 0)
  123. ;
  124. a[k]--;
  125. k--;
  126. while(k > i)
  127. {
  128. a[k] = 9;
  129. k--;
  130. }
  131. a[i] = a[i] + 10;
  132. result[j] = a[i] - b[i];
  133. }
  134. else
  135. {
  136. result[j] = a[i] - b[i];
  137. }
  138. j++;
  139. result[0]++;
  140. }
  141. while( (result[0] > 1) && ( result[result[0]] == 0))
  142. --result[0];
  143. }
  144.  
  145. void solve()
  146. {
  147. int sign = 0;
  148. if( a_flag && !b_flag)
  149. {
  150.  
  151. sign = -1;
  152. add(a, b);
  153. }
  154. else if( !a_flag && !b_flag)
  155. {
  156.  
  157. sign = compare();
  158. if(sign < 0)
  159. sub(b, a);
  160. else
  161. sub(a, b);
  162. }
  163. else if( !a_flag && b_flag)
  164. {
  165.  
  166. sign = 0;
  167. add(a, b);
  168. }
  169. else if( a_flag && b_flag )
  170. {
  171.  
  172. if( compare() == 0 )
  173. {
  174. sign = -1;
  175. sub(a, b);
  176. }
  177. else
  178. {
  179. sign = 0;
  180. sub(b, a);
  181. }
  182. }
  183.  
  184.  
  185. if(sign < 0)
  186. cout << '-';
  187. print(result);
  188.  
  189. }
  190.  
  191. int main()
  192. {
  193. int test;
  194.  
  195. istringstream ss;
  196. cin >> test;
  197. cin.ignore();
  198. while(test--)
  199. {
  200.  
  201. a_flag = b_flag = false;
  202. string lstr, a_str, b_str;
  203. getline(cin, lstr);
  204.  
  205. ss.str(lstr);
  206.  
  207. ss >> a_str;
  208. ss >> b_str;
  209.  
  210. read_big(a_str, b_str);
  211.  
  212. solve();
  213.  
  214. lstr.clear();
  215. ss.clear();
  216. a_str.clear();
  217. b_str.clear();
  218.  
  219. a_flag = b_flag = false;
  220. memset(&a, 0, sizeof(a));
  221. memset(&b, 0, sizeof(b));
  222. memset(&result, 0, sizeof(result));
  223. }
  224. return 0;
  225. }
  226.  
Success #stdin #stdout 0.01s 2984KB
stdin
29
00002 09
009 002
900 200
0 0
10 3
1000 9
1000 999
4 9
0 8
5 2
1000 20000
999 997
16555 17000
10000 1
1 100000
2000000000000000000000000000000000000000000000000 100000000000000000000000000000000000000000000001
20004 999
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 9999999999999999999999999999999999999999
-1 20
20 -5
2000000000000000000000000000000000000000000000000 100000000000000000000000000000000000000000000001
-500 1000
-1000 500
-500 -1000
-1000 -500
500 -1000
1000 -500
500 1000
1000 500
stdout
-7
7
700
0
7
991
1
-5
-8
3
-19000
2
-445
9999
-99999
1899999999999999999999999999999999999999999999999
19005
99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990000000000000000000000000000000000000001
-21
25
1899999999999999999999999999999999999999999999999
-1500
-1500
500
-500
1500
1500
-500
500