Triangular numbers
Triangular numbers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A triangular number is the number of dots in an equilateral 易做图 uniformly filled with dots. For example, three dots can be arranged in a 易做图; thus three is a triangular number. The n-th triangular number is the number of dots in a 易做图 with n dots on a side. . You can learn more about these numbers from Wikipedia (http://en.易做图.org/wiki/Triangular_number).
Your task is to find out if a given integer is a triangular number.
Input
The first line contains the single number n (1 ≤ n ≤ 500) — the given integer.
Output
If the given integer is a triangular number output YES, otherwise output NO.
Sample test(s)
input
1
output
YES
input
2
output
NO
input
3
output
YES
用一个哈希表存储哪些是triangular number,满足这个数字的要求就是这个数能由公式得到。
AC代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int p[200000]; int main() { int n,i,x; memset(p,0,sizeof(p)); for(i = 1; i <= 500; i++) { x = i*(i+1)/2; p[x] = 1; } while(scanf("%d",&n)!=EOF) { if(p[n] == 1) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }
补充:软件开发 , C++ ,