周末又做了两个笔试,难度不大,都是差半道题,主要就是会的就是会,不会的就g。主要还是刷熟练度,顺带多练一练其他的方法
1. 小红拆分数组
小红拿到了一个长度为n的数组,她希望把该数组拆分成k个非空子序列。(每个元素必须出现在某个子序列中,且恰好出现一次),使得这个k序列的平均数之和尽可能小。
注:子序列可以不连续。例如数组为[3,2,1,3],k=2时,子序列可以拆分为[3,1]和[2,3]
输入描述:
第一行输入两个正整数和,代表数组的长度、子序列的数量。
第二行输入n个正整数,代表数组的元素ai。
1<=k<=n<=10^5, -10^9<=a<=10^9
输出描述:
一个小数,代表最终平均数之和的最小值。如果你的答案和正确答案的误差不超过10^-6,则认为答案正确。
测试样例:
输入:
4 3
-1 2 -1 3
输出:
0.5000000000
思路:贪心想法。最大的值需要分配最大的除数,最小的值尽量不除
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
sort(nums.begin(), nums.end());
double res = 0;
for (int i = 0; i < k - 1; i++) {
res += nums[i];
}
double temp = 0;
for (int i = k - 1; i < n; i++) {
temp += nums[i];
}
temp /= (n - k + 1);
printf("%.9f", res + temp);
return 0;
}
2. red回文串
给定一个整数x,请你构造一个仅由'r'、'e'、'd'三种字符组成的字符串,其中回文子串的数量恰好为x。字符串的长度不得超过10^5.
输入描述:
一个正整数x。1<=x<=10^9
输出描述:
输出一个仅由'r','e','d'三种字符组成的字符串。若有多解输出任意即可
示例1:
输入
3
输出
red
思路:首先考虑单一字符,其次考虑三种类型的单一字符组成固定长度的字符串。最后转变三数之和的问题。题目不难,需要解析题目转换为子问题确实还需要多练
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
//1 2 3 6 10 15 21 28
int main() {
int x;
cin >> x;
vector<int> nums;
int cnt = 0;
nums.push_back(1);
while (nums[cnt] < x) {
cnt++;
nums.push_back(nums[cnt - 1] + cnt);
}
for (int i = 0; i < cnt; i++) {
int left = i, right = cnt - 1;
int target = x - nums[i];
while (left <= right) {
if (nums[left] + nums[right] > target) {
right--;
}
else if (nums[left] + nums[right] < target) {
left--;
}
else {
string s;
for (int j = 0; j < nums[i]; j++) {
s += 'r';
}
for (int j = 0; j < nums[left]; j++) {
s += 'e';
}
for (int j = 0; j < nums[right]; j++) {
s += 'd';
}
cout << s;
return 0;
}
}
}
return 0;
}
3. 小红的重载
众所周知,在java里可以利用重载的特性,用不同的参数调用同名方法。小红依次尝试创建了n个方法,但其中的一些方法因为违背重载特性导致创建不成功。你可以帮助小红确认每个方法是否创建成功吗?
输入描述:
第一行输入一个正整数n,代表小红创建的方法数量。接下来的n行,每行输入一个字符串,包含方法的名称和参数。请注意,方法的主体(大括号内部的内容)已经省略。1<n<=5000每个字符串长度不超过80.保证每个方法都是合法的,一定包含返回类型,方法名,参数(或者无参)以及参数类型
输出描述:
对于每个方法,如果创建成功则输出“Yes”,否则输出“No”
示例一:
输入
5
int f(int x)
void f()
int f()
int solve(Node node, int x)
int f(int y)
输出
Yes
Yes
No
Yes
No
思路:模拟,但是需要看测试用例,可能会有很多不是规范代码格式,或者是有一些指针引用写在参数类型后面,下方代码只考虑到了最基础情况。
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
using namespace std;
//1 2 3 6 10 15 21 28
int main() {
int n;
cin >> n;
string s;
getline(cin, s);
unordered_map<string, int> hash_map;
for (int i = 0; i < n; i++) {
getline(cin, s);
int cnt = 0;
while (s[cnt] != ' ') {
cnt++;
}
cnt++;
string fun;
while (s[cnt] != '(') {
fun += s[cnt];
cnt++;
}
cnt++;
while (s[cnt] != ')') {
while (s[cnt] != ' ') {
fun += s[cnt];
cnt++;
}
while (s[cnt] != ',' && s[cnt] != ')') {
cnt++;
}
if (s[cnt] == ',') {
while (s[cnt] == ',' || s[cnt] == ' ') {
cnt++;
}
}
}
if (hash_map[fun] == 0) {
hash_map[fun]++;
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
return 0;
}
本文介绍了如何通过贪心策略解决数组拆分问题,构造满足条件的回文字符串,并分析Java方法重载的规则。
36

被折叠的 条评论
为什么被折叠?



