英雄会第四届在线编程大赛·线上初赛:带通配符的数
个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客:百度搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来搞科研为人民作出自己的贡献;
博客内容:带通配符的数;
知识选自:编程题;
博客时间:2014-3-25;
-
引言
我一天的时间做了这个题目,后来去提交,真心不想说了,我提交的代码一直在编译和测试,我就无语了,是不是测试系统有问题?头疼了。。。
-
题目
带通配符的数:
给定一个带通配符问号的数W,问号可以代表任意一个一位数字。
再给定一个整数X,和W具有同样的长度。
问有多少个整数符合W的形式并且比X大?
输入格式
多组数据,每组数据两行,第一行是W,第二行是X,它们长度相同。在[1..10]之间.
输出格式
每行一个整数表示结果。
-
思路
看到这个题目,我首先用笔在纸上做了半个小时,渐渐发现了思路,后来在纸上写伪代码,写算法。
个人思路用几个例子说明:
我们从左向右依次比较W[i] 和 X[i] 的值
有以下四种情况
好吧,我们模拟算法,看看怎么样吧。
相等,这时候我们比较下一组,ok;
还是相等,接着比较下一组,ok
出现?,这时候把X[2] 加入栈中;接着比较下一组
这时候已经找到不平等元素,发现 W[3] > X[3] 比较结束。
这时候我们就可以知道在W中,5后面的?可以取10中可能(0~9);而栈中的?可以取 (9 - 9 + 1)= 1 中情况;
前后两情况之和想乘即可 为 1*10 = 10 ;
退回上一步,如果是另一中情况怎么办?如下
这时候 W[3] < X[3] , 我们也停止比较。
这时候
这时候我们就可以知道在W中,5后面的?可以取10中可能(0~9);而栈中的?可以取 (9 - 9 )中情况;
前后两情况之和想乘即可 为 0 * 10 = 0 ;
ok,算法演示结束,各种情况都已处理到。
-
实验
-
代码
test.cpp
// include: head file #include<iostream> #include<stack> #include<string> #include<utility> using namespace std; // extra the class of string class String:public string { public: // mode the add of int static 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; static 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; static 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 ) return '-'+c; else return c; } // mode the division a/b static 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 * 10 + a.substr(i,b.size())) // shang = beichushu/ b; // c = c + shang; // yushu = beichushu - b* shang; 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 static 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 static 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; } return result; } // static char division a/b static 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; static 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 static 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 static int 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 static 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 static bool 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 static char 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 number static void Standardization(string &a) { if(!check_all_number(a)) { cout<<a<<" exception of input Standardization"<<endl; } 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; } }; // function: test for string std::pair<bool,int> test_for_string(string W,string X); // function: X and W string W_and_X(string W,string X); // function: main int main() { string a,b; cout<<"please input w and x"<<endl; while(cin>>a>>b) { cout<<"result= "<<W_and_X(a,b)+"\n"; } system("pause"); return 0; } // function: X and W string W_and_X(string W,string X) { std::pair<bool,int> r = test_for_string(W,X); if( r.first ) { stack<char> * S = new stack<char>; char fuhao = '='; string result; for(size_t i = 0;i<W.length();i++) { if(W[i] < X[i]) { fuhao = '<'; break ; } else if(W[i] > X[i] ) { if(W[i] != 63) { fuhao = '>'; break ; } else { S -> push(X[i]) ; } } } size_t length = S->size(); if(fuhao == '<') { string N = String::Int_To_String(r.second - S->size()); string result_houmian = String::Pow_Int("10",N); string result_qianmian =""; char now ; while(! S->empty() ) { now = S->top(); S->pop(); result_qianmian = String::Int_To_String( 9 - String::CharToNumber(now) ) + result_qianmian; } if(!result_qianmian.empty()) result = String::MULT_Int(result_qianmian,result_houmian); else return "0"; String::Standardization(result); return result; } else if(fuhao == '>') { string N = String::Int_To_String(r.second - S->size()); string result_houmian = String::Pow_Int("10",N); string result_qianmian =""; char now ; while(! S->empty() ) { now = S->top(); S->pop(); result_qianmian = String::Int_To_String( 9 - String::CharToNumber(now)+1 ) + result_qianmian; } if(!result_qianmian.empty()) result = String::MULT_Int(result_qianmian,result_houmian); else result = result_houmian; String::Standardization(result); return result; } else if(! S->empty()) { string N = String::Int_To_String(r.second - S->size()); string result =""; char now ; while(! S->empty() ) { now = S->top(); S->pop(); result = String::Int_To_String( 9 - String::CharToNumber(now) ) + result; } String::Standardization(result); return result; } } return string("0"); } // function: test for string std::pair<bool,int> test_for_string(string W,string X) { if(W.length() != X.length()) return std::make_pair(false,0); int n = 0; if(W.length() > 0) { for(size_t i =0;i<W.length();i++) { if( !((W[i] <= 57 && W[i] >= 48) || W[i] == 63 ) ) { return std::make_pair(false,0); } if( W[i] == 63 ) n++; } } if(X.length() > 0) { for(size_t i=0;i<X.length();i++) { if( X[i] > 57 || X[i] < 48 ) { return std::make_pair(false,0); } } } return std::make_pair(true,n); }