AC了,但我想不明白

B2077 角谷猜想

LeeJC @ 2022-10-14 16:32:43

#include <stdio.h>
int main(void)
{
    long long N;
    scanf("%d",&N);
    while(1)
    {
        if(N%2!=0 && N!=1)
        {
            printf("%d*3+1=%d\n",N,3*N+1);
            N=3*N+1;
        }
        if(N%2==0)
        {
            printf("%d/2=%d\n",N,N/2);
            N/=2;
        }
        if(N==1)
        {
            printf("End");
            break;
        }
    }
    return 0;
}

dev上显示有编译错误,并且我输入了1之后会变成0/2无限循环下去


by lingfunny @ 2022-10-14 16:34:15

long long N;
scanf("%d", &N);

by ivoleuoy @ 2022-10-14 16:36:34

显然一个long long类型的数不应该用%d来输入。


by Binaries @ 2022-10-14 16:54:42

@lingfunny 您的酷炫badge是怎样实现的(((


by lingfunny @ 2022-10-14 17:29:07

@Binaries

背景那栏填这个:url('https://pic.imgdb.cn/item/62f4683316f2c2beb1aab22a.gif')


by xiexiaoyuawa @ 2023-01-14 12:05:06

这样会更好一点吧,循环条件改一下

#include<bits/stdc++.h>
using namespace std;
long long n;
int main(){
    cin>>n;
    while(n != 1){
        if(n%2){
            printf("%d*3+1=%d\n",n,n*3+1);
            n = n*3+1;
        }
        else{
            printf("%d/2=%d\n",n,n/2);
            n = n/2;
        }
    }
    printf("End");
}

by xiexiaoyuawa @ 2023-01-14 12:06:25

而且这个long long应该用%lld吧


|