B. 三角形数
1 1
2 3 2 3
4 5 6 4 5 6
7 8 9 10
形如此类的为三角形数{1,3,6,10},现在我想知道接下来给出的数字是不是三角形数,聪明的你能否帮我解决一下这个问题?
Input
多组案例
每一行输入一个n 求n是否为三角形数
1<=n<=1e9
Output
若n为三角形数,请输出Yes
否则输出No
Sample Input
6
1
9
Sample Output
Yes
Yes
No
就是求解a[i]-a[i-]=i;
列项相消求解
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll n;
while(cin>>n)
{
int f=0;
double x1=(-1+sqrt(1+8*n))/2;
double x2=(-1-sqrt(1+8*n))/2;
if(x1>=1.0)
{
if(x1==(ll)x1)
f=1;
}
else if(x2>=1.0)
{
if(x2==(ll)x2)
f=1;
}
if(f)
puts("Yes");
else puts("No");
}
return 0;
}