3n+1问题

来源:互联网 发布:mac怎么改 编辑:程序博客网 时间:2024/05/17 18:01

大三下了,马上要出去参加夏令营了,就开始复习算法和数据结构了。

这是第一个简单的题目。

#include <stdio.h>int main(){int n;int count = 0;scanf("%d", &n);while(1 && n != 1){if (n % 2 == 0 && n != 1){n = n / 2;count++; } else{n = 3 * n + 1;count++;}}printf("%d",count);return 0;}


2 0
原创粉丝点击