前言:这个还是要找规律,不能先转为十进制,相加后再转换为二进制,因为如果输入的二进制足够长,转换为十进制以后会溢出
因为我就是这么做的,提交时直接runtime error!
思路:设定一个变量flag作为进位标志位,找二进制相加的规律,注意字符与数字之间的来回转换。代码有注释
两个循环,第一个循环找两串公共长度;若两串长度不等,不等的部分触发第二个循环
正确代码如下:(================下方为前言提到的转进制计算代码)
1 #include <iostream>
2 #include <string>
3 #include <stack>
4
5 using namespace std;
6
7 class Solution
8 {
9 public:
10 string addBinary(string a, string b)
11 {
12 int a_len = a.length();
13 int b_len = b.length();
14 string result = "";
15 stack<char> anti_result;
16
17 if (a == "0")
18 result = b;
19 else if (b == "0")
20 result = a;
21 else
22 {
23 if (a_len < b_len) //确保a串长度>=b串长度
24 {
25 string c = a;
26 a = b;
27 b = c;
28 }
29 int flag = 0;//进位标志位
30 int i = a.length();
31 int j = b.length();
32 while (i > 0 && j > 0)//第一个循环,用于循环两串公共长度部分
33 {
34 if (a[i - 1] - '0' + b[j - 1] - '0' + flag > 1)//注意字符转数字,并判断 两数之和加flag 是否>1
35 {
36 anti_result.push((a[i - 1] - '0' + b[j - 1] - '0' + flag - 2) + '0');//若是,则有进位。结果中该位置数值=值-2,注意字符转数字,再转字符
37 flag = 1;
38 i -= 1;
39 j -= 1;
40 }
41 else
42 {
43 anti_result.push((a[i - 1] - '0' + b[j - 1] - '0' + flag) + '0');//否则,没有进位。结果中该位置数值=值,注意字符转数字,再转字符
44 flag = 0;
45 i -= 1;
46 j -= 1;
47 }
48 }
49
50 int k = a.length() - b.length(); //第二个循环,若两串长度不等触发
51 while (k > 0)
52 {
53 if (a[k - 1] - '0' + flag > 1)
54 {
55 anti_result.push('0');
56 flag = 1;
57 k -= 1;
58 }
59 else
60 {
61 anti_result.push((a[k - 1] - '0' + flag) + '0');
62 flag = 0;
63 k -= 1;
64 }
65 }
66
67 if (flag == 1) //两循环完毕,若产生进位,则结果还需要拼接1;
68 anti_result.push('1');
69 }
70 while (!anti_result.empty())//将倒序的结果正过来
71 {
72 result += anti_result.top();
73 anti_result.pop();
74 }
75
76 return result;
77 }
78 };
79
80 int main()
81 {
82 string a = "1";
83 string b = "111";
84 string result;
85 Solution sol;
86 result = sol.addBinary(a, b);
87 cout << result << endl;
88
89 int u;
90 cin >> u;
91
92 return 0;
93 }
========================================================================================================================================================
错误代码如下:
1 #include <iostream>
2 #include <string>
3 #include <stack>
4
5 using namespace std;
6
7 //将串儿转换成十进制,相加得到的结果在转换为二进制
8
9 class Solution
10 {
11 public:
12 string addBinary(string a, string b)
13 {
14 int temp;//存储两数之和,十进制表示
15 int a_len = a.length();
16 int b_len = b.length();
17 string result = "";
18 if (a == "0")
19 result = b;
20 else if (b == "0")
21 result = a;
22 else
23 {
24 //二进制转十进制
25 int a_10 = 0;
26 int b_10 = 0;
27 for (int i = 0; i < a_len; i++)
28 a_10 += ((a[i] - '0') * exp2(a_len - 1 - i));
29 for (int j = 0; j < b_len; j++)
30 b_10 += ((b[j] - '0') * exp2(b_len - 1 - j));
31 temp = a_10 + b_10;
32
33 //十进制转二进制
34 stack<int> space;//存放二进制的倒序
35 while (temp>0)
36 {
37 space.push(temp % 2);
38 temp /= 2;
39 }
40
41 //将栈内的数据,倒出来(顺序就正了)
42 while (!space.empty())
43 {
44 result += (space.top() + '0');
45 space.pop();
46 }
47 }
48 return result;
49 }
50 };
51
52 int main()
53 {
54 string a = "0";
55 string b = "0";
56 string result;
57 Solution sol;
58 result = sol.addBinary(a, b);
59 cout << result << endl;
60
61 int u;
62 cin >> u;
63
64 return 0;
65 }