1 条题解
-
0
C :
#include<stdio.h> #include<string.h> int main() { int x,s=0; while(scanf("%d",&x),x) { for(s=0;x>1;) { if(x%2==0) { x=x/2; s++; }else { x=3*x+1; x=x/2; s++; } } printf("%d\n",s); } }
C++ :
#include <cstdio> int cal(int x) { if (1 == x) return 0; if (x & 1) x = x * 3 + 1; return cal(x / 2) + 1; } int main() { //freopen("data.in", "r", stdin); //freopen("data.out", "w", stdout); int n; while (EOF != scanf("%d", &n)) { if (0 == n) break; printf("%d\n", cal(n)); } return 0; }
Java :
import java.util.Scanner; public class Main{ public static void main(String[] args){ int n, i; Scanner in=new Scanner(System.in); while(in.hasNextInt()){ n = in.nextInt(); if(n == 0) System.exit(0); for(i = 0; n != 1; i++){ if(n % 2 == 0) n /= 2; else n = (3 * n + 1) / 2; } System.out.println(i); } } }
- 1
信息
- ID
- 1868
- 时间
- 1000ms
- 内存
- 32MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者