传智杯大赛:-2027年第八届传智杯程序设计挑战赛初赛第一场C++[B] - = vec[i]) {
第一题: 简单排序
#include
#include
#include
using namespace std;
int main() {
int n, l;
cin >> n >> l;
vectorvec(n, 0);
for (int i = 0; i < n; i++) cin >> vec[i];
sort(vec.begin(), vec.end());
for (int i = 0; i < n; i++) {
if (l >= vec[i]) {
l++;
}
}
cout << l;
}
// 64 位输出请用 printf("%lld")
第二题: 二进制转换10进制
#include
#include
#include
#include
#include
using namespace std;
using ll = long long;
ll getleng(string& a) { //二进制转换十进制
ll value = 0;
int j = 0;
for (int i = a.size() - 1; i >= 0; i--) {
value += (a[i] - '0') * pow(2, j++);
}
return value;
}
int main() {
string str;
getline(cin, str);
int len = 1; //当前遍历长度
int ptr = 0; //当前字符串下标指针
int count = 0; //累加数 可以直接算出的这个值
vectorarr;//记录输出情况
while (1) {
string s1 = "";
if ((str.length() - ptr) < len) break; //如果剩下字符串区间不能满足其长度就退出
for (int i = 1; i <= len; i++) { //
s1 += str[ptr];
ptr++;
}
if (len >= 10) len = 1; //如果len 等于10时 按照题目要求置1
else len++;
count++;
arr.push_back(getleng(s1));
}
cout << count << endl;
for (ll i : arr) cout << i << ' ';
}
第3题: 模拟
因为开始没有加锁,所以可以设置 left 和 right 为 该区间的未加锁数累加和
可以设置 left = x,right = n - y + 1
通过 设置一个数组 记录 command 操作数判断当前操作会导致区间里面的数+1还是-1(解锁还是加锁)
记录的command 执行次数可以知道 当前该位置的锁是否是 锁状态 还是 解锁状态 操作
0 1 2 3 4 5 6 reps一个command操作次数
- + - + - + - ... - 代表 -1 ,+ 代表 +1
可以通过 奇数次 或者 偶数次 command 来判断对 left 或者 right 进行操作,但是要注意题目left和right可能会重合
#include
#include
#include
#include
#include
#include
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int T;
cin >> T;
while (T--) {
int n, m, x, y;
cin >> n >> m >> x >> y;
// left = x right = n - y + 1
int left = x, right = n - y + 1; //设 left为 左区间 right 为 右区间
vectorreps(400000, 0); //创建一个数组记录command操作数
for (int i = 0; i < m; i++) {
int command;
cin >> command;
if (command <= x && command >= y) { //判断command 出现在那个区间
//满足区间
if (reps[command] == 0) //没出现过
{
left--;
right--;
}
else if (reps[command] % 2 == 0) {
//出现偶次
left--;
right--;
}
else {
//出现奇数次
left++;
right++;
}
reps[command]++; //操作数+1
}
else if (command <= x) { 拼多多帮助力入口
//满足区间
if (reps[command] == 0) //没出现过
left--;
else if (reps[command] % 2 == 0) {
//出现偶次
left--;
}
else {
left++;
}
reps[command]++;
}
else if (command >= y) {
if (reps[command] == 0) //没出现过
right--;
else if (reps[command] % 2 == 0) {
//出现偶次
right--;
}
else {
right++;
}
reps[command]++;
}
cout << left << ' ' << right << endl;
}
}
}
第4题: 字符串
(x + p) (x + b) 想获取x的1次方的系数可以知道 x 只要不要乘以x 就能保持 1次方(x 只能乘数),
也就是说 只需要保证 x 乘 的是 数 而不是变量就可以保证是1次方,可以通过
x * b + p * x 来知道其1 次方的值,所以(x+p)(x+b) 的1次方x是 b + p
现在来看更多的多项式 (x + p)(x + b)(x + d)
第一个x 可以通过 依次乘 b 和 d 来获取: x * b * d
第二个p由于需要一个x,我们只需要乘 b 或者 d 相加 就可以了: p * x * d + p * b * x
所以更多更多的多项式 (x + p)(x + b)(x + d)(x + c)
可以由 x * b * d * c + (p * x * d * c + p * b * x * c + p * b * d * x) 得到1次方
代码层面可以通过
vectorarr;
来依次保存 (x+a) 的 a值,如(x+a)(x+b)(x+c) 只需要保存 a , b , c 就行了
#include
#include
#include
#include
#include
#include
using namespace std;
const int mode = 10007;
using ll = long long;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
string str;
getline(cin, str);
vectorarr;
for (int i = 4; i < str.size();i+=5) {
char fuh = str[i - 2];
int value = str[i - 1] - '0'; //字符转数字
if (fuh == '-') value = value * -1; //检测负数
arr.push_back(value);
}
//双数 (x+p)(x+b) 形式
if (str.size() == 10) {
int a = arr[1];
int b = arr[0];
cout << a + b;
return 0;
}
//多数 (x+p)(x+b)(x+d)...形式
// x直接和其他多项式相乘
ll x_value1 = 1; //第一个x获取的值
for (int i = 1; i < arr.size(); i++) {
x_value1 *= arr[i];
}
//这里 p 需要乘以一个 x 在和其他多项式的值相互乘
int kk = arr[0]; //获取第一个 (x + p) 的 p 值
ll y_value = 0; //p 和其他表达式的结果
arr.erase(arr.begin()); //删除x变量
int p = 0; //每一次循环去掉某一个值,因为需要和x相乘保证1次方
for (int i = 0; i < arr.size(); i++) { // 通过给出的思路知道需要乘多少次
ll thisvalue = kk; //让 第一个 x+p 的 p 值和其他值相乘
for (int j = 0; j < arr.size(); j++) { //次排序
if (j == p) continue; //要和一个x相乘,所以该位置跳过
thisvalue *= arr[j]; //依次乘
}
p++;
y_value += thisvalue;
}
cout << x_value1 + y_value; // x乘的数 和 p乘的数相互加起来
return 0;
}
第5题: 01背包 好像是
第6题:好像是双指针或者滑动窗口 不是很清楚,传智杯大赛