【題解】ZeroJudge a059: 完全平方和

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=a059

#include <iostream>
using namespace std;

int main() {
    int N, a, b;
    cin >> N;
    for (int i = 1; i <= N; i++){
        cin >> a >> b;
        int ans = 0, sum = 0, num = 1;
        while (sum < a){
            sum += num;
            num += 2;
        }
        while (sum <= b){
            ans += sum;
            sum += num;
            num += 2;
        }
        cout << "Case " << i << ": " << ans << "\n";
    }
}
分享本文 Share with friends