解题思路:

注意事项:

参考代码:

int main()
{
    int num,t;
    scanf("%d",&num);
    while(num!=1)
    {
        if(num%2==0)
        {
            t=num;                                                    //变量t是保存进行变化前的数,输出时的格式会用到
            num=num/2;
            printf("%d/2=%d\n",t,num);
            continue;                                                //continue直接回到循环,不然到1会1*3+1然后一直循环
        }                                                          
        if(num%2!=0)
        {
            t=num;
            num=num*3+1;
            printf("%d*3+1=%d\n",t,num);
            continue;                                                //continue直接回到循环
        }        
    }
    return 0;
}

 

0.0分

1 人评分

  评论区

直接用else不就可以不用continue了吗
2019-05-30 10:56:34
  • «
  • 1
  • »