51 条题解

  • 20
    @ 2022-8-16 18:19:47

    角谷猜想,非常著名的猜想之一,来看看这道题吧~

    1. 用 while 语句来完成,接下来就是循环内的代码啦
    2. 如果是奇数:输出 这个数 * 3 + 1 = 这个数 * 3 + 1,并且把 a 的值也重新赋值一下下
    3. 否则:输出 这个数 / 2 = 这个数 / 2,同样赋值给 a /= 2
    4. 注意!:每一次输出后都要换行呦!!!!

    上代码吧~(AC过!说了多少遍了,相信UP)

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    	int a;
    	cin >> a;
    	while (a != 1)
    	{
    		if (a % 2 == 1)
    		{
    			cout << a << "*" << 3 << "+" << 1 << "=" << a * 3 + 1 << endl;
    			a = a * 3 + 1;
    		}
    		else
    		{
    			cout << a << "/" << 2 << "=" << a / 2 << endl;
    			a = a / 2;
    		}
    	}
    	cout << "End";
    	return 0;
    }
    
    • @ 2022-8-29 14:52:44

      我当然相信你

    • @ 2022-10-6 10:38:10

      本大爷永远相······口误······人家永远相信你!

    • @ 2022-11-29 20:49:40

      emm············································

    • @ 2022-12-13 14:20:08

      把“*3+1=”和“/2=”写一起不行吗??

    • @ 2022-12-31 20:01:33

      我......

    • @ 2023-8-12 18:27:19

      @ 当然可以押

    • @ 2024-3-24 14:16:45

      你b站几级了?

    • @ 2024-3-24 14:17:24

      @不能

    • @ 2024-5-26 17:30:33

      666@

  • 7
    @ 2023-11-2 16:27:35
    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    	int n;
    	cin>>n;
    	while(n!=1){
    		if(n&1){//n不是2的倍数
    			cout<<n<<"*3+1="<<n*3+1<<endl;
    			n=n*3+1;
    		}else{//n是二的倍数
    			cout<<n<<"/2="<<n/2<<endl;
    			n/=2;
    		}
    	}
    	puts("End");//n等于1了,可替换成printf("End")或cout<<"End"
    	return 0;
    }
    
    • 4
      @ 2023-12-30 21:51:25
      int n;
          cin >> n;
          while(n > 0)
          {
              if(n == 1)
              {
                  cout << "End";
                  break;
              }
              else if(n % 2 ==1)
              {
                  cout << n << "*" << 3 << "+" << 1 << "=" << n*3+1;
                  n = n*3+1;
              }
              else
              {
                  cout << n << "/" << 2 << "=" << n/2;
                  n = n/2;
              }
              cout << endl;
          }
      

      核心部分

      长又累赘,仅供小白参考。 (今天第一次进前一千名,前一百等我😄)

      • @ 2024-3-2 13:10:51

        虽然但是,这是最简单的写法辣👍

    • 4
      @ 2023-12-30 20:52:39

      赞 -> 看 懂?

      #include <iostream>
      // 你觉得万能头文件这类东西是你该学的吗
      using namespace std;
      int main()
      {
          int n;
          cin >> n;
          //核心循环
          while (n != 1)
          {
              if (n % 2 == 1)//是奇数
              {
                  cout << n << "*3+1=" << n * 3 + 1 <<endl;
                  n = n*3+1;
              }
              else//是偶数
              {
                  cout << n << "/2=" << n/2 << endl;
                  n = n/2;
              }
          }
          cout << "End";//结束
          return 0;
      }
      

      已AC,请放心食用

      快给瓦点赞!!!!

      • 3
        @ 2023-11-29 22:04:00

        递归

        #include<bits/stdc++.h>
        using namespace std;
        int n;
        void num(int k); 
        int main(){
            scanf("%d",&n);
            num(n);
            return 0;
        }
        void num(int k){
            if(k == 1){
                printf("End");
                return;
            }
            if(k&1){
                printf("%d*3+1=%d\n",k,k*3+1);
                num(k*3+1);
                return;
            }
            printf("%d/2=%d\n",k,k/2);
            num(k/2);
            return;
        }
        
        • 3
          @ 2023-10-3 18:50:29
          #include <bits/stdc++.h>
          using namespace std;
          int as(int a){
              int b;
              if (a%2==1){//判断奇数偶数
                  b=a*3+1;//带入公式
                  cout<<a<<"*3+1="<<b<<endl;
              }
              else{b=a/2;cout<<a<<"/"<<2<<"="<<b<<endl;}//否则...
              return b;//返回b的值
          }
          int main(){
              int a;
              cin>>a;
              while (a!=1){//循环到a=1
                  a=as(a);
              }
              cout<<"End\n";
          }
          
          • @ 2024-3-2 13:09:51

            大哥啊,你这个太复杂了嘞

        • 2
          @ 2023-9-11 21:33:43

          这个代码是AC了,快点赞

          #include <bits/stdc++.h> using namespace std; long long n; int main(){ scanf("%d",&n); while (n!=1){ if (n%2==0){ cout << n << "/2=" << n/2; n/=2; } else{ cout << n << "3+1=" << n3+1; n*=3; n++; } cout << endl; } cout << "End"; return 0; }

          • 2
            @ 2023-8-30 10:26:48

            过辣!

                long long n;
                cin>>n;
                while (n!=1)//因为不知道会循环多少次,用while
                {
                    cout<<n;
                    //先输出n,再根据n的奇偶性进行进一步的操作
                    if (n%2==1)
                    {
                        n=n*3+1;
                        cout<<"*3+1="<<n<<endl;
                    }
                    else
                    {
                        n/=2;
                        cout<<"/2="<<n<<endl;
                    }
                }
                cout<<"End";//因为while循环条件为n不等于1,所以当n为1时会退出循环。于是我们就要在while之后再输出“End”。
            
            • 2
              @ 2023-8-25 22:05:08

              题解

              考虑使用 while\verb!while! 循环解题。当 n=1n=1 时,就退出;否则,

              • n1(mod2)n\equiv 1\pmod 2 时,根据题面上的格式输出,并让 n3×n+1n\gets 3\times n+1
              • n0(mod2)n\equiv 0\pmod 2 时,同样地,根据题面上的格式输出后令 nn÷2n\gets n\div 2

              最后使用 puts\verb!puts! 函数(它可以用来输出一个 C风格字符串并换行),输出 End\verb!End! 。如 puts("End")


              在 C++ 的表达式的计算中,它是先计算完表达式的值后再返回的。特别地,对于赋值语句(例如 t=3*n+1 ),那么它会先将 3×n+13\times n+1 的结果计算出来,赋值到 tt 上,然后将 t\bm t 的值作为表达式的返回值。因此我们可以这样简化一下代码。

              AC代码

              #include<bits/stdc++.h>
              #define up(l,r,i) for(int i=l,END##i=r;i<=END##i;++i)
              #define dn(r,l,i) for(int i=r,END##i=l;i>=END##i;--i)
              using namespace std;
              typedef long long i64;
              const int INF =2147483647;
              int n,t;
              int main(){
                  scanf("%d",&n); while(n!=1){
                      if(n%2==1) printf("%d*3+1=%d\n",n,t=n*3+1);
                      else       printf("%d/2=%d\n",n,t=n/2);
                      n=t;
                  }
                  puts("End");
                  return 0;
              }
              
              • 1
                @ 2024-5-5 20:58:03
                #include<iostream>
                using namespace std;
                int main()
                {
                    int a;
                    cin>>a;
                    if(a==1)
                    {
                        cout<<"End";
                        return 0;
                    }
                    while(a!=1)
                    {
                        if(a%2==0)
                        {
                            cout<<a<<"/2="<<a/2<<endl;
                            a/=2;
                            if(a==1)
                            {
                                cout<<"End";
                                return 0;
                            }
                        }
                        if(a%2!=0)
                        {
                            cout<<a<<"*3+1="<<a*3+1<<endl;
                            a=a*3+1;
                    
                        }
                    }
                }
                

                100AC.绝对通过🚀️

                • 1
                  @ 2024-4-6 14:49:23
                  # 不用while(还没学)
                  #include <iostream>
                  using namespace std;
                  int main()
                  {
                      int n;
                      cin >> n;
                      for (int i;n != 1;i++)
                      {
                          if (n % 2 == 0)
                          {
                              cout << n << "/2=" << n/2 << endl;
                              n=n/2;
                          }
                          else
                          {
                              cout << n << "*3+1=" << n*3+1 << endl;
                              n = n*3+1;
                          }
                      }
                      cout << "End";
                      return 0;
                  }
                  
                • 1
                  @ 2024-2-20 20:24:54
                  //此题按题意编程即可~(别看它标题很“高深”,实际上……)
                  #include<iostream>
                  using namespace std;
                  int n;
                  int main(){
                  	cin>>n;
                  	while(n!=1){
                          //如果是奇数则*3+1
                  		if(n%2==1){
                  			cout<<n<<"*3+1="<<n*3+1<<endl;
                  			n=n*3+1;
                  		}
                          //如果是偶数则/2
                  		else{
                  			cout<<n<<"/2="<<n/2<<endl;
                  			n/=2;
                  		}
                  	}
                  	cout<<"End";
                  	return 0;
                  }//原题:角谷猜想
                  //编者:@Royal
                  
                  • 1
                    @ 2023-10-2 21:46:04
                    #include<bits/stdc++.h>
                    using namespace std;
                    int main()
                    {
                        int a;
                        cin>>a;
                        while(a!=1)
                        {
                            if(a%2==1)
                            {
                                cout<<a<<"*3+1=";
                                a=a*3+1;
                                cout<<a<<endl;
                            }
                            else
                            {
                                cout<<a<<"/2=";
                                a=a/2;
                                cout<<a<<endl;
                            }
                        }
                        cout<<"End";
                        return 0;
                    }
                    
                    • 1
                      @ 2023-8-28 18:05:29
                      角谷猜想,非常著名的猜想之一,来看看这道题吧~
                      1. 用 while 语句来完成,接下来就是循环内的代码啦
                      2. 如果是奇数:输出 这个数 \* 3 + 1 = 这个数 \* 3 + 1,并且把 a 的值也重新赋值一下下
                      3. 否则:输出 这个数 / 2 = 这个数 / 2,同样赋值给 a /= 2
                      4. 注意!:每一次输出后都要换行呦!!!!
                      
                      
                      
                      
                      
                      
                      
                      
                      ```#include <iostream>
                      using namespace std;
                      int main()
                      {
                          int n;
                          cin >> n;
                          while (n!=1)
                          {
                              if (n%2==1)
                              {
                                  cout << n << "*3+1=" << n*3+1;
                                  n*=3;
                                  n++;
                              }
                              else
                              {
                                  cout << n << "/2=" << n/2;
                                  n/=2;
                              }
                              cout << endl;
                          }
                          cout << "End";
                          return 0;
                      }
                      
                      
                      
                      
                      
                      
                      
                      已AC
                      快点赞!
                      
                    • 1
                      @ 2023-7-6 16:47:40
                      #include <iostream> // 使用"#include"指令导入"iosteeam"库.
                      using namespace std; // 使用标准的命名空间.
                      
                      int main()
                      {
                      	int n; // 定义一个正整数"n".
                      	cin >> n; // 输入n的值.
                      	while (true) // 无限循环.
                      	{
                      		if (n == 1) // 当n的值为1时输出"End"并结束循环.
                      		{
                      			cout << "End";
                      			break;
                      		}
                      		else // 如果n的值不为1,就判断n的值是奇数还是偶数.
                      		{
                      			if (n % 2 == 1) // 当n的值为奇数时.
                      			{
                      				cout << n << "*3+1=" << n * 3 + 1 << endl; // 输出n乘3加1及其结果.
                      				n = n * 3 + 1; // 将n的值转化为n乘3加1的结果.
                      			}
                      			else // 如果n的值不为奇数,而是偶数时.
                      			{
                      				cout << n << "/2=" << n / 2 << endl; // 输出n除以2及其结果.
                      				n /= 2; // 将n的值转化为n除以2的结果.
                      			}
                      		}
                      	}
                      	return 0; // 返回值为0,程序正常退出;
                      }
                      
                      • 0
                        @ 2024-4-19 21:28:24
                        #include <iostream>
                        using namespace std;
                        int q,w;
                        int main()
                        {
                            cin >> q;
                            while (q != 1)
                            {
                                if (q % 2 == 1)
                                {
                                    w = q;
                                    q = q * 3;
                                    q = q + 1;
                                    cout << w << "*3+1=" << q << endl;
                                }
                                if (q % 2 == 0)
                                {
                                    w = q;
                                    q = q / 2;
                                    cout << w << "/2=" << q << endl;
                                }
                            }
                            cout << "End";
                        	return 0;
                        }
                        
                        • 0
                          @ 2024-4-19 21:27:06
                          #include <iostream>
                          using namespace std;
                          int q,w;
                          int main()
                          {
                              cin >> q;
                              while (q != 1)
                              {
                                  if (q % 2 == 1)
                                  {
                                      w = q;
                                      q = q * 3;
                                      q = q + 1;
                                      cout << w << "*3+1=" << q << endl;
                                  }
                                  if (q % 2 == 0)
                                  {
                                      w = q;
                                      q = q / 2;
                                      cout << w << "/2=" << q << endl;
                                  }
                              }
                              cout << "End";
                          	return 0;
                          }
                          
                          • 0
                            @ 2024-4-19 21:26:34
                            #include <iostream>
                            using namespace std;
                            int q,w;
                            int main()
                            {
                                cin >> q;
                                while (q != 1)
                                {
                                    if (q % 2 == 1)
                                    {
                                        w = q;
                                        q = q * 3;
                                        q = q + 1;
                                        cout << w << "*3+1=" << q << endl;
                                    }
                                    if (q % 2 == 0)
                                    {
                                        w = q;
                                        q = q / 2;
                                        cout << w << "/2=" << q << endl;
                                    }
                                }
                                cout << "End";
                            	return 0;
                            }
                            
                            • 0
                              @ 2024-4-15 21:34:56

                              蒟蒻扫盲计划NO.4

                              (核心)代码:

                              while法:

                              while(n!=1){
                                  if(n%2!=0){
                                      n=n*3+1;
                                  }else{
                                      n=n/2;
                                  }
                                  cout<<n<<" ";
                              }
                              cout<<"End";
                              //编者注:此为核心代码,头文件什么的自己去加!!!忘加了没有AC不怪我!!!
                              

                              do_while法:

                              do{
                                  if(n%2!=0){
                                      cout<<n<<"*3+1="<<n*3+1<<endl;
                                      n=n*3+1;
                                  }else{
                                      cout<<n<<"/2="<<n/2<<endl;
                                      n=n/2;
                                  }
                              }while(n!=1);
                                  cout<<"End";
                              //编者注:此为核心代码,头文件什么的自己去加!!!忘加了没有AC不怪我!!!
                              

                              总结:

                              难点:while和do_while的出口

                              考点:while和do_while的运用

                              综合指数:●●

                              • @ 2024-4-15 21:41:36

                                追加一条:

                                do_while法前加一个

                                if(n==1){
                                    cout<<"End";
                                    return 0;
                                }
                                

                                不然会导致输入1,输出多的情况

                            • 0
                              @ 2024-3-17 9:36:03

                              用while语句就可以了

                              #include<bits/stdc++.h>
                              using namespace std;
                              int main()
                              {
                                  int n;
                                  cin>>n;
                                  while(n!=1)
                                  {
                                      if(n%2==1)
                                      {
                                          cout<<n<<"*3+1="<<n*3+1<<endl;
                                          n=n*3+1;
                                      }
                                      if(n%2==0)
                                      {
                                          cout<<n<<"/2="<<n/2<<endl;
                                          n=n/2;
                                      }
                                  }
                                  cout<<"End";
                                  return 0;
                              }
                              

                              信息

                              ID
                              1511
                              时间
                              1000ms
                              内存
                              256MiB
                              难度
                              4
                              标签
                              递交数
                              2423
                              已通过
                              1139
                              上传者