1000带入不对,求助

P2669 [NOIP2015 普及组] 金币

LMR_Minecraft @ 2024-02-24 17:24:43

问题:

6带入答案是对的,1000不对,输出结果为29370

code:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int k,jd=0,x=1,y=0,gz,ts,ans=0;
    cin>>k;
    int n[10005];
    while(x<=k){
    k=k-x;
    y++;
    x++;
}
    for(int jd=0;jd<=y;jd++){
        for(int ts=1;ts<=jd;ts++){
            gz=jd;
            ans=ans+gz;
        }
    }
    cout<<ans;
    return 0;
}
(个人认为思路没错)

提醒:

jd是发工资的阶段

gz是工资数

ts是发工资的天数


by 0v_0___SYC @ 2024-02-24 17:55:43

@LMR_ 你有没有考虑过如16这样的数据,16=1+2+3+4+5+1,你没有加上多余的天数。


by 0v_0___SYC @ 2024-02-24 17:56:18

@LMR_ ac代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int k,jd=0,x=1,y=0,gz=0,ts=0,ans=0;
    cin>>k;
    int n[10005];
    while(x<=k){
    k=k-x;
    y++;
    x++;
}
    for(int jd=1;jd<=y;jd++){
        for(int ts=1;ts<=jd;ts++){
            ans=ans+jd;
        }
    }
    ans+=(y+1)*k;
    cout<<ans;
    return 0;
}

by LMR_Minecraft @ 2024-02-24 18:12:38

@0v_0___SYC 哦


by HEROBRINEH @ 2024-02-24 18:13:58

#include <bits/stdc++.h>
using namespace std;
int main() {
    int k;
    cin >> k;
    int sum = 0;
    int an = 1;
    int b = 0 ;
    for(int i = 1 ; i <= k ; i++) {
      k -= i;
         b += an * an;
        an ++;
    }
    cout << b + k * an;
}

by Andy_hpy @ 2024-08-09 11:07:46

递归方案

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005];
ll f1(ll n,ll s){
    if(n<s)return n*s;
    if(a[n]!=0)return a[n];
    a[n]=s*s+f1(n-s,s+1);
    return a[n];
}
ll f(ll n){return f1(n,1);}
int main(){
    ll n;
    cin>>n;
    cout<<f(n);
    return 0;
}

|