力扣第319场周赛+第91场双周赛+Acwing第77场周赛补题

力扣单周赛

一.温度转换

1.原题链接:力扣

2.解题思路:

        根据题意写表达式

3.参考代码:

class Solution {
public:
    vector<double> convertTemperature(double celsius) {
        vector<double>ans(2);
        ans[0] = celsius + 273.15;
        ans[1] = celsius * 1.80 + 32.00;
        return ans;
    }
};

二.最小公倍数为k的子数组数目

1.原题链接:力扣

2.解题思路:

        每次向后一个位置并判断当前子数组的最小公倍数是不是K,若是,则res++, 否则退出当前小循环;求两个数的最小公倍数等于两数之积除以它们的最大公约数;

3.参考代码:

class Solution {
public:
     int gcd(int a,int b){
            if(b == 0)return a;
         return gcd(b, a % b);
        }
    int subarrayLCM(vector<int>& nums, int k) {
        int res = 0,n = nums.size();
        for(int i = 0; i < n; i++){            
             int cur = nums[i];
         for(int j = i; j < n; j++){
             cur = cur * nums[j] / gcd(cur, nums[j]);
             if(cur == k) res++;
             if(cur > k)break;
            }   
        }
        return res;       
    }
};

双周赛

一.不同的平均值数目

1.原题链接:力扣

2.解题思路:

        先将数组由小到大排序,将题意转化为求最小值与最大值的和,然后删除当前的最小值与最大值,产生新的最小值与最大值,不断求和,记录共产生多少个不同的和,即为答案;

3.参考代码:

class Solution {
public:
    int distinctAverages(vector<int>& nums) {
        int n = nums.size();
        vector<int>b(1000, 0);
        int sum = 0, ans = 0;
        sort(nums.begin(), nums.end());
        for(int i = 0; i < n/2; i++){
            sum = nums[i] + nums[n- i - 1];
            b[sum] = 1;
        }
        for(int i = 0; i < b.size(); i++){
            if(b[i] == 1)ans++;
        }
        return ans;
    }
};

二.统计构造好字符串的方案数

1.原题链接 :力扣

2.解题思路:

        爬楼梯问题。相当于每次选择走zero步或one步,走到[low, high]步的方案数是多少。

维护f[i]表示长度恰为i的字符串有几种,递推式为f[i] = f[i - zero] + f[i - one],初值为 f[0] = 1,答案

就是 f[low] + f[low + 1] + ... + f[high]

3.参考代码:

class Solution {
public:
    int countGoodStrings(int low, int high, int zero, int one) {
        const int MOD = 1e9+7;
        int f[100010] = {0};
        f[0] = 1;
        for (int i = 1; i <= high; i++) {
            if (i >= zero) f[i] = (f[i] + f[i - zero]) % MOD;
            if (i >= one) f[i] = (f[i] + f[i - one]) % MOD;
        }
        long long ans = 0;
        for (int i = low; i <= high; i++) ans = (ans + f[i]) % MOD;
        return ans;
    }
};

Acwing

一.进球

1.原题链接:4716. 进球 - AcWing题库

2.解题思路:

        先输入一支球队,若后面输入的球队是该球队,则该球队的赢球次数加一,否则另一支球队的赢球次数加一,最后比较两支球队的赢球次数,次数多的球队即为答案;

3.参考代码:

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int main()
{
    int n, A = 0, B = 0;
    cin >> n;
    string s;
    cin >> s;
    string str1 = s, str2;
    A++;
    for(int i = 1; i < n; i++){
        cin >> s;
        if(s == str1){
          str1 = s, A++;
        }
        else{
        str2 = s, B++;
        }
    }
    if(A > B)cout << str1 << endl;
    else cout << str2 << endl;
    return 0;
}

二.环形队伍

1.原题链接:4717. 环形队伍 - AcWing题库

2.解题思路:

        先算出可以输出几个完整的字符串s,然后分类讨论即可;

3.参考代码:

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n; cin >> n;
    string s = "ROYGBIV", x = "G", y = "GB", z = "GBI", xx = "GBIV", yy = "GBIVG", zz = "GBIVGB";
    int r = n / 7;
    string str;
    for(int i = 0; i < r; i++)str += s;
    if(n % 7 == 0)cout << str << endl;
    else if(n % 7 == 1){str += x; cout << str << endl;}
    else if(n % 7 == 2){str += y; cout << str << endl;}
    else if(n % 7 == 3){str += z; cout << str << endl;}
    else if(n % 7 == 4){str += xx; cout << str << endl;}
    else if(n % 7 == 5){str += yy; cout << str << endl;}
    else{str += zz; cout << str << endl;}
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值