hdu 1279 验证角谷猜想(简单的模拟)

Problem Description
数论中有许多猜想尚未解决,其中有一个被称为“角谷猜想”的问题,该问题在五、六十年代的美国多个著名高校中曾风行一时,这个问题是这样描述的:任何一个大于一的自然数,如果是奇数,则乘以三再加一;如果是偶数,则除以二;得出的结果继续按照前面的规则进行运算,最后必定得到一。现在请你编写一个程序验证他的正确性。
 

 

Input
本题有多个测试数据组,第一行为测试数据组数N,接着是N行的正整数。
 

 

Output
输出验证“角谷猜想”过程中的奇数,最后得到的1不用输出;每个测试题输出一行;每行中只有两个输出之间才能有一个空格;如果没有这样的输出,则输出:No number can be output !。
 

 

Sample Input
4 5 9 16 11
 

 

Sample Output
5 9 7 11 17 13 5 No number can be output ! 11 17 13 5
 

 

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 using namespace std;
15 #define ll long long
16 #define eps 1e-10
17 #define MOD 1000000007
18 #define N 1000000
19 #define inf 1e12
20 int n;
21 vector<int> g;
22 int main()
23 {
24     int t;
25     scanf("%d",&t);
26     while(t--){
27         g.clear();
28         scanf("%d",&n);
29         if(n<=1){
30             printf("No number can be output !\n");
31             continue;
32         }
33         while(n!=1){
34             if((n&1) && n!=1) g.push_back(n);
35             
36             if(n&1){
37                 n=n*3+1;
38             }else{
39                 n/=2;
40             }
41             
42         }
43         
44         
45         if(g.size()==0){
46             printf("No number can be output !\n");
47             continue;
48         }
49         if(g.size()==1){
50             printf("%d\n",g[0]);
51             continue;
52         }
53         for(int i=0;i<g.size()-1;i++){
54             printf("%d ",g[i]);
55         }
56         printf("%d\n",g[g.size()-1]);
57         
58     }
59     return 0;
60 }
View Code

 

posted @ 2016-02-02 02:07  UniqueColor  阅读(314)  评论(0编辑  收藏  举报