【Daily Code】LeetCode 2096. 统计已测试设备 && LeetCode 2974. 最小数字游戏 && AcWing 3717. 整数序列

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;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值