AcWing第59场周赛
1.数组操作
简单的前缀和思想,一边录入一边计算前缀和,最后依照题意计算即可
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n;
int a[N];
int main()
{
int sum=0;
cin >> n;
int s[n+1];
s[0]=0;
for (int i = 0; i < n; i ++ )
{
cin>>a[i];
sum+=a[i];
s[i+1]=sum;
}
sort(s,s+n+1);
int x=s[0];
cout << sum-x;
return 0;
}
2.减法操作
纯数学问题,找到最小质因子并用原来的数减
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int n;
cin >> n;
bool flag = 0;
int x;
for(int i = 2;i <= sqrt(n);i ++)
{
if(n % i == 0)
{
x = i;
flag = 1;
break;
}
}
if(flag)
cout << 1+(n-x)/2 << '\n';
else
cout << '1' << '\n';
return 0;
}
力扣第301场周赛
三种水都比较均衡,每次装其中两个,瓶颈就是总数。某种水特别多,瓶颈就是数量最多的那种水(其实就是找规律)
class Solution {
public:
int fillCups(vector<int>& amount) {
int a = max({amount[0],amount[1],amount[2]});
int b = amount[0]+amount[1]+amount[2]-a;
if(a>=b) return a;
else return ((a+b+1)/2);
}
};
SmallestInfiniteSet:初始化需要的参数:无限集中最小整数;移除了的整数集合。
popSmallest:首先将要移除的最小整数加入移除集合,然后更新下一个最小整数。
addBack:首先判断移除集合是否含有需要添加的元素,如果有则去掉对应元素,然后重新确定最小整数。
class SmallestInfiniteSet {
public int minNum;
public List<Integer> list;
public SmallestInfiniteSet() {
minNum = 1;
list = new ArrayList<>();
}
public int popSmallest() {
list.add(minNum);
while (list.contains(minNum)) {
minNum++;
}
return list.get(list.size() - 1);
}
public void addBack(int num) {
if (list.contains(num)) {
list.remove(list.indexOf(num));
}
minNum = Math.min(num, minNum);
}
}
/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* SmallestInfiniteSet obj = new SmallestInfiniteSet();
* int param_1 = obj.popSmallest();
* obj.addBack(num);
*/