TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
C语言趣味程序百例精解之java实现(81)角谷猜想

程序:
- public class Test81{
- public static void main(String args[]){
- int count=0;
- int n=Integer.parseInt(args[0]);
- do{
- if(n%2!=0){
- n=n*3+1;
- System.out.printf("[%d]:%d*3+1=%d
- ",++count,(n-1)/3,n);
- }else{
- n/=2;
- System.out.printf("[%d]:%d/2=%d
- ",++count,2*n,n);
- }
- }while(n!=1);
- }
- }
- C:at>java Test81 10
- [1]:10/2=5
- [2]:5*3+1=16
- [3]:16/2=8
- [4]:8/2=4
- [5]:4/2=2
- [6]:2/2=1
- C:at>java Test81 69
- [1]:69*3+1=208
- [2]:208/2=104
- [3]:104/2=52
- [4]:52/2=26
- [5]:26/2=13
- [6]:13*3+1=40
- [7]:40/2=20
- [8]:20/2=10
- [9]:10/2=5
- [10]:5*3+1=16
- [11]:16/2=8
- [12]:8/2=4
- [13]:4/2=2
- [14]:2/2=1
-
复制代码
源码下载:http://file.javaxxz.com/2014/11/25/000418750.zip |
|