笔试题之sqrt函数重写 之续

来源:互联网 发布:mysql 删除表某个字段 编辑:程序博客网 时间:2024/06/03 12:00

笔试题之sqrt函数重写 之续

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来搞科研为人民作出自己的贡献;

博客内容:笔试题之sqrt函数重写;

博客时间:2014-4-1

编程语言:C++

编程坏境:Windows

编程工具:vs2008

 

  • 引言

超越永无止境-----衡水二中

致我的高中母校 ---- 衡水二中

致我的高三语文老师 ---- 赵建业

致我的高一高二化学老师 --- 郭建文

致我的高一高二数学老师 --- 庞艳香

致我的高二班主任即年级主任 --- 高红蕾

致我的高三班主任 ---  高明静

致我的高一班主任 ---  王丽雅

谢谢一路相伴的老师们,你们每一位老师都有不同程度的影响,谢谢你们,陈青松永远记着你们。

  • 题目

求一个正整数N的开方,不能用库函数sqrt,精度在0.001即可。

  • 思路

思路共有两步,第一步的想法与上一篇博客中的想法不同,当然这个更快,第二步就完全相同了。

请看上一篇博客

第一步是找出查找区间,上一篇博客是逐渐扩大,然后找到,时间是现行的,那么可不可以变成常量的,答案是肯定的,

我们得到参数以后可以知道它的长度,根据它的长度,我们就可以在O(1)时间内,找出一个范围。

如果参数的长度是 偶数 m

那么 区间为  ( 10^(m/2 -1) ,10^(m/2) )

否则

区间为 ( 10^(m/2) , 10^(m/2)+1 )

区间确定后,便继续上一篇介绍的第二步完成算法即可。

举个例子 N = 3646132,长度是 7

那么 区间为 (10^3 , 10^4) 因为 10^6 <= N <= 10^8

如果 N = 6465

那么 区间为 (10^1 , 10^2) 因为 10^2 <= N <= 10^4

 

 

实验

解释程序 :

第一行输入的是 N

第二行输入的是 小数的位数(如果输入的小于0 ,将异常抛出,重置为0 )

第三行输出的是 结果

 

代码

mine_string.h

#include <iostream>#include <string>#include <limits>using namespace std;// extra the class of stringclass 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 / 3static string Division(string a,string b);// function 8: make a-b mode int a - b; 4 - 3static string MinusInt(string a,string b);// function 9: mode the add of int :3 + 4static string AddInt(string a,string b);// function 10: make char to the int number :'9' = 9static 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 numberstatic 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);};// mode the add of intstring String::ADD_Int(string a,string b){// exception of inputif( 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 inputif( 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 inputif( 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 resultc = 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/bstring String::DIV_Int(string a,string b){// exception of inputif( 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,ystring String::Pow_Int(string a,string b){// exception of inputif( 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/bstring String::Division(string a,string b){// exception of inputif( 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 < bif(  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 inputif(!Check_all_number(a) || !Check_all_number(b))return "exception of input MinusInt";Standardization(a);Standardization(b);// particular string of inputif(a.empty()){if(b.empty())return "0";elsereturn "-"+b;}else if(b.empty()){return a;}// normal number a < bstring 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 >= bstring::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 intstring String::AddInt(string a,string b){// exception of inputif( 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 numberint 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 charstring 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 bchar 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 numberbool 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 numberstd::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);}};


test.cpp

// include head file#include<iostream>#include"mine_string.h"using namespace std;// function: get data// input: a int number data with string// output: a string number b// 功能: 实现 sqrt 函数string _sqrt_data( string data ,int weishu);// function: go larger// input: a int keyword with string , and the int keyword key// output: a double number // 功能: 尝试找到一个区间使得 a^2 <= key <= b^2string _make_qujian(string key,int weishu);// function: 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 == keystring _binary_search(string s,string b,string key,int weishu);// function: get perfect double// input: two strings with data and key ,weishu // output: a string with result(data < result < data+1) ,and result^2 <= key// 功能: 实现整数开方精确到小数部分string _get_perfect(string data,string key,int weishu);// 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> _get_more_number(string data,string key);// function: mainint main( ){string data;int weishu;cout<<"请输入一个整数,和需要的精度,即小数位数"<<endl;while(cin>>data){cin>>weishu;data = _sqrt_data(data,weishu);cout<<data<<endl;cout<<"请输入一个整数,和需要的精度,即小数位数"<<endl;}system("pause");return 0;}// function: get data// input: a int number data with string// output: a double number b with string// 功能: 实现 sqrt 函数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^2string _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 == keystring _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");}/*if( zhong * zhong > key)b = zhong -1;else if(zhong * zhong == key)return String::Int_To_String(zhong);else if(zhong * zhong < key && (zhong+1)*(zhong+1) > key)//goto floatreturn _get_perfect(String::Int_To_String(zhong),String::Int_To_String(key));else if( zhong * zhong < key)s = 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 _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> _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;}}}


 

 

0 0
原创粉丝点击