C++40分不知道错在哪里,求助大佬!

P2669 [NOIP2015 普及组] 金币

Dear123 @ 2024-01-13 20:29:57

#include <iostream>
using namespace std;
int main(){
    int k,s=0,n=0;
    cin>>k;
    for(int i=1;i<=k;){
        n++;
        s=s+n*n;
        i=i+n;
    }
    cout<<s;
}

by lij123 @ 2024-01-13 20:47:14

改成这样

#include <iostream>
using namespace std;
int main(){
    int k,s=0,n=1;
    cin>>k;
    for(int i=1;i<=k;i++){
        k-=i;
        s=s+n*n;
        n++;
    }
    cout<<s+k*n;
}
}

by lij123 @ 2024-01-13 20:47:25

@Dear123


|