样例没过 C++ 求助

P2669 [NOIP2015 普及组] 金币

Mark_666 @ 2022-05-18 21:39:59

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    long long sum=0;
    cin>>n;
    int t=0;
    for(int i=1,j=1;i<=n;i+=t,j++)
    {
        sum+=j*i;
        t++;
    }
    cout<<sum<<endl;
    return 0;
}

大佬来看看,哪里错了


by Keep_RAD @ 2022-05-18 21:57:18

这一题这样做比较麻烦,尽量从平方数的想法做。


by Keep_RAD @ 2022-05-18 21:59:32

或者直接从1~n循环

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    long long sum=0;
    cin>>n;
    int t=1;
    int s=0;
    for(int i=1;i<=n;i++)
    {
        s++;
        sum+=t;
        if(s==t) t++,s=0;
    }
    cout<<sum<<endl;
    return 0;
}

by Susara @ 2022-09-27 21:35:02

我这样更简便一些,新手适合


by Susara @ 2022-09-27 21:38:18

int day = 0,cnt = 1,n,sum = 0;
cin>>n;
while(n--){
    sum+=cnt;
    day++;
    if(day == cnt){
        cnt++;
        day = 0;
    }
}
cout<<sum;

by Susara @ 2022-09-27 21:38:44

cnt存储每天金币数量,sum存储金币的总和


by shuyuan2016 @ 2022-12-25 20:49:35

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

|