代码中的世界 2014-12-19 17:36 采纳率: 100%
浏览 1680
已采纳

哎,我也就郁闷了,怎么简单的题自己竟然找不见错误。

问题描述:
对于每一个大于1的正整数,如果它是奇数,则将其乘3加1,如果它是偶数,则将除以2,如此循环,最终将得到1。输出数据包含从这个整数到1的按照叙拉古猜想变换的序列,每行一个数字。

 #include <stdio.h>

int main()
{
    int a;
    scanf("%d",&a);
    while(1)
    {
        if( a%2 == 0 )
        {
            a = a / 2;
            printf("%d\n",a);
        }
        if( a%2 != 0 )
        {
            a = a * 3 + 1;
            printf("%d\n",a);
        }
        if( a == 1 )    break;
    }
    return 0;
}

  • 写回答

3条回答 默认 最新

  • threenewbee 2014-12-19 20:15
    关注
     // ConsoleApplication1.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "math.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int a;
        scanf_s("%d", &a);
        while (1)
        {
            if (a == 1)
                break;
            if (a % 2 == 0)
            {
                a = a / 2;
                printf("%d\n", a);
                continue;
            }
            if (a % 2 != 0)
            {
                a = a * 3 + 1;
                printf("%d\n", a);
                continue;
            }
        }
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器