拼多多 100,100,5,30
第一题 奇偶数优先级排序 AC 100
import os import sys def Fn(data:list, N): data.sort(reverse=True) a = [str(x) for x in data if x%2==0] b = [str(x) for x in data if x%2!=0] data = a+b print(",".join(data[0:N])) def main(): line = sys.stdin.readline().strip() line = line.split(';') data = list(map(int, line[0].split(','))) N = int(line[1]) Fn(data, N) if __name__=='__main__': main()
第二题 扑克牌 AC 100
#pragma once #include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; void FnCore(string S1, const string &S2, int N, string newString, string round, vector<string>&res) { if (newString == S2 && round.size() == N) { res.push_back(round); return; } if (S1.size() < 1 || round.size() > N) return; FnCore(S1.substr(1), S2, N, newString, round + 'd', res); FnCore(S1.substr(1), S2, N, S1[0]+newString, round + 'l', res); FnCore(S1.substr(1), S2, N, newString+ S1[0], round + 'r', res); } void Fn(string S1, string S2) { vector<string>res; FnCore(S1, S2, S1.size(), "", "", res); if (res.size() > 0) sort(res.begin(), res.end()); for (int i = 0;i < res.size();++i) { for (int j = 0;j<res[i].size();++j) { printf("%c ", res[i][j]); } cout << endl; } } int main() { int S; cin >> S; string S1, S2; for (int i = 0;i < S;++i) { cin >> S1; cin >> S2; cout << "{" << endl; Fn(S1, S2); cout << "}" << endl; } return 0; }第三题 掷骰子 AC 5
import os import sys def FnCore(X, ret, count, index, CurMax): if index==len(X): count[CurMax-1]+=1 return for i in range(1, X[index]+1): ret.append(i) if i>CurMax: CurMax=i FnCore(X, ret, count, index+1,CurMax) del ret[-1] def Fn(X): maxVal = max(X) count = [0]*maxVal ret = [] CurMax= 0 FnCore(X, ret, count, 0,CurMax) Total = sum(count) P = 0 for i in range(len(count)): P+=(i+1)*count[i]/Total P = round(float(P), 2) print(P) def main(): n = int(input()) X = list(map(int, sys.stdin.readline().strip().split(' '))) Fn(X) if __name__=='__main__': main()第四题 寻找矩阵中第K大的数 AC 30
#pragma once #include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int Fn(int N, int M, int K) { priority_queue<int, vector<int>, less<int>>data; for(int i = N;i>=1;--i) for (int j = M;j >= 1;--j) { data.push(i*j); } for (int r = 0;r < K-1;++r) { data.pop(); } int ret = data.top(); return ret; } int main() { int N, M, K; cin >> N >> M >> K; int ret = Fn(N, M, K); cout << ret; return 0; }