面试题之二进制输出两数相加
个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客:google搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来搞科研为人民作出自己的贡献;
博客内容:面试题之二进制输出两数相加;
博客时间:2014-4-1
编程语言:C++
编程坏境:Windows
编程工具:vs2008
-
引言
超越永无止境,不要停止,不要害怕,一步一步向前走。
-
题目
微软一面:输入两个数,相加求和,二进制输出。
-
思路
分析:此题有两步
1.实现相加操作
2.把十进制数转化成二进制(解法是以前从书上学的 除以2求余数, 余数的倒序组合便是答案)
-
实验
程序解释 :
第一行输入 加数
第二行输入 加数
第三行输出 加数相加结果
第四行输出 加数相加结果的二进制形式
我的程序是用string处理的,所以处理的数据已经超出了整形表示的范围
-
代码
mine_string.h
#include <iostream> #include <string> #include <limits> using namespace std; // extra the class of string class String:public string { public: // function 1: mode the add of int( (-3) + (-3) ) = - 6 // input: 两个字符串 a 和 b,里面放的都是整数; // output: 返回一个字符串,字符串里面是整数; // 功能: 实现参数两个整数的相加操作,结果存在返回的字符串里 static string ADD_Int(string a,string b); // function 2: make a-b mode int a - b; 7 - (-3) = 10 // input: 两个字符串 a 和 b,里面放的都是整数; // output: 返回一个字符串,字符串里面是整数; // 功能: 实现参数两个整数的相减操作,结果存在返回的字符串里 static string MINUS_Int(string a,string b); // function 3: make a*b mode int a * b; // input: 两个字符串 a 和 b,里面放的都是整数; // output: 返回一个字符串,字符串里面是整数; // 功能: 实现参数两个整数的相乘操作,结果存在返回的字符串里 static string MULT_Int(string a,string b); // function 4: mode the division a/b // input: 两个字符串 a 和 b,里面放的都是整数; // output: 返回一个字符串,字符串里面是整数; // 功能: 实现参数两个整数的相除操作,结果存在返回的字符串里 static string DIV_Int(string a,string b); // function 5: pow number a^b // input: 两个字符串 a 和 b,里面放的都是整数; // output: 返回一个字符串,字符串里面是整数; // 功能: 实现参数两个整数的a^b操作,结果存在返回的字符串里 static string Pow_Int(string a,string b); // function 6: int To string :"123" = 123 // input: 一个int数 a; // output: 返回一个字符串,字符串里面是整数; // 功能: 将整数a转换成对应的字符串格式 static string Int_To_String(int x); // function 7: static char division a/b : 4 / 3 static string Division(string a,string b); // function 8: make a-b mode int a - b; 4 - 3 static string MinusInt(string a,string b); // function 9: mode the add of int :3 + 4 static string AddInt(string a,string b); // function 10: make char to the int number :‘9‘ = 9 static int CharToNumber(char c); // function 11: make int to the model char : 7 = ‘7‘ static string IntToChar(int i); // function 12: check whether the string is legal static bool Check_all_number(string a); // function 13: compare string a and b // input: 两个字符串 a 和 b,里面放的都是整数; // output: 返回一个字符,字符里是a和b的大小关系; // 功能: 实现参数两个整数的a和b比较操作,结果< or = or >存在返回的字符里 static char Compare(string a,string b); // function 14: make string into standard string number static bool Standardization(string &a); // function 15: make string(>0) into standard int number // input: 一个字符串 a,里面放的是一个整数; // output: 返回一个字符串,字符串里是a对应的整形数据; // 功能: 将存在字符串里的整数取出来,放在整形容器里,然后返回,根据返回的结果可以判定是否转换成功 static std::pair<bool,int> String_into_intNumber(string &a); // function 16: get data // input: a int number data with string // output: a string number b // 功能: 实现 sqrt 函数 static string Sqrt_data( string data ,int weishu); // function 17: go larger // input: a int keyword with string , and the int keyword key // output: a double number // 功能: 尝试找到一个区间使得 a^2 <= key <= b^2 static string _make_qujian(string key,int weishu); // function 18: binary search // input: two int numbers s and b, and the int keyword key and weishu // output: a double number with string // 功能: 实现sqrt函数,找到 r^2 == key static string _binary_search(string s,string b,string key,int weishu); // function 19: get perfect double // input: two strings with data and key ,weishu // output: a string with result(data < result < data+1) ,and result^2 <= key // 功能: 实现整数开方精确到小数部分 static string _get_perfect(string data,string key,int weishu); // function 19: get more number // input: two string data and key ,(bigger) // output: a pair with <bool,string>,if(true) perfect string,or bad string // 功能: 进一步精确小数的位数 static std::pair<bool,string> _get_more_number(string data,string key); // function 20: 求余操作 // input: two int number within string // output: a int number within string // 功能: 求出a%b static string Qiuyu(string a,string b); // function 21: Qiu fanshu // input: a int number within string // output: a int number within string // 功能: 实现取反操作 static string Qiu_fanshu(string a); }; // mode the add of int string String::ADD_Int(string a,string b) { // exception of input if( a.empty() ) return b; else if( b.empty() ) return "0"; if(!Check_all_number(a) || !Check_all_number(b)) { return "exception of input ADD_Int"; } Standardization(a); Standardization(b); if(a[0] != ‘-‘ && b[0] != ‘-‘) return AddInt(a,b); else if(a[0] != ‘-‘&& b[0] == ‘-‘) return MinusInt( a,b.substr( 1,b.size() ) ); else if(a[0] == ‘-‘&& b[0] != ‘-‘) return MinusInt(b,a.substr(1,a.size())); else return ‘-‘+AddInt(a.substr(1,a.size()),b.substr( 1,b.size() )); }; // make a-b mode int a - b; string String::MINUS_Int(string a,string b) { // exception of input if( a.empty() ) return b; else if( b.empty() ) return "0"; if(!Check_all_number(a) || !Check_all_number(b)) { return "exception of input Multiplies_Int"; } Standardization(a); Standardization(b); if(a[0] != ‘-‘ && b[0] != ‘-‘) return MinusInt(a,b); else if(a[0] != ‘-‘ && b[0] == ‘-‘) return AddInt(a,b.substr(1,b.size())); else if(a[0] == ‘-‘ && b[0] != ‘-‘) return "-"+AddInt(a.substr(1,a.size()),b); else return MinusInt( b.substr(1,b.size()) , a.substr(1,a.size()) ); }; // make a*b mode int a * b; string String::MULT_Int(string a,string b) { // exception of input if( a.empty() ) return b; else if( b.empty() ) return "0"; if(!Check_all_number(a) || !Check_all_number(b)) { return "exception of input Multiplies_Int"; } Standardization(a); Standardization(b); string::size_type i = a.size(),j = b.size(); string c = "0",d = ""; bool fushu = (a[0] == ‘-‘ && b[0] != ‘-‘)||(a[0] != ‘-‘ && b[0] == ‘-‘); if(a[0] == ‘-‘) a = a.substr(1,a.size()); if(b[0] == ‘-‘) b = b.substr(1,b.size()); int jinwei = 0; for( j = b.size()-1 ; j < b.size() ;j--) { // each number of b to * a jinwei = 0; for( i = a.size()-1 ; i < a.size() ;i-- ) { d = IntToChar( ( CharToNumber(a[i]) * CharToNumber(b[j]) + jinwei ) % 10 )+ d ; jinwei = ( CharToNumber(a[i]) * CharToNumber(b[j]) + jinwei ) / 10 ; } if(jinwei) d = IntToChar(jinwei) +d; // add all number result c = ADD_Int(c,d); d = ""; unsigned int zero = 0 ; while( zero < b.size() - j ) { d = d + ‘0‘; zero ++; } } Standardization(c); if( fushu && c != "0" ) return ‘-‘+c; else return c; }; // mode the division a/b string String::DIV_Int(string a,string b) { // exception of input if( a.empty() ) return "0"; else if( b.empty() ) return "e"; if(!Check_all_number(a) || !Check_all_number(b)) { return "exception of input DIV_Int"; } Standardization(a); Standardization(b); if(b == "0") return "e"; bool fushu = (a[0] == ‘-‘ && b[0] != ‘-‘)||(a[0] != ‘-‘ && b[0] == ‘-‘); if( a[0] == ‘-‘ ) a = a.substr(1,a.size()); if( b[0] == ‘-‘ ) b = b.substr(1,b.size()); if( Compare(a,b) == ‘<‘ ) return "0"; string yushu = ""; string beichushu = a.substr(0,b.size()); string shang = Division( beichushu , b); yushu = MinusInt( beichushu ,MULT_Int( shang, b) ); string c = shang; for(string::size_type i = b.size(); i<a.size(); i++) { beichushu = yushu+ a[i] ; shang = Division( beichushu , b); c = c + shang; yushu = MinusInt( beichushu ,MULT_Int( shang, b) ); } Standardization(c); return fushu?(‘-‘+c):c; }; // function: pow number x,y string String::Pow_Int(string a,string b) { // exception of input if( a.empty() ) return "0"; else if( b.empty() ) return "e"; if(!Check_all_number(a) || !Check_all_number(b)) { return "exception of input DIV_Int"; } Standardization(a); Standardization(b); string result = "1" ; if(Compare(b,"0") != ‘<‘) for(string i ="0" ;Compare(i,b) == ‘<‘ ;i = AddInt(i,"1")) { result = MULT_Int(result,a); } else for(string i ="0" ;Compare(i,b) == ‘>‘ ;i = MINUS_Int(i,"1")) { result = DIV_Int(result,a); } return result ; }; // function : int To string string String::Int_To_String(int x) { bool fushu = false; string result=""; if(x < 0 ) { fushu = true ; x = -x; } else if( x == 0 ) return "0"; while(x) { result = IntToChar(x % 10) + result; x = x/10; } if(fushu) result = "-"+result; return result; }; // static char division a/b string String::Division(string a,string b) { // exception of input if( a.empty() ) return "0"; else if( b.empty() ) return "e"; if(!Check_all_number(a) || !Check_all_number(b)) { cout<<"exception of input Division"<<endl; return "e"; } Standardization(a); Standardization(b); int i = 0; while( i<=9 ) { // if a - b*i < b if( Compare( MINUS_Int( a , MULT_Int(IntToChar(i),b) ) , b ) == ‘<‘ ) break; i++; } if( i>9 ) return "e"; return ""+IntToChar(i); }; // make a-b mode int a - b; string String::MinusInt(string a,string b) { // exception of input if(!Check_all_number(a) || !Check_all_number(b)) return "exception of input MinusInt"; Standardization(a); Standardization(b); // particular string of input if(a.empty()) { if(b.empty()) return "0"; else return "-"+b; } else if(b.empty()) { return a; } // normal number a < b string c = ""; bool check = true ; if(Compare(a,b) == ‘=‘) return "0"; else if(Compare(a,b) == ‘<‘) { c = a ; a = b ; b = c ; c = ""; check = false ; } // normal number a >= b string::size_type i = a.size()-1, j = b.size()-1; int jiewei = 0,now; while(i < a.size() && j < b.size()) { now = CharToNumber(a[i]) - CharToNumber(b[j]) - jiewei ; if( now < 0 ) { jiewei = 1; now = 10 + now ; } else jiewei = 0; c = IntToChar(now) + c ; i--;j--; } while(i < a.size()) { now = CharToNumber(a[i]) - jiewei ; if( now < 0 ) { jiewei = 1; now = 10 + now ; } else jiewei = 0; c = IntToChar( now ) + c ; i--; } Standardization(c); if(!check) c = ‘-‘ + c; return c; }; // mode the add of int string String::AddInt(string a,string b) { // exception of input if( a.empty() ) return b; else if( b.empty() ) return "0"; if(!Check_all_number(a) || !Check_all_number(b)) { return "exception of input AddInt"; } Standardization(a); Standardization(b); string::size_type i = a.size()-1 ,j = b.size()-1 , k = 0 ; string c = ""; int jinwei = 0; while( i < a.size() && j < b.size() ) { c = IntToChar( ( CharToNumber(a[i]) + CharToNumber(b[j]) + jinwei ) % 10 ) + c; jinwei = ( CharToNumber(a[i]) + CharToNumber(b[j]) + jinwei ) / 10; j--;i--; } while( j < b.size() ) { c = IntToChar( ( CharToNumber(b[j]) + jinwei ) % 10 ) + c; jinwei = ( jinwei + CharToNumber(b[j]) ) / 10; j--; } while( i < a.size() ) { c = IntToChar( ( CharToNumber(a[i]) + jinwei ) % 10 ) + c; jinwei = ( jinwei + CharToNumber(a[i]) ) / 10; i--; } if( jinwei ) c = IntToChar( jinwei ) + c; Standardization(c); return c; }; // make char to the int number int String::CharToNumber(char c) { if( c >= ‘0‘ && c <= ‘9‘ ) return int(c - ‘0‘); else { cout<<c<<" exception of input CharToNumber "<<endl; system("pause"); return 0; } }; // make int to the model char string String::IntToChar(int i) { if( i >= 0 && i <= 9 ) { string c = ""; return c+char(i+48); } else { cout<<i<<" exception of input IntToChar"<<endl; system("pause"); return "e"; } }; // check whether the string is legal bool String::Check_all_number(string a) { if(a.empty()) return true ; string::size_type L = a.size(),i = 0; if(a[0] == ‘-‘) i++; while( i < L ) { if( a[i] < ‘0‘ || a[i] > ‘9‘) return false; i++; } return true ; }; // compare string a and b char String::Compare(string a,string b) { if(a.empty() || b.empty()) { cout<<"error of input compare"; return ‘e‘; } else { if(!Check_all_number(a) || !Check_all_number(b)) { return ‘e‘; } Standardization(a); Standardization(b); if(a[0] == ‘-‘ && b[0] != ‘-‘) return ‘<‘; else if( a[0] != ‘-‘ && b[0] == ‘-‘) return ‘>‘; bool fushu = (a[0] == ‘-‘); if(a.size() > b.size() ) return fushu?‘<‘:‘>‘; else if(a.size() == b.size()) { for(string::size_type i = 0;i < a.size(); i++) { if(a[i] > b[i]) return fushu?‘<‘:‘>‘; if(a[i] < b[i]) return fushu?‘>‘:‘<‘; } return ‘=‘; } return fushu?‘>‘:‘<‘; } }; // make string into standard string number bool String::Standardization(string &a) { if(!Check_all_number(a)) { cout<<a<<" exception of input Standardization"<<endl; return false; } string::size_type i = 0; bool fushu = false ; if( a[0] == ‘-‘ ) { fushu = true; i = 1; } while(i < a.size()) { if(a[i] != ‘0‘) break; i++; } if(i == a.size()) i--; a = a.substr(i,a.size()); if( fushu && a != "0") a = ‘-‘ + a; return true ; }; // make string(>0) into standard int number std::pair<bool,int> String::String_into_intNumber(string &a) { if(Standardization(a)) { int max_int = numeric_limits<int>::max()-1 ; string max = Int_To_String(max_int); bool fushu = false; if(a[0] == ‘-‘) { fushu = true ; a = a.substr(1,a.length()); } if(Compare(a,max) != ‘<‘) { cout<<"溢出 exception"<<endl; return std::make_pair(false,0); } int result = 0 ; for(size_t i =0;i<a.length();i++) { result = result * 10 + CharToNumber(a[i]); } if(fushu) result = - result; return std::make_pair(true,result); } else { cout<<"exception of function String_into_intNumber input"<<endl; return std::make_pair(false,0); } }; // function: get data // input: a int number data with string // output: a double number b with string // 功能: 实现 sqrt 函数 string String::Sqrt_data( string data ,int weishu) { if(weishu < 0) weishu = 0; if(! String::Check_all_number(data)) { cout<<"exception of function _sqrt_data input"<<endl; return "NULL"; } String::Standardization(data); char r = String::Compare(data,"0"); if(r == ‘=‘) return "0"; else if(r == ‘<‘) { cout<<"exception of function _sqrt_data input"<<endl; } return _make_qujian(data,weishu); } // function: go larger // input: two int number a and b, and the int keyword key // output: a double number with string // 功能: 尝试找到一个区间使得 a^2 <= key <= b^2 string String::_make_qujian(string key,int weishu) { if( String::Compare(key,"0") == ‘>‘ && weishu >=0 ) { string s,b; int length = key.length()/2; // 偶数 if(key.length() % 2 == 0) { s = String::Pow_Int("10",String::Int_To_String(length-1)); b = String::Pow_Int("10",String::Int_To_String(length)); } // 奇数 else { s = String::Pow_Int("10",String::Int_To_String(length)); b = String::Pow_Int("10",String::Int_To_String(length+1)); } return _binary_search(s,b,key,weishu);// binary search } else { cout<<"exception of function _go_larger input"<<endl; return "NULL"; } } // function: binary search // input: two int numbers s and b, and the int keyword key // output: a double number with string // 功能: 实现sqrt函数,找到 r^2 == key string String::_binary_search(string s,string b,string key,int weishu) { if( String::Compare(s,"1")!= ‘<‘ && String::Compare(b,s) != ‘<‘ && weishu >= 0 ) { string s_2 = String::MULT_Int(s,s); string b_2 = String::MULT_Int(b,b); if(String::Compare(key,s_2) == ‘=‘) return s; if(String::Compare(key,b_2) == ‘=‘) return b; string zhong ,zhong_2,zhong2_2; char r ; while(String::Compare(s,b) != ‘>‘) { zhong = String::DIV_Int(String::ADD_Int(s,b) ,"2"); //zhong = s + (b-s)/2; zhong_2 = String::MULT_Int(zhong,zhong); r = String::Compare(zhong_2,key); if(r == ‘>‘) b = String::MINUS_Int(zhong,"1"); else if( r == ‘=‘ ) return zhong; else if(r == ‘<‘) { zhong2_2 = String::MULT_Int(String::ADD_Int(zhong,"1"),String::ADD_Int(zhong,"1")); r = String::Compare(zhong2_2,key); if(r == ‘>‘) return _get_perfect(zhong,key,weishu); else s = String::ADD_Int(zhong,"1"); } } } else { cout<<"exception of function _binary_search input"<<endl; } } // function: get perfect double // input: two strings with data and key // output: a string with result(data < result < data+1) ,and result^2 <= key // 功能: 实现整数开方精确到小数部分 string String::_get_perfect(string data,string key,int weishu) { int i = weishu; string d = data; string k = key; string result; std::pair<bool,string> r ; while(i > 0) { r = _get_more_number(d,k); if(r.first == true) break; else { d = r.second; k += "00"; } i--; } if(weishu != 0) result = data+"."+d.substr(data.length(),d.length()-data.length()); else result = data; return result; } // function: get more number // input: two string data and key ,(bigger) // output: a pair with <bool,string>,if(true) perfect string,or bad string // 功能: 进一步精确小数的位数 std::pair<bool,string> String::_get_more_number(string data,string key) { char s = ‘1‘,e =‘9‘,zhong,guanxi1,guanxi2 ; key += "00" ; string r1,r2 ; r1 = String::MULT_Int((data+‘9‘),(data+‘9‘)) ; guanxi1 = String::Compare(r1,key) ; if(guanxi1 == ‘<‘) return std::make_pair(false,data+‘9‘); else if(guanxi1 == ‘=‘) return std::make_pair(true,data+‘9‘); r1 = String::MULT_Int((data+‘1‘),(data+‘1‘)); guanxi1 = String::Compare(r1,key); if(guanxi1 == ‘=‘) return std::make_pair(true,data+‘1‘); else if(guanxi1 == ‘>‘) return std::make_pair(false,data+‘0‘); while(s <= e) { zhong = (s+e)/2; r1 = String::MULT_Int((data+zhong),(data+zhong)); guanxi1 = String::Compare(r1,key); if(guanxi1 == ‘=‘) { return std::make_pair(true,data+zhong); } else if(guanxi1 == ‘>‘) e = zhong -1; else if(guanxi1 == ‘<‘) { r2 = String::MULT_Int((data+char(zhong+1)),(data+char(zhong+1))); guanxi2 = String::Compare(r2,key); if(guanxi2 == ‘>‘) return std::make_pair(false,data+zhong); else s = zhong+1; } } } // function: 求余操作 // input: two int number within string // output: a int number within string // 功能: 求出a%b string String::Qiuyu(string a,string b) { if(String::Compare(a,"0") != ‘>‘ || String::Compare(b,"0") != ‘>‘) return "error of function Qiuyu input"; string shang = String::DIV_Int(a,b); string chengji = String::MULT_Int(shang,b); return String::MINUS_Int(a,chengji); } // function: Qiu fanshu // input: a int number within string // output: a int number within string // 功能: 实现取反操作 string String::Qiu_fanshu(string a) { if(String::Check_all_number(a)) { String::Standardization(a); if(a[0] == ‘-‘) return a.substr(1,a.length()-1); else return ("-"+a) ; } else { cout<<"exception of function Qiu_fanshu input"<<endl; return "error"; } }
test.cpp#include "mine_string.h" using namespace std; // function: data to binary system std::pair<bool,string> Data_to_Binary_system(string data); int main() { string a ,b,c; while(cin>>a>>b) { c = String::ADD_Int(a,b); cout<<a<<" + "<<b<<" = "<<c<<endl; std::pair<bool,string> r = Data_to_Binary_system(c); cout<<"符号位 = "<<r.first<<endl; cout<<"数据位 = "<<r.second<<endl; } system("pause"); return 0; } // function: data to binary system std::pair<bool,string> Data_to_Binary_system(string data) { if(String::Check_all_number(data)) { String::Standardization(data); string result = ""; if(String::Compare(data,"0") == ‘=‘) return std::make_pair(false,"0"); bool fushu = false; if(String::Compare(data,"0") == ‘<‘) { data = String::Qiu_fanshu(data); fushu = true; } while(String::Compare(data,"0") == ‘>‘) { result = String::Qiuyu(data,"2") + result ; data = String::DIV_Int(data,"2"); } return std::make_pair(fushu,result); } else { cout<<"exception of function Data_to_Binary_system input"<<endl; return make_pair(true,"error"); } }