c语言问题 角谷猜想,hdu 1279 验证角谷猜想(简单的模拟)

本文介绍了一种编程挑战,要求验证角谷猜想:任何大于1的自然数通过特定的奇偶规则运算最终会得到1。展示了如何编写C++程序来展示这一数论现象,包括输入解析、逻辑处理和结果输出的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

25199f0a12f00e6d60c87511413286ec.gif

d1451eafbd8dec9cffda2045f688c42e.gif

1 #pragma comment(linker, "/STACK:1024000000,1024000000")

2 #include

3 #include

4 #include

5 #include

6 #include

7 #include

8 #include

9 #include

10 #include

11 #include

12 #include

13 #include

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

54 printf("%d ",g[i]);

55 }

56 printf("%d\n",g[g.size()-1]);

57

58 }

59 return 0;

60 }

时间: 02-02

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值