关于变量一出循环就从44变1000这回事

P2669 [NOIP2015 普及组] 金币

applesandwich @ 2025-01-11 11:18:49

本人是初学者,大佬勿喷

这代码一言难尽,大佬们自己看吧

样例2没过

#include <bits/stdc++.h>
using namespace std;
long long a,b,k,i,days,sum; 
int main()
{
    cin>>k;
    days=k;
    for(i=0;i<=k;i++)
    {
        if(days>=i)
        {
            sum=sum+i*i;
            days=days-i;
            //cout<<sum<<" "<<days<<" "<<i<<endl;
        }
    }
    if(days>0)
    {
        //cout<<i<<endl;
        sum=sum+(i+1)*days;
        cout<<sum<<endl; 
        //cout<<sum<<" "<<days<<endl;
    }
    else
    cout<<sum<<endl;
    return 0;
}

回复题解更好


by bsdsdb @ 2025-01-11 11:22:25

题解更好


by bsdsdb @ 2025-01-11 11:24:08

@applesandwich 出第一个循环的时候你的 i 是 k+1


by user121_2023wbm @ 2025-01-11 11:27:57

#include <bits/stdc++.h>
using namespace std;
long long a,b,k,i,days,sum; 
int main()
{
    cin>>k;
    days=k;
    for(i=0;i<=k;i++)
    {
        if(days>=i)
        {
            sum=sum+i*i;
            days=days-i;
            //cout<<sum<<" "<<days<<" "<<i<<endl;
        }
        else
            break;
    }
    if(days>0)
    {
        //cout<<i<<endl;
        sum=sum+(i+1)*days;
        cout<<sum<<endl; 
        //cout<<sum<<" "<<days<<endl;
    }
    else
    cout<<sum<<endl;
    return 0;
}

by applesandwich @ 2025-01-11 11:32:58

@user121_2023wbm


by applesandwich @ 2025-01-11 11:33:50

@user121_2023wbm

40分代码?


|