@@ -8,23 +8,24 @@ using namespace std;
88**/
99
1010// 字符串除法
11- string divide (string str,int x) {
12- int remainder=0 ;// 保留余数
11+ string Divide (string str,int x) {
12+ int remainder=0 ;
1313 for (int i=0 ; i<str.size (); i++) {
14- int current=str[i]-' 0' +remainder* 10 ;
14+ int current=remainder* 10 + str[i]-' 0' ;
1515 str[i]=current/x+' 0' ;
1616 remainder=current%x;
1717 }
18- int index=0 ;
19- while (str[index]==' 0' )index++;
20- return str.substr (index);
18+ int pos=0 ;
19+ while (str[pos]==' 0' ) {
20+ pos++;
21+ }
22+ return str.substr (pos);
2123}
22-
2324// 字符串乘法
24- string mutiple (string str,int x) {
25- int carry=0 ;// 保存进位
25+ string Multiple (string str, int x) {
26+ int carry=0 ;
2627 for (int i=str.size ()-1 ; i>=0 ; i--) {
27- int current=x* (str[i]-' 0' )+carry;
28+ int current=(str[i]-' 0' )*x +carry;
2829 str[i]=current%10 +' 0' ;
2930 carry=current/10 ;
3031 }
@@ -33,9 +34,8 @@ string mutiple(string str,int x) {
3334 }
3435 return str;
3536}
36-
3737// 字符串加法
38- string add (string str,int x) {
38+ string Add (string str, int x) {
3939 int carry=x;
4040 for (int i=str.size ()-1 ; i>=0 ; i--) {
4141 int current=(str[i]-' 0' )+carry;
@@ -53,20 +53,16 @@ int main() {
5353 while (getline (cin,str)) {
5454 vector<int > binary;
5555 while (str.size ()!=0 ) {
56- // 最低位计算
5756 int last=str[str.size ()-1 ]-' 0' ;
58- // 取模运算
5957 binary.push_back (last%2 );
60- // 整除运算
61- str=divide (str,2 );
58+ str=Divide (str,2 );
6259 }
6360 string answer=" 0" ;
6461 for (int i=0 ; i<binary.size (); i++) {
65- answer=mutiple (answer,2 );// 乘法运算
66- answer=add (answer,binary[i]);// 加法运算
62+ answer=Multiple (answer,2 );
63+ answer=Add (answer,binary[i]);
6764 }
6865 cout<<answer<<endl;
6966 }
70-
7167 return 0 ;
7268}
0 commit comments