HDU1229 还是A+B 水题

  水题,首先把两个数组的的长度设为一样的,即空位补零,数字在数组中以右对齐方式存储。

  代码如下:

 1 #include <cstdlib>
2 #include <cstring>
3 #include <cstdio>
4 using namespace std;
5
6 int main()
7 {
8 char a[10], b[10], aa[10], bb[10];
9 int bit, num1, num2;
10 while( 1 )
11 {
12 memset( a, '0', sizeof( a ) );
13 memset( b, '0', sizeof( b ) );
14 scanf( "%s %s", a, b );
15 if( a[0] == '0' && b[0] == '0' )
16 break;
17 strcpy( aa, a );
18 strcpy( bb, b );
19 scanf( "%d", &bit );
20 int len1 = strlen( a ), len2 = strlen( b );
21 a[len1] = b[len2] = '0';
22 for( int i = len1 - 1, j = 8; i >= 0; --i, --j )
23 {
24 a[j] = a[i];
25 a[i] = '0';
26 }
27 for( int i = len2 - 1, j = 8; i >= 0; --i, --j )
28 {
29 b[j] = b[i];
30 b[i] = '0';
31 }
32 a[9] = b[9] = '\0';
33 if( strcmp( a + 9 - bit, b + 9 - bit ) )
34 {
35 num1 = atoi( aa );
36 num2 = atoi( bb );
37 printf( "%d\n", num1 + num2 );
38 }
39 else
40 puts( "-1" );
41 }
42 return 0;
43 }

  

原文地址:https://www.cnblogs.com/Lyush/p/2174612.html