设为首页 加入收藏

TOP

Triangular numbers
2014-11-23 17:44:41 来源: 作者: 【 】 浏览:4
Tags: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 triangle uniformly filled with dots. For example, three dots can be arranged in a triangle; thus three is a triangular number. The n-th triangular number is the number of dots in a triangle with n dots on a side. . You can learn more about these numbers from Wikipedia (http://en.wikipedia.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
#include
#include

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;
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇BNU Concentric Rings 下一篇 hdu 1035 Robot Motion(dfs)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·请问c语言刚入门,该 (2025-12-26 10:21:04)
·python 编程怎么定义 (2025-12-26 10:21:01)
·09-指 针 (一)-c语言 (2025-12-26 10:20:58)
·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)