1.题目链接:6233. 温度转换 - 力扣(LeetCode)
题目思路:保留五位数,移位化为整形,再除回去
class Solution {
public:
vector<double> convertTemperature(double celsius) {
vector<double> res(2);
res[0] = (int)((celsius + 273.15) * 100000) / 100000.0;
res[1] = (int)((celsius * 1.80 + 32.00) * 100000) / 100000.0;
return res;
}
};
2.题目链接:6234. 最小公倍数为 K 的子数组数目 - 力扣(LeetCode)
题目思路:两层for循环i为起点,j进行遍历,找到最小公倍数为k的子数组,res++;当最小公倍数大于k,往后遍历已不存在找到最小公倍数为k的子数组可能性,直接跳出进行下一次循环
class Solution {
public:
int subarrayLCM(vector<int>& nums, int k) {
int res = 0;
for(int i = 0; i < nums.size();i ++ ) {
int cnt = nums[i];
for(int j = i; j < nums.size(); j++ ) {
cnt = cnt * nums[j] / gcd(cnt, nums[j]); // 两个数的最小公倍数等于两数的乘积除以最大公约数
if(cnt == k) res ++;
else if(cnt > k) break;
}
}
return res;
}
};