一直10分不知道错哪里啊

P2669 [NOIP2015 普及组] 金币

zwb3_1415926 @ 2021-08-23 22:22:47

#include<bits/stdc++.h> 
using namespace std;
int main()
{
    int k,n,k1,k2,a1;
    cin>>k;
    if (k>=1 && k<=1000)
    {
        for (k1=1;k1<=k;k1++)
        {
            for (k2=1;k2<=k1;k2++)
            {
                n=n+k1;
                a1=a1+1; 
                if (a1>=k)break;
            }
            if (a1>=k)break;
        }
        cout<<n;
    }
    return 0;
} 

by 刺客阿柒 @ 2021-08-23 22:24:59

#include<bits/stdc++.h>
using namespace std;
int a[100001]={0};
int n=0,sum=0,num,k;
int main()
{
    scanf("%d",&num);
    for(int i=1;i<=num;i=k)
    {
        n++;
        for(k=i;k<i+n;k++)
        {
            a[k]=n;
        }
    }
    for(int j=0;j<=num;j++)
    {
        sum+=a[j];
    }
    printf("%d",sum);
    return 0;
}

我也不太会改,只能给你我的AC代码。

注!:不要直接复制提交


by zwb3_1415926 @ 2021-08-23 22:33:30

那也看不懂啊!


by 一洛本谷通 @ 2021-10-06 14:43:31

@zwb3_1415926 思路不错,不过记得把n与a1初始化为0,改过代码为:

#include<bits/stdc++.h> 
using namespace std;
int main()
{
    int k,n=0,k1,k2,a1=0;
    cin>>k;
    if (k>=1 && k<=1000)
    {
        for (k1=1;k1<=k;k1++)
        {
            for (k2=1;k2<=k1;k2++)
            {
                n=n+k1;
                a1=a1+1; 
                if (a1>=k)break;
            }
            if (a1>=k)break;
        }
        cout<<n;
    }
    return 0;
} 

|