牛客小白月赛72

水平比较cai,所以只有前三题,写写博客来加深自己的理解。这次前面的签到题只要理解很容易写出来,代码都不长。

1、跳跃游戏

只比较第一个和最后一个就行,最后一个比第一个小不管怎么样都跳不到最后一个,这里注意等于也跳不上

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll a[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
	ll n;
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    if(a[1] < a[n])    cout << "YES";
    else cout << "NO";
	return 0;
}

2、数数

函数求因子数量,1到n循环求因子数量是否为奇数

#include<bits/stdc++.h>
using namespace std;
int f(int n) 
{
	int ans = 0;
	for (int i = 1; i * i <= n; i++)
	{
		if (n % i == 0)
		{
			ans++;
			if (i * i != n) ans++;
		}
	}
	return ans;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,sum = 0;
	    cin >> n;
	    for(int i = 1; i <= n; i++)
            if(f(i) % 2 != 0)    sum++;
        cout << sum << '\n';
    }
	return 0;
}

3、操作数组

一个+必对应一个-,所以对应a数组和b数组作差,最后判断作差的和相不相等即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll a[N],b[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
	ll n,s1 = 0,s2 = 0;
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    for(int i = 1; i <= n; i++)   
        cin >> b[i];
    for(int i = 1; i <= n; i++)
    {
        if(b[i] - a[i] > 0)    s1 += (b[i] - a[i]);
        if(b[i] - a[i] < 0)    s2 += (a[i] - b[i]);
    }
    if(s1 != s2)    cout << -1;
    else cout << s1;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值