混子文章|蓝桥杯一题 -平方差

 题目考点: 平方差 ,平方差奇偶关系

 

代码

#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
#define N 100005
using unl = __int128_t;
using ll = long long;
using namespace  std;
class Solution {
public: 
    void slove() {
        int sum = 0;
        int L, R; cin >> L >> R;
        // 要将 x 分为奇数偶数 奇数x y 相差 1 偶数 xy 相差 2
        for (int i = L; i <= R; i++) {
            if(i % 2 != 0) sum++;
            else if (i % 4 == 0) sum++;
        }
        cout << sum << endl;
    }
};


signed main() {

    cin.tie(0) -> ios::sync_with_stdio(0);
    cout.tie(0) -> ios::sync_with_stdio(0);

    #if Run
        int _;cin>>_;while(_--) Solution().slove();
    #else
        Solution().slove();
    #endif

    return 0;
}

思路解析

如果 x 为奇数 y + z = x , y - z = 1

如果 x 为偶数 y + z = x / 2 y - z = 2

本文作者:2c237c6

本文链接:https://www.cnblogs.com/27dCnc/p/18568633

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   2c237c6  阅读(8)  评论(0编辑  收藏  举报  
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起