求助大佬,新手小萌新的基础实在差得不行

P2669 [NOIP2015 普及组] 金币

霜雪purity @ 2021-01-28 17:08:43

,怎么在 cout << sum;的后面就结束程序啊,break好像不OK...

#include <iostream>
using namespace std;
int main() {
    int k, sum = 0, N = 0;
    cin >> k;
    for (int i = 1; ; i++) {
        for (int j = 1; j <= i; j++) {
            if (N < k) {
                sum += i;
                N++;
            }
            else {
                cout << sum;
        }
        }
    }
    return 0;
}

by _caiji_ @ 2021-01-28 17:09:42

直接return 0就可以了

#include <iostream>
using namespace std;
int main() {
    int k, sum = 0, N = 0;
    cin >> k;
    for (int i = 1; ; i++) {
        for (int j = 1; j <= i; j++) {
            if (N < k) {
                sum += i;
                N++;
            }
            else {
                cout << sum;
                return 0;
            }
        }
    }
    return 0;
}

by 霜雪purity @ 2021-01-28 17:16:19

@caijianhong 谢谢谢谢,过了


|