您的位置:首页 > 其它

Lintcode——952. 数字问题

2018-04-04 21:49 274 查看
给一个转换规则来转换数字n:

n是奇数,n = 3n + 1

n是偶数,n = n / 2

经过若干次转换后,n会变成1。

现在给一个n,输出它转换到1需要的次数。

水水题

代码:

public class Solution {
/**
* @param n: the number n
* @return: the times n convert to 1
*/
public int digitConvert(int n) {
// Write your code here
int count=0;
while(n!=1){
if(n%2==0){
n=n/2;
}
else{
n=3*n+1;
}
count++;
}
return count;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Lintcode 算法 水题