码迷,mamicode.com
首页 > 其他好文 > 详细

51Nod 1087 1 10 100 1000

时间:2017-12-08 23:11:59      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:nod   names   style   判断   math   就是   tin   ret   false   

如果将第二位看成第一位,那么 k * (k+1) / 2都是1,k >= 1,那么其他位都是 0 

所以如果n-1=k*(k+1)/2  (n>1),则该位就是1

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     std::ios::sync_with_stdio(false);
 9     int t, n;
10     cin >> t;
11     while (t--){
12         cin >> n;
13         if (n == 1){ cout << "1" << endl; continue; }
14         int k = sqrt(2 * (n - 1));//判断2*n=k*(k+1)
15         for (int i = k;; i++){
16             if (i*(i + 1) == 2 * (n - 1)){
17                 cout << "1" << endl;
18                 break;
19             }
20             if (i*(i + 1) > 2 * (n - 1)){
21                 cout << "0" << endl;
22                 break;
23             }
24         }
25 
26     }
27     return 0;
28 }

 

51Nod 1087 1 10 100 1000

标签:nod   names   style   判断   math   就是   tin   ret   false   

原文地址:http://www.cnblogs.com/jaydenouyang/p/8006935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!