柠檬微趣2.26笔试题
第一题IP地址拆分转16进制 CPP不太熟,写的很丑
#include <iostream> #include <iomanip> #include <sstream> #include <vector> using namespace std; void print(int num) { int i, j; i = num % 16; j = num / 16; if (j >= 10) { char res; res = j - 10 + 'A'; cout << res; } else cout << j; if (i >= 10) { char res; res = i - 10 + 'A'; cout << res; } else cout << i; } int main() { string ip_l; cin >> ip_l; string tmp; vector<int> res_a; for (auto c : ip_l) { if (c != '.') tmp += c; else { stringstream ss; int res; ss << tmp; ss >> res; if (res > 255 || res < 0) { cout << "X" << endl; return 0; } res_a.push_back(res); tmp.clear(); } } int res; stringstream ss; tmp = ""; for (int i = ip_l.rfind('.') + 1; i < ip_l.length(); ++i) { tmp += ip_l[i]; } ss << tmp; ss >> res; if (res > 255 || res < 0) { cout << "X" << endl; return 0; } res_a.push_back(res); for (auto i : res_a) { print(i); } cout << endl; return 0; }
第二题输出两点之间的最大角度,双指针只过了60
from collections import defaultdict if __name__ == '__main__': hash_table = {} flag = 0 data = list(map(float, input().split()))[1:] j = data.index(max([i for i in data if i - data[0] <= 180])) i = 0 t = j res = data[j] - data[i] while(i != j and t != len(data)-1): i += 1 t += 1 if(data[j] - data[i] > res): res = data[j] - data[i] print(res)第三题 上位中位数 AC
#include <iostream> #include <climits> using namespace std; int main(int args, char *argv[]) { int N; cin >> N; int a[N], b[N]; for(int i = 0; i < N; ++i) cin >> a[i]; for(int i = 0; i < N; ++i) cin >> b[i]; int m(0), n(0); while(m + n != N-1) { if(a[m] < b[n]) ++m; else if(a[m] == b[n]) ++m, ++n; else ++n; } if(b[n] < a[m]) cout << b[n]; else cout << a[m]; return 0; }第四题,筛子点数,动态规划,ac
if __name__ == '__main__': n = int(input()) dp = { 1: { 0: 1, 1: 1, 2: 2, 3: 2 } } for i in range(2, n+1): dic = {} keys = [i + j for i in dp[i-1] for j in dp[1]] dic = dic.fromkeys(keys, 0) for key_1 in dp[i-1]: for key_2 in dp[1]: dic[key_1 + key_2] += dp[i-1][key_1] * dp[1][key_2] dp[i] = dic for key in dp[n]: print("{}:{}".format(key, dp[n][key]))
第二题有人做出来吗,分享下思路