Manchester


私信TA

用户名:wenyajie

访问量:313210

签 名:

在历史前进的逻辑中前进,这个逻辑就是人心向背的逻辑

等  级
排  名 1
经  验 62797
参赛次数 1
文章发表 188
年  龄 0
在职情况 学生
学  校 Xiamen University
专  业 计算机科学

  自我简介:

在历史前进的逻辑中前进,这个逻辑就是人心向背的逻辑

解题思路:
输入一个数N;

N为偶数,输出N/2=,然后N/=2,判读N是否等于1,等于跳出循环;

N为奇数,输出N*3+1=,然后N=N*3+1,判读N是否等于1,等于跳出循环;

注意事项:
输入N=1时,循环不可结束

参考代码:

#include <iostream>
using namespace std;

int main()
{
    int N;
    cin >> N;

    while ( 1 )
    {
        if ( N % 2 == 0 )
        {
            cout << N << "/" << "2=" << (N / 2) << endl;
            N /= 2;
            if ( N == 1 )
                break;
        }else  {
            cout << N << "*3+1=" << (N * 3 + 1) << endl;
            N = N * 3 + 1;
            if ( N == 1 )
                break;
        }
    }
    return(0);
}

别忘点赞哦-.-

 

0.0分

17 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

何必呢,输入1循环了两次就出来了,多此一举吗不是
2023-12-09 22:56:25
何必呢,输入的是1,循环两次出来不还是1,多此一举吗不是
2023-12-09 22:55:02
#include<stdio.h>
int jiaogu(int n){
	if (n==1) return 0;
	if (n%2==0) {
		printf("%d/2=%d\n",n,n/2);
		return jiaogu(n/2);
		}
	else {
		printf("%d*3+1=%d\n",n,(n*3+1));
		return jiaogu(n*3+1);
		}	
	}
 
int main(void){
	int n;
	scanf("%d",&n);
	jiaogu(n);
	return 0;
	}
2021-07-28 18:16:22
#include <stdio.h>
int main()
{
	int a;
	scanf("%d",&a);
	while(a!=1)
	{
		int i,j;
		if(a%2==0)
		{
			i=a/2;
			printf("%d/2=%d\n",a,i);
			a/=2;
		}else
		{
			j=a*3+1;
			printf("%d*3+1=%d\n",a,j);
			a=a*3+1;
     	}
	}
	return 0;
}
2021-02-07 14:50:00
#include<stdio.h>
#pragma warning(disable:4996)
int main()
{
	int a;
	scanf("%d", &a);
	e:
	while (a != 1)
	{
		if (a % 2 == 0)
		{
			printf("%d/2=%d\n", a, a / 2);
			a /= 2;
			goto e;
		}
		else(a % 2 == 1);
		{
			printf("%d*3+1=%d\n", a, a * 3 + 1);
			a = a * 3 + 1;
			goto e;
		}
	}
	return 0;
}
2020-05-06 21:21:51
#include<stdio.h>
int main()
{
	int n=0;
	scanf("%d",&n);
	while(n!=1){
		if(n%2==0){
			printf("%d/2=",n);
			n=n/2;
			printf("%d\n",n);
		}
		else{printf("%d*3+1=",n);
		n=n*3+1;
		printf("%d\n",n);
		}
	}
	return 0;
}
2020-03-04 21:23:35
这小于小于什么鬼?
2019-05-30 10:50:56
  • «
  • 1
  • »