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

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

 

代码

#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

posted @ 2024-02-20 23:44  2c237c6  阅读(18)  评论(0)    收藏  举报  来源