1 solutions

  • 0
    @ 2025-11-5 16:30:56

    C++ :

    #include <cstdlib>
    #include <cstdio>
    using namespace std;
    bool even(long long);
    int main()
    {
    	long long  n;
    	long long c=0;
    	long long k;
    	scanf("%lld",&k);
    	while(k!=1)
    	{
    		if(even(k)==false)
    		{
    			long long  a=3*k+1;
    			printf("%lld*3+1=%lld\n",k,a);
                k=a;
    		}
    		else
    		{
    			long long a=k/2;
    			printf("%lld/2=%lld\n",k,a);
    			k=a;
    		}
    	}
    	//system("pause");
    	return 0;
    }
    bool even(long long a)
    {
    	if(a%2==0)
    	    return true;
    	else
    		return false;
    }
    
    

    Pascal :

    var n,tot: Longint;
    begin    
      readln(n);    
      tot:= 1;    
      while n > 1 Do    
      begin       
        write(n,' ');        
        if odd(n) then n:=n*3+1       
        else n:=n div 2;       
        inc(tot);   
      end;   
      writeln(1);   
      writeln(tot);
    end.
    
    • 1

    Information

    ID
    17819
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By