首页 > 试题广场 >

xxx定律

[编程题]xxx定律
  • 热度指数:17792 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。     请计算需要经过几步才能将n变到1,具体可见样例。

输入描述:
    测试包含多个用例,每个用例包含一个整数n。(1<=n<=10000)


输出描述:
    对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行。
示例1

输入

3
1

输出

5
0

python解法:





while True:
    try:
        a,res=int(input()),0
        if a!=0:
            while a!=1:
                res+=1
                if a%2==0:
                    a=a//2
                else:
                    a=(3*a+1)//2
            print(res)

    except:
        break
发表于 2017-10-06 21:33:40 回复(0)
//比较简单,就没什么好注意的了
#include<iostream>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        if(n==0)
            break;
        int times=0;
        while(n!=1){
            if(n%2==0)
                n/=2;
            else
                n=(3*n+1)/2;
            times++;
        }
        cout<<times<<endl;
    }
}

发表于 2020-01-11 17:33:45 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int count =0;
            if(n == 0){
                break;
            }
            while(n !=1){
                if(n%2 == 0){
                    n/=2;
                }else{
                    n = 3*n +1;
                    n/=2;
                }
                count++;
            }
            System.out.println(count);
        }
    }
}

发表于 2018-10-23 19:45:21 回复(0)
#include<stdio.h>
int main()
{
    int n,num=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        while(n!=1)
        {
            if(n%2==0)//偶数
                n=n/2;
            else
                n=(3*n+1)/2;
            num++;
        }
        printf("%d",num);
    }
}

发表于 2020-04-16 14:24:01 回复(0)
Java 解法
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            // n=0 表示结束
            int count=0;
            if (n!=0){
                while (n!=1){
                    if (n%2==0) {
                        n = n / 2;
                        count++;
                    } else{
                        n=n*3+1;
                        n=n/2;
                        count++;
                    }
                }
                System.out.println(count);
            }
        }
    }
}


发表于 2020-03-06 14:18:27 回复(0)
#include <iostream>
using namespace std;
int main()
{
	int n = 0, cnt = 0;;
	while (cin>>n && 0 != n)
	{
		cnt = 0;
		while (1 != n)
		{
			if (0 == n % 2) n /= 2;
			else n = (3 * n + 1) / 2;
			++cnt;
		}
		cout << cnt << endl;
	}
	return 0;
}

发表于 2020-02-19 21:40:45 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main() {
    int input;
    while (cin >> input) {
        if (input == 0)
            break;
        int step = 0;
        while (input != 1) {
            if (input % 2 == 0)
                input /= 2;
            else
                input = (3 * input + 1) / 2;
            step += 1;
 
 
        }
        cout << step << endl;
    }
 
    return 0;
}

发表于 2020-02-13 12:12:19 回复(0)
#include<iostream>
using namespace std;
int judge(int n)
{
    int count = 0;
    if(n == 1)
    {
        return 0;
    }
    while(true)
    {
        if(n%2 == 0)
        {
            n = n /2;
        }
        else
        {
            n = n*3+1;
            n = n /2;
        }
        count++;
        if(n == 1)
        {
            break;
        }
    }
    return count;
}
int main()
{
    int n;
    while((cin>>n)&&(n!=0))
    {
        cout<<judge(n)<<endl;
    }
}

发表于 2019-08-28 16:57:16 回复(0)
#include<bits/stdc++.h>
int main(){
    int n,s=0;
    while(scanf("%d",&n)!=EOF && n!=0){
        while(n!=1){
            if(n&1) n=(3*n+1)/2;
            else n=n/2;
            s++;
        }
        printf("%d\n",s);
    }
}
发表于 2019-03-07 17:15:38 回复(1)
while True:
    try:
        num = int(input())
        if num==0:
            break
        result = 0
        while num != 1:
            if num%2==0:
                num//=2
            else:
                num = (num*3+1)//2
            result+=1
        print(result)
    except Exception:
        break
