C++求助,不明所以

P2669 [NOIP2015 普及组] 金币

Traveller_Blanca @ 2023-09-06 21:25:15

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int k;
    cin>>k;
    int b=1;
    int t=1;
    int bnum=0;
    int tnum=0;
    while(tnum<k)
    {
        if(t==b)
        {
            t=0;
            b++;
        }
        bnum+=b;
        t++;
        tnum++;
    }
    cout <<bnum;
    return 0;
}

by SpeedStar @ 2023-09-06 22:02:39

@Stare

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int k;
    cin>>k;
    int b=1;
    int bnum=0;
    int tnum=0;
    while(tnum<k)
    {
        for (int t = 0; t < b and tnum < k; ++t, ++tnum)  {
            bnum+=b;
        }
        b++;
    }
    cout <<bnum;
    return 0;
}

这样改就行了


|