9.29奇安信笔试
题1:5进制和10进制的转换
#include <bits/stdc++.h> using namespace std; unordered_map<char, int> m1 = {{'o', 0}, {'y', 1}, {'e', 2}, {'a', 3}, {'s', 4}}; unordered_map<int, char> m2 = {{0, 'o'}, {1, 'y'}, {2, 'e'}, {3, 'a'}, {4, 's'}}; //5转10 int ftot(string& s){ int i = s.size() - 1; int res = 0; for(auto& t : s){ res += m1[t] * pow(5, i); i--; } return res; } //10转5 string ttof(string& s){ int t = stoi(s); string res; while(t){ res += m2[t % 5]; t /= 5; } reverse(res.begin(), res.end()); return res; } int main(){ int n; cin >> n; while(n--){ string s; cin >> s; if(!(s[0] >= '0' && s[0] <= '9')){ cout << ftot(s) << endl; } else{ cout << ttof(s) << endl; } } return 0; }题2:有多少种方法从左上角走到右下角,dfs,代码忘记存了
题3:给你一个只包含01的字符串,统计有多少个子串满足连续1的个数是最大,不会,只能暴力,当然是过不了了QAQ
#include <bits/stdc++.h> using namespace std; const int MOD = 1e10 + 7; int res = 0; int total = 0; int onesum(const string& s){ int i = 0; int res = 0; int l = 0; for(int i = 0; i != s.size(); i++){ if(s[i] == '1') l++; else l = 0; res = max(res, l); } return res; } int check(const string s, int sum){ int i = 0; int res = 0; int l = 0; for(int i = 0; i != s.size(); i++){ if(s[i] == '1') l++; else l = 0; res = max(res, l); } return res == sum; } void dfs(string& s, int index){ if(index == s.size()){ return; } for(int i = index; i != s.size(); i++){ if(check(s.substr(index, i - index + 1), total)){ res = (res + 1) % MOD; } dfs(s, i + 1); } } int main(){ int n; cin >> n; string s; cin >> s; total = onesum(s); dfs(s, 0); cout << res << endl; return 0; }