编辑于 2018-09-25 10:15:58 回复(0)
#include<iostream>
using namespace std;
int main()
{
    int n;
    while((cin>>n)&&(n!=0))
    {
        int k=0;
        int s=n;
        while(s!=1)
        {
            if(s%2==0)
            {
                s=s/2;
                k++;
            }
            else if(s%2==1)
            {
                s=(s*3+1)/2;
                k++;
            }
        }
        cout<<k<<endl;
    }
}

发表于 2018-09-10 09:44:27 回复(0)
//主要是对输入的数进行判断和处理
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int a = sc.nextInt();
            if (a == 0)
                return;
            else {
                int count = 0;
                while (a != 1) {
                    if (a % 2 == 0) {
                        a = a / 2;
                    } else {
                        a = (a * 3 + 1) / 2;
                    }
                    count++;
                }
                System.out.println(count);
            }
        }
    }
}

发表于 2018-05-29 10:17:01 回复(0)
属于较为简单的题,争取提高通过效率😏
#include<iostream>
#include<cstdio>
#define IsEven(x) ((x)%2==0?1:0)
using namespace std;
int main(){
    int n;
    while(cin>>n){
        int step = 0;
        while(n!=1){
            if(IsEven(n)){
                n = n/2;
                step++;
            }
            else{
                n = (3*n+1)/2;
                step++;
            }
        }
        cout<<step<<endl;
    }
    return 0;
}

发表于 2020-02-12 15:00:39 回复(0)
#include <cstdio>

int main(){
    int n;
    while((scanf("%d",&n))!=EOF){
        int count=0;
        while(n != 1){
            if(n%2==0){  //n为偶
                n = n/2;
            }else{     //n为奇
                n = (3*n+1)/2;
            }
            count++;
        }
        printf("%d\n",count);
    }
    return 0;
}

编辑于 2024-01-12 17:38:36 回复(0)
C++
#include<iostream>
using namespace std;

int main()
{
    int n = 0;
    while(cin>>n)
    {
        int step = 0;
        while(n!=1)
        {
            if(n%2==0) n/=2;
            else n=(3*n+1)/2;
            step++;
        }
        cout<<step<<endl;
    }
    return 0;
}


发表于 2024-01-08 21:47:22 回复(0)
#include <iostream>
using namespace std;

int main() {
    int n;
    while (cin >> n) {
        int time = 0;
        while (n != 1) {
            
            if (n % 2 == 0) {
                n = n / 2;
                time++;
            } else {
                n = (3 * n + 1) / 2;
                time++;
            }
        }
        cout<<time<<endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

发表于 2023-08-21 14:14:19 回复(0)
#include <iostream>
#include "cstdio"
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int step=0;
        while(n!=1){
            if(n%2==0){
                n=n/2;
            }else {
                n=(3*n+1)/2;
            }
            step++;
        }
         printf("%d\n",step);






    }
return 0;


}

发表于 2023-03-01 13:43:59 回复(0)
#include <cstdio>  int cut(int num){ int num2;  if(num%2==0){
        num2=num/2;  }else{
        num2=(num*3+1)/2;  } return num2; } int main() { int num;  while(scanf("%d",&num)!=EOF){ if(num==0){ break;  } int turns=0;  while(num!=1){
            turns++;  num=cut(num);  }
        printf("%d\n",turns);  } return 0; }

发表于 2023-02-23 14:52:04 回复(0)
#include<iostream>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        int count = 0;
        while (n != 1){
            if (n % 2 == 0){
                n = n / 2;
                count++;
            }
            else{
                n = (3 * n + 1) / 2;
                count++;
            }
           
        }
        cout<<count<<endl;
    }
    
    
    return 0;
}

发表于 2023-02-05 10:44:58 回复(0)
while循环,次数累加就行
#include <iostream>
using namespace std;

int main() {
    int a; int t = 0;
    while (cin >> a) {
        while (a != 1) {
            if (a % 2 == 0)
                a /= 2;
            else
                a = (3 * a + 1) / 2;
            t++;
        }
        cout << t;
        t = 0;
    }
}

发表于 2023-01-26 16:53:30 回复(0)

问题信息

难度:
83条回答 5244浏览

热门推荐

通过挑战的用户

查看代码