标签:
卡拉兹(Callatz)猜想:
对任何一个自然数n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把(3n+1)砍掉一半。这样一直反复砍下去,最后一定在某一步得到n=1。卡拉兹在1950年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命题,结果闹得学生们无心学业,一心只证(3n+1),以至于有人说这是一个阴谋,卡拉兹是在蓄意延缓美国数学界教学与科研的进展……
我们今天的题目不是证明卡拉兹猜想,而是对给定的任一不超过1000的正整数n,简单地数一下,需要多少步(砍几下)才能得到n=1?
输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。
输出格式:输出从n计算到1需要的步数。
输入样例:
3
输出样例:
5
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <iostream> 4 #include <string.h> 5 #include <string> 6 #include <math.h> 7 8 9 int main(){ 10 int n,count=0; 11 scanf("%d",&n); 12 while(n!=1) 13 { 14 15 if(n%2==1) 16 { 17 n=(3*n+1)/2; 18 count++; 19 }else 20 { 21 n=n/2; 22 count++; 23 } 24 } 25 printf("%d",count); 26 return 0; 27 }
标签:
原文地址:http://www.cnblogs.com/ligen/p/4296881.html