找规律

/*

A triangular number is the number of dots that can be packed in an equilateral triangle with n dots on a side. Here are a few example triangles, with their corresponding triangular numbers:

\epsfbox{p5958.eps}

You can easily see that a triangular number is the additive equivalent of a factorial:

Tn = $\displaystyle \sum_{{k=1}}^{n}$k

 

Your team is to write a program that will, given an integer value, determine if it is a triangular number.  If the given value is a  triangular number, determine the number of dots on a side.

Example:If k is 10, your program will report that it is a triangular number with 4 dots on a side, since 10 = 4 + 3 + 2 + 1. If kis 11, your program will report that it is not a triangular number.

 

Input

Input to your program will be a series of lines. Each line contains an integer N0 < N < 109with no leading zeroes or spaces and no trailing spaces.

 

Output

Your program is to determine if each integer N is a triangular number.  If it is, print a line containing the number of dots on a side. If it is not a triangular number, print a line containing the string `bad'. In both cases print the answer with no leading zeroes or whitespace and no  trailing whitespace.

*/

#include<iostream>

#include<cmath>                                                                                                     

using namespace std;

int main()

{

         int num;

         int n;

         while(cin>>num)

         {

                   n=(int)pow(2.0*num,0.5);

                   if((n*(1+n))/2==num)  

                            cout<<n<<endl;

                   else

                            cout<<"bad"<<endl;

         }

         return 0;

}

posted @ 2012-11-25 14:07  ♂咱說 ろ算  阅读(242)  评论(0编辑  收藏  举报