SZU:B47 Big Integer I

Judge Info

  • Memory Limit: 32768KB
  • Case Time Limit: 10000MS
  • Time Limit: 10000MS
  • Judger: Normal

Description

Please calculate the answer of A+B and the answer of A-B, both A and B are integer.

Input

The first line of input contains , the number of test cases. There is only line for each test case. It contains two integers .

Output

For each test case, output two lines A+B and A-B.

Sample Input

3

1 2

1 1

-1 -1

Sample Output

3

-1

2

0

-2

0

题解:

1.  调试了很多遍,WA,之后朋友用python 测试很多大数据,我都测试了,数据正常,但还是WA,问了cxc,才发现,擦擦,尼玛,爆粗有木有? 调代码调了四个多小时只是没考虑前导0 ?带前导0也算神马整数  ,  -000001   是  -1   0000002  是 2  , 哪有这么无语的数据,擦擦!!

2.  第二次调试发现前导0去掉了。但是忽略了一个重要的数据,妹的 -00000   减去    +0000000   等于  -0 的有木有?   又单独考虑0的符号问题,最终AC。

 

虽然我代码写的很啰嗦,也很难懂,给自己做个留念吧。 

 

  1 #include <stdio.h>

  2 #include <string.h>

  3  

  4 #define max(a, b) ((a) >= (b) ? (a) : (b)) 

  5 

  6 char A[1012];

  7 char B[1012];

  8 int  C[1012];

  9 int mark;

 10  

 11  

 12 char *revstr(char *str, size_t len)

 13 {

 14  

 15     char    *start = str;

 16     char    *end = str + len - 1;

 17     char    ch;

 18  

 19     if (str != NULL)

 20     {

 21         while (start < end)

 22         {

 23             ch = *start;

 24             *start++ = *end;

 25             *end-- = ch;

 26         }

 27     }

 28     return str;

 29 }

 30  

 31 void add(char A[], char B[]){

 32  

 33     int i, lenA, lenB, len;

 34     memset(C,0,sizeof(C));

 35  

 36     lenA = strlen(A);

 37     lenB = strlen(B);

 38     len = max(lenA, lenB);

 39  

 40     revstr(A,lenA);

 41     revstr(B,lenB);

 42  

 43  

 44     A[lenA] = '0';

 45     B[lenB] = '0';

 46  

 47     for(i=0;i<len+1;i++){

 48  

 49         C[i] = (A[i] - '0') + (B[i] - '0') ;

 50     }

 51  

 52     for(i=0;i<len+1;i++){

 53         if(C[i] > 9 ){

 54             C[i]%=10;

 55             C[i+1]++;

 56         }

 57     }

 58  

 59     if(mark == 1) putchar('-');

 60     if(C[len]==0){

 61         for(i=len-1;i>=0;--i)

 62             printf("%d", C[i]);

 63         printf("\n");

 64     }

 65  

 66     else {

 67         for(i=len;i>=0;--i)

 68             printf("%d", C[i]);

 69         printf("\n");

 70     }

 71     A[lenA] = '\0';

 72     B[lenB] = '\0';

 73     revstr(A,lenA);

 74     revstr(B,lenB);

 75  

 76 }

 77  

 78 void minus(char A[], char B[]){

 79  

 80     int i, lenA, lenB, len;

 81     memset(C,0,sizeof(C));

 82  

 83     lenA = strlen(A);

 84     lenB = strlen(B);

 85     len = max(lenA, lenB);

 86     revstr(A,lenA);

 87     revstr(B,lenB);

 88  

 89  

 90     A[lenA] = '0';

 91     B[lenB] = '0';

 92  

 93     for(i=0;i<len;i++){

 94  

 95         C[i] = (A[i] - '0') - (B[i] - '0') ;

 96     }

 97  

 98     for(i=0;i<len;++i){

 99         if(C[i] <0 ){

100             C[i] += 10;

101             C[i+1]--;

102         }

103     }

104  

105     if(mark == 1) putchar('-');

106  

107     for(;C[len]==0;--len)

108         if(len==0)

109             break;

110     for(i=len;i>=0;--i)

111         printf("%d", C[i]);

112     printf("\n");

113  

114     A[lenA] = '\0';

115     B[lenB] = '\0';

116     revstr(A,lenA);

117     revstr(B,lenB);

118 }

119  

120  

121  

122 int main(int argc, char const *argv[])

