40分求解!!

P2669 [NOIP2015 普及组] 金币

domerawind @ 2018-12-25 16:12:52

#include <iostream>
#include <cstdio> 
#include <cmath>
using namespace std;
int main()
{
    long long n=0;
    cin>>n;
    long long total=0,i=n;
    int b=0;
    for(int a=0;;a++)
    {
        i=i-a;
        b=a;
        total=total+a*a;
        if(i<=0) break;
    }
    if(i<0)
    {
    int d=(b*(b-1))/2;
    int e=0;
    e=n-d;
    total=total+(b+1)*e;
    }
    cout<<total;
}

by dagggg @ 2018-12-25 16:38:33

#include <iostream>
#include <cstdio> 
#include <cmath>
using namespace std;
int main()
{
    long long n;
    cin>>n;
    long long total=0,i=n;
    int b;
    for(int a=1;;a++)
    {
        i=i-a;
        b=a;
        if(i<0) break; 
        total=total+a*a;
//      printf("%d\n", total);
    }
    if(i<0)
    {
    int d=(b*(b-1))/2;
    int e;
    e=n-d;
    total=total+(b)*e;
    }
    cout<<total;
}

by dagggg @ 2018-12-25 16:40:38

@domerawind


by ⚡进击_蒟蒻⚡ @ 2018-12-25 16:59:10

@domerawind

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

by runtime_error @ 2018-12-25 17:58:06

#include <iostream>
#include <cstdio>
using namespace std;
long long ans,k;
int main()
{
    scanf("%lld",&k);
    int i=1;
    while(k-i>0){
        ans+=i*i;
        k-=i;
        i++;
    }
    ans+=k*i;
    printf("%lld",ans);
    return 0;
}

|