1 条题解
-
0
C :
#include<stdio.h> void main(){ int n,c=0; scanf("%d",&n); while(n!=1){ if(n%2==0){ n=n/2; }else{ n=n*3+1; } c++; } printf("%d",c); }C++ :
#include<stdio.h> int main() { int n; scanf("%d", &n); int c = 0; while (n != 1) { if (n % 2 == 1) { int sum = n * 3 + 1; //printf("%d*3+1=%d\n", n,sum); c++; n = sum; } else { int div = n / 2; //printf("%d/2=%d\n", n, div); c++; n = div; } } printf("%d\n", c); return 0; }Pascal :
var n,cs:longint; begin read(n); while n<>1 do begin if n mod 2=0 then begin n:=n div 2; inc(cs); end else begin n:=n*3+1; inc(cs); end; end; write(cs); end.Java :
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int s=0; while(n!=1){ if(n%2==0){ n=n/2; s++; }else{ n=n*3+1; s++; } } System.out.println(s); } }Python :
n = int(input()) c = 0 while True: if n % 2 == 0: n = n // 2 else: n = n * 3 + 1 c += 1 if n == 1: break print(c)
- 1
信息
- ID
- 236
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 2
- 已通过
- 2
- 上传者