123 {

124     int t, i, lenA, lenB, len, Amark, Bmark;

125  

126     scanf("%d", &t);

127     while(t--){

128  

129         mark = 0;

130         Amark = 0;

131         Bmark = 0;

132  

133         memset(A,'0',sizeof(A));

134         memset(B,'0',sizeof(B));

135  

136  

137         scanf("%s", A);

138         scanf("%s", B);

139  

140  

141  

142         lenA = strlen(A);

143         lenB = strlen(B);

144  

145         int len;

146         len = max(lenA, lenB);

147  

148         if(A[0] == '-'){

149              for(i=1;i<lenA;++i)

150                  A[i-1]=A[i];

151              A[i-1]='\0';

152              A[i] = '0';

153              Amark = 1;

154          }

155  

156          if(B[0] == '-'){

157              for(i=1;i<lenB;++i)

158                  B[i-1]=B[i];

159              B[i-1]='\0';

160              B[i] = '0';

161              Bmark = 1;

162          }

163  

164  

165          while(A[0]== '0'){

166              for(i=0;i<lenA;++i)

167                  A[i]=A[i+1];

168              A[lenA]='0';

169          }

170  

171          if(A[0]=='\0'){

172              A[0]='0';

173              A[1]='\0';

174          }

175  

176          while(B[0]== '0'){

177              for(i=0;i<lenB;++i)

178                  B[i]=B[i+1];

179              B[lenB]='0';

180  

181          }

182  

183          if(B[0]=='\0'){

184              B[0]='0';

185              B[1]='\0';

186          }

187 

188  

189          if(A[0] == '0' && B[0] == '0'){

190              printf("0\n");

191              printf("0\n");

192              continue;

193          }

194  

195         lenA = strlen(A);

196         lenB = strlen(B);

197         len = max(lenA, lenB);

198  

199         lenA = strlen(A);

200         lenB = strlen(B);

201  

202         if(Amark == 0 && Bmark == 0 ){

203             mark = 0;

204             add(A,B);

205  

206             if(lenA>lenB){

207                 mark = 0;

208                 minus(A,B);

209             }

210             else if(lenA < lenB){

211                 mark = 1;

212                 minus(B,A);

213             }

214             else {

215                 if(strcmp(A,B)>0){

216                     mark = 0;

217                     minus(A,B);

218                 }

219                 else if(strcmp(A,B)<0){

220                     mark = 1;

221                     minus(B,A);

222                 }

223                 else {mark =0 ;minus(A,B);}

224             }

225             continue;

226         }

227  

228         if(Amark == 1 && Bmark ==1){

229  

230             mark = 1;

231             add(A,B);

232  

233             if(lenA>lenB){

234                 mark = 1;

235                 minus(A,B);

236             }

237             else if(lenA<lenB){

238                 mark = 0;

239                 minus(B,A);

240             }

241             else {

242                 if(strcmp(A,B)>0){

243                     mark = 1;

244                     minus(A,B);

245                 }

246                 else if(strcmp(A,B)<0){

247                     mark = 0;

248                     minus(B,A);

249                 }

250                 else{

251                     minus(A,B);

252                 }

253             }

254             continue;

255         }

256  

257         if(Amark == 1 && Bmark == 0){

258             if(lenA>lenB){

259                 mark = 1;

260                 minus(A,B);

261             }

262             else if(lenA<lenB){

263                 mark = 0;

264                 minus(B,A);

265             }

266             else {

267                 if(strcmp(A,B)>0){

268                     mark = 1;

269                     minus(A,B);

270                 }

271                 else if(strcmp(A,B)<0){

272                     mark = 0;

273                     minus(B,A);

274                 }

275                 else{

276                     mark = 0;

277                     minus(A,B);

278                 }

279             }

280  

281             mark = 1;

282             add(A,B);

283             continue;

284         }

285  

286         if(Amark == 0 && Bmark == 1){

287             if(lenA>lenB){

288                 mark = 0;

289                 minus(A,B);

290             }

291             else if(lenA<lenB){

292                 mark = 1;

293                 minus(B,A);

294             }

295             else{

296                 if(strcmp(A,B)>0){

297                     mark = 0;

298                     minus(A,B);

299                 }

300                 else if(strcmp(A,B)<0){

301                     mark = 1;

302                     minus(B,A);

303                 }

304                 else {

305                     mark = 0;

306                     minus(A,B);

307                 }

308             }

309             mark = 0;

310             add(A,B);

311             continue;

312         }

313     }

314  

315     return 0;

316 }

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Integer)