混子文章|蓝桥杯一题 -平方差
题目考点: 平方差 ,平方差奇偶关系
代码
#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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步