c++求助!!

P2669 [NOIP2015 普及组] 金币

Konnyaku_q @ 2022-03-12 09:18:41

RT,TLE了,求助!!

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

 } 

by Yearn @ 2022-03-12 09:28:57

只要一个循环就够了


by siyu0816 @ 2022-09-13 21:18:34

#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=s+c;  
        q--;    
        if(q==0)    
        {
            c++;    
            q=c;    
        }
    }
    cout<<s;
    return 0;
}

by siyu0816 @ 2022-09-13 21:19:24

不好意思啊


|