3n+1数列问题

绝地灬酷狼 2022-05-16 06:18 240阅读 0赞

Problem Description
有一天小标遇到了经典的3n+1数链问题,他想知道3n+1数链的前k个数是多少。
下面小标来给你介绍一下3n+1数链是什么,
给定一个数n,如果n为偶数,那么下一个数n1 = n / 2;否则n1 = 3 * n + 1;
如果n1为偶数,那么下一个数n2 = n1 / 2;否则n2 = 3 * n1 + 1;
如果n2为偶数,那么下一个数n3 = n2 / 2;否则n3 = 3 * n2 + 1;
…..
小标最近刚刚学习了链表,他想把这两个知识结合一下,所以,他想按照下面的规定去做。
①起始n为10,k为5,链表为空
②判断n为偶数,他会往链表头部加一个5(即n/2),此时链表中序列为5,n变为5 -> NULL
③接下来n==5,判断n为奇数,他会往链表尾部加一个16(即3*n+1),此时链表中序列为5 -> 16 -> NULL
④接下来n==16,判断n为偶数,他会往链表头部加一个8(即n/2),此时链表中序列为8 -> 5 -> 16 -> NULL
⑤接下来n==8,判断n为偶数,他会往链表头部加一个4(即n/2),此时链表中序列为4 - > 8 - > 5 -> 16 -> NULL
⑥接下来n==4,判断n为偶数,他会往链表头部加一个2(即n/2),此时链表中序列为2 - > 4 - > 8 - > 5 -> 16 -> NULL
到此时,小标得到了前k个数,那么输出这个序列。
Ps: 为了变得更容易理解,简单来说就是n为偶数,加在链表头部,n为奇数,加在链表尾部

Input
多组输入。
对于每组数据,每行两个整数1 <= n , k <= 1000,含义如上

Output
输出链表序列

Sample Input
10 5
Sample Output
2 4 8 5 16

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7. };
  8. struct node *creat (int n,int k)
  9. {
  10. struct node *head ,*p ,*q;
  11. head=(struct node *)malloc(sizeof(struct node));
  12. head->next=NULL;
  13. while(k)
  14. {
  15. if(n%2!=0)
  16. {
  17. p=(struct node *)malloc(sizeof(struct node));
  18. n=n*3+1;
  19. if(head->next)
  20. {
  21. q=head->next;
  22. }
  23. else
  24. q=head;
  25. while(q->next)
  26. {
  27. q=q->next;
  28. }
  29. p->data=n;
  30. p->next=NULL;
  31. q->next=p;
  32. q=p;
  33. }
  34. else
  35. {
  36. p=(struct node *)malloc(sizeof(struct node));
  37. n=n/2;
  38. p->data=n;
  39. p->next =head->next;
  40. head->next=p;
  41. }
  42. k--;
  43. }
  44. return head;
  45. }
  46. void print(struct node *head)
  47. {
  48. struct node *p;
  49. p=head->next;
  50. while(p!=NULL)
  51. {
  52. printf("%d",p->data);
  53. if(p->next!=NULL)
  54. {
  55. printf(" ");
  56. }
  57. else
  58. {
  59. printf("\n");
  60. }
  61. p=p->next;
  62. }
  63. }
  64. int main()
  65. {
  66. int n,k;
  67. struct node *head;
  68. while(~scanf("%d %d",&n,&k))
  69. {
  70. head=creat(n,k);
  71. print(head);
  72. }
  73. return 0;
  74. }

先解释输出函数:为啥外层循环是p!=NULL;里层是p->next !=NULL;外层是让指针一直指向下一个,直到没有。内层最后一项的next 是NULL,也就是不能到达最后一项。
建立函数:先建一个头;如果是奇数,也就是顺序建表,让q指向尾节点,方法是先让他指向头结点或者收个节点,然后一直指向尾部。然后让p插在最后。每次都要找尾节点。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7. };
  8. struct node *creat (int n,int k)
  9. {
  10. struct node *head ,*p ,*tail;
  11. head=(struct node *)malloc(sizeof(struct node));
  12. head->next=NULL;
  13. tail=head;
  14. while(k)
  15. {
  16. if(n%2!=0)
  17. {
  18. p=(struct node *)malloc(sizeof(struct node));
  19. n=n*3+1;
  20. p->data=n;
  21. p->next=NULL;
  22. tail->next=p;
  23. tail=p;
  24. }
  25. else
  26. {
  27. p=(struct node *)malloc(sizeof(struct node));
  28. n=n/2;
  29. p->data=n;
  30. p->next =head->next;
  31. head->next=p;
  32. while(tail->next!=NULL)
  33. {
  34. tail=tail->next;
  35. }
  36. }
  37. k--;
  38. }
  39. return head;
  40. }
  41. void print(struct node *head)
  42. {
  43. struct node *p;
  44. p=head->next;
  45. while(p!=NULL)
  46. {
  47. printf("%d",p->data);
  48. if(p->next!=NULL)
  49. {
  50. printf(" ");
  51. }
  52. else
  53. {
  54. printf("\n");
  55. }
  56. p=p->next;
  57. }
  58. }
  59. int main()
  60. {
  61. int n,k;
  62. struct node *head;
  63. while(~scanf("%d %d",&n,&k))
  64. {
  65. head=creat(n,k);
  66. print(head);
  67. }
  68. return 0;
  69. }

发表评论

表情:
评论列表 (有 0 条评论,240人围观)

还没有评论,来说两句吧...

相关阅读

    相关 1+2+3+...+n

    题目:计算1+2+3+…+n,要求:不使用if, while, for, switch, case等关键字及三目运算符。 看到题目要求不能使用这么多关键字,那么可以考虑用

    相关 3n+1数列问题

    Problem Description 有一天小标遇到了经典的3n+1数链问题,他想知道3n+1数链的前k个数是多少。 下面小标来给你介绍一下3n+1数链是什么,

    相关 (3n+1)猜想

    卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把 (3n+1) 砍掉一半。这样一直反复砍下去,最后一定在某一步得到