首先你 $s$ 最大到 $\frac{(n \times(n+1))}{2}$,$a$ 数组肯定越界。
by BlackPanda @ 2023-06-23 20:00:47
@[wanyuanshenma2](/user/767739)
by BlackPanda @ 2023-06-23 20:00:56
@[wanyuanshenma2](/user/767739)
然后就是题目要求输出前 $k$ 天一共的金币数
> 请计算在前 $k$ 天里,骑士一共获得了多少金币。
输出要累加一下。
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[10001];
int main(){
int n,s=1;
cin>>n;
for(int i=1;i<=n;i++){
if (s > n) break;
for(int j=1;j<=i;j++){
a[s]=i;
s++;
if (s > n) break;
}
}
int ans = 0;
for (int i = 1 ; i <= n; i ++ )
ans += a[i];
cout << ans << endl;
return 0;
}
```
by BlackPanda @ 2023-06-23 20:03:37
@[wanyuanshenma2](/user/767739) 数组越界了,要开到1e8
by Steve_xh @ 2023-06-23 20:04:31
@[Queue_Hao](/user/486799) 谢谢
by wanyuanshenma2 @ 2023-06-26 17:04:23
用map不然会开到1e8会MLE
by lhs_chris @ 2023-08-15 13:30:36