1 条题解
-
0
既然分水岭是由(1+2+3+.....+i)个1组成的,那么很容易就可以把分水岭看作一个公差为1的等差数列的和计算。简单做法考虑暴力枚举等差数列有i项,既可以用等差数列求和公式直接计算,也可以简单递推一下,这里就给出使用等差数列求和公式的代码。
#include<bits/stdc++.h> using namespace std; #define LL long long #define pii pair<int,int> #define pu push_back LL mod=1; void babason(){ int n;cin>>n; for(int i=1;i<=1e6;i++){ if((i)*(i+1)/2==n){ cout<<i;return; } } } int main(){ cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); //int t;cin>>t;while(t--) babason(); return 0; }当然cty学长有跟优雅的方法,但是为什么可以这样做呢?(提示:需要数学证明)
#include <bits/stdc++.h> using namespace std; int main(){ int n; scanf("%d", &n); printf("%d", (int)sqrt(2 * n)); return 0; }
- 1
信息
- ID
- 1110
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 68
- 已通过
- 32
- 上传者