LeetCode 2096. 统计已测试设备
class Solution {
public:
int countTestedDevices(vector<int>& batteryPercentages) {
int s = 0;
int n = batteryPercentages.size();
for(int i = 0; i < n; i ++) {
if(batteryPercentages[i] > 0) s++;
for(int j = i + 1; j < n && batteryPercentages[i] > 0; j ++) {
batteryPercentages[j] = max(0, batteryPercentages[j] - 1);
}
}
return s;
}
};
LeetCode 2974. 最小数字游戏
class Solution {
public:
vector<int> numberGame(vector<int>& nums) {
vector<int> res;
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i ++) {
int a = nums[i++];
int b = nums[i];
res.push_back(b);
res.push_back(a);
}
return res;
}
};
AcWing 3717. 整数序列
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int N;
int main()
{
cin >> N;
int cnt = 0;
// x + y = 2 * N / len
// y - x + 1 = len
// 2y+1 = 2n/len + len
// -> 2 * N % len == 0 &&
// 数字串的长度
for(int i = sqrt(2*N); i >= 2; i --) {
// x + y 必须是整数
if(2*N % i == 0) {
int first_equal = 2 * N / i;
int second_equal = i - 1;
// y是整数
if(( first_equal + second_equal ) % 2== 0) {
cnt ++;
int y = ( first_equal + second_equal ) / 2;
int x = first_equal - y;
for(int j = x; j <= y; j ++) {
cout << j << " ";
}
cout << endl;
}
}
}
if(cnt == 0 ) cout << "NONE" << endl;
return 0;
}