zoukankan      html  css  js  c++  java
  • 2016.6.21——Add Binary

    Add Binary

    本题收获:

    对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.
    1.string中默认每个元素为char

    2.从int型转化为char型  s[i] - '0'

     从char型转化为int型  s[i] + '0'

    3.char型和int型相加时按上式会报错   s = (char)(s[i] + '0') +s

      s = (char)(s[i] + '0') +s 这样的写法 每次新的s(由(char)(s[i] + '0'))会放在左边,(加的s为老的s)

      s += (char)(s[i] + '0') 每次新的s会放在右边

      注意高低位来决定“+”的位置

    4.c += i >= 0 ? a[i] - '0' : 0;这个语句的执行是    c += (i >= 0 ? a[i] - '0' : 0); 首先执行判断语句 ? :,在执行表达式c +=>

    5.stray '343' in program leetcode中报错,将报错行的空格全部删除,需要在重新打上(在写代码时可能出现全角符号的值或者是空格,好好检查下,或者重新输入)

     题目:

      Given two binary strings, return their sum (also a binary string).

      For example,
      a = "11"
      b = "1"
      Return "100".
      思路:

        我的思路:每位相加,但是没想出具体怎么做,其实这样行不通

        leetcode:利用中间int型数,每次加两个数的相同位,然后在转化为string型,产生的进位保留加到下次循环。

      代码:思路非常棒

     1 class MyClass
     2 {
     3 public:
     4     string addBinary(string a, string b)            
     5     {
     6         string s;
     7         long c = 0;
     8         int i = a.size() - 1, j = b.size() - 1;
     9 
    10         while (i >= 0 || j >= 0 || c ==1)        //注意循环条件 || c == 1
    11         {
    12             c += i >= 0 ? a[i] - '0' : 0;        //string默认的为char型
    13             i--;
    14             c += j >= 0 ? b[j] - '0' : 0;        //从char型到int型 -'0'
    15             j--;
    16             s = (char)((c % 2 ) + '0') +s ;        //从int型到char型 +'0'
    17             c = c / 2;                //注意什么时候% ,什么时候 /
    18         }
    19 
    20         return s;
    21     }
    22 };

      我的测试代码:

     1 // Add Binary.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include "iostream"
     6 #include "string"
     7 using namespace std;
     8 
     9 class MyClass
    10 {
    11 public:
    12     string addBinary(string a, string b)            
    13     {
    14         string s;
    15         long c = 0;
    16         int i = a.size() - 1, j = b.size() - 1;
    17 
    18         while (i >= 0 || j >= 0 || c ==1)        //注意循环条件
    19         {
    20             c += i >= 0 ? a[i] - '0' : 0;        //string默认的为char型
    21             i--;
    22             c += j >= 0 ? b[j] - '0' : 0;        //从char型到int型 -'0'
    23             j--;
    24             s = (char)((c % 2 ) + '0') +s ;        //从int型到char型 +'0'
    25             c = c / 2;
    26         }
    27 
    28         return s;
    29     }
    30 };
    31 
    32 
    33 
    34 
    35 int _tmain(int argc, _TCHAR* argv[])
    36 {
    37     string a, b, res;
    38     cin >> a;
    39     cin >> b;
    40     MyClass solution;
    41     res = solution.addBinary(a, b);
    42     cout << res << endl;
    43     system("pause");
    44     return 0;
    45 }
  • 相关阅读:
    Saltstack module gem 详解
    Saltstack module freezer 详解
    Saltstack module firewalld 详解
    Saltstack module file 详解
    Saltstack module event 详解
    Saltstack module etcd 详解
    Saltstack module environ 详解
    Saltstack module drbd 详解
    Saltstack module dnsutil 详解
    获取主页_剥离百度
  • 原文地址:https://www.cnblogs.com/zhuzhu2016/p/5603591.html
Copyright © 2011-2022 走看看