【NOIP2015】【Luogu2669】金币(模拟)

problem

  • 骑士在连续的N天里每天收到N枚金币,骑士会在之后的连续 N+1 天里,每天收到 N+1 枚金币。
  • 求骑士在前K天里一共收到了多少金币

solution

  • 直接模拟死循环n天。

codes

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int n;  cin>>n;
    int ans = 0, t = 0;//金币,天数
    for(int i = 1; 1; i++){
        t += i;
        ans += i*i;
        if(t > n){
            t -= i;
            ans -= i*i;
            ans += i*(n-t);
            break;
        }
    }
    cout<<ans<<'\n';
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33957603/article/details/80951739
今日推荐