3 条题解

  • 0
    @ 2024-6-5 0:19:58

    字符串查询函数 find()

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main() {
    	int n; cin >> n;
    	string B = "0123456789ABCDEF";
    	for (int i = 0; i < n; i++) {
    		string s; cin >> s;
    		bool b2 = true;
    		bool b8 = true;
    		bool b10 = true;
    		bool b16 = true;
    		for (char c: s) {
    			size_t p = B.find(c); // 查找是否在
    			if (p == string::npos) {
    					b2 = b8 = b10 = b16 = false;
    					break;
    			}
    			if (p > 1) b2 = false;
    			if (p > 7) b8 = false;
    			if (p > 9) b10 = false;
    		}
    		cout << b2 << ' ' << b8 << ' ' << b10 << ' ' << b16 << endl;
    	}
    }
    
    
    • 0
      @ 2024-6-5 0:17:48

      解析:找出每个字符串中最大的字符,根据其大小判断可能是几进制数,详见代码:

      #include <iostream>
      using namespace std;
      int main() {
          int n = 0;
          cin >> n;
          for (int i = 0; i < n; i++) {
              string s;
              cin >> s;
              char maxc = '0';
              for (int i = 0; i<s.length(); i++)
                  if (s[i] > maxc)
                      maxc = s[i];
              if (maxc>'F'){
                  cout<<"0 0 0 0"<<endl;
              }else if(maxc>'9'){
                  cout<<"0 0 0 1"<<endl;
              }else if (maxc>'7'){
                  cout<<"0 0 1 1"<<endl;
              }else if (maxc>'1'){
                  cout<<"0 1 1 1"<<endl;
              }else{
                  cout<<"1 1 1 1"<<endl;
              }
          }
          return 0;
      }
      

      另一种解法:

      #include <iostream>
      using namespace std;
      int main() {
          int n = 0;
          cin >> n;
          for (int i = 0; i < n; i++) {
              char str[11];
              cin >> str;
              char max = '0';
              for (int i = 0; str[i] != '\0'; i++)
                  if (str[i] > max)
                      max = str[i];
              cout << (max <= '1') << " " << (max <= '7') << " " << (max <= '9') << " " <<
                   (max <= 'F') << endl;
          }
          return 0;
      }
      
      • 0
        @ 2024-6-5 0:05:29
        #include <iostream>
        #include <string>
        using namespace std;
        // 二进制
        bool is_bin(string n) {
            for (int i = 0; i < n.length(); i++) {
                if (n[i] < '0' || n[i] > '1') return false;
            }
            return true;
        }
        // 八进制
        bool is_oct(string n) {
            for (int i = 0; i < n.length(); i++) {
                if (n[i] < '0' || n[i] > '7') return false;
            }
            return true;
        }
        // 10 进制
        bool is_dec(string n) {
            for (int i = 0; i < n.length(); i++) {
                if (n[i] < '0' || n[i] > '9') return false;
            }
            return true;
        }
        // 16进制
        bool is_hex(string n) {
            for (int i = 0; i < n.length(); i++) {
                if (n[i] > '9' && n[i] < 'A' || n[i] < '0' || n[i] > 'F') return false;
            }
            return true;
        }
        int main() {
            int k;
            cin >> k;
            for (int i = 0; i < k; i++) {
                string str;
                cin >> str;
                if (is_bin(str)) {
                    cout << 1 << " " << 1 << " " << 1 << " " << 1 << endl;
                } else if (is_oct(str)) {
                    cout << 0 << " " << 1 << " " << 1 << " " << 1 << endl;
                } else if (is_dec(str)) {
                    cout << 0 << " " << 0 << " " << 1 << " " << 1 << endl;
                } else if (is_hex(str)) {
                    cout << 0 << " " << 0 << " " << 0 << " " << 1 << endl;
                } else {
                    cout << 0 << " " << 0 << " " << 0 << " " << 0 << endl;
                }
            }
            return 0;
        }
        
        • 1

        信息

        ID
        1298
        时间
        1000ms
        内存
        256MiB
        难度
        10
        标签
        递交数
        7
        已通过
        2
        上传者