15 条题解

  • 3
    @ 2023-6-9 13:14:26

    解析

    计算 2n2^n 的结果。

    方案一

    使用<cmath>中的pow函数

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
       int n;
       cin >> n;
       cout << (int)(pow(2, n));
       return 0;
    }
    
    #include <iostream>
    #include <cmath>
    #include <cstdio>
    using namespace std;
    int main()
    {
       int n;
       cin >> n;
       printf("%.0f", pow(2, n));
       return 0;
    }
    

    方案二

    使用<<左移运算

    #include <iostream>
    using namespace std;
    int main()
    {
       int n;
       cin >> n;
       cout << (1 << n);
       return 0;
    }
    
    • 2
      @ 2024-4-19 20:01:23
      #include <iostream>
      using namespace std;
      int main()
      {
         int n;
         cin >> n;
         cout << (1 << n);
         return 0;
      }
      也是刚好学到左移运算符好吧
      
      • 1
        @ 2023-6-9 12:01:50
        #include<iostream>
        #include<cmath>
        using namespace std;
        int main()
        {
        	int n,mi;
        	cin>>n;
        	mi=pow(2,n);
        	cout<<mi;
        	return 0;
        }
        
        
        
        • @ 2023-6-9 13:11:27

          你们都不上学的么= =,我吃个饭的功夫,没有隐藏,就开始做了??

        • @ 2023-6-9 16:57:26

          欸嘿,高考放假

        • @ 2023-6-9 19:27:49

          666

      • 0
        @ 2024-6-5 19:40:23

        题解 c++

        #include <bits/stdc++.h> hetao4040809
        using namespace std;
        int main()
        {
           long long n;
           cin >> n;
           cout << (1 << n);
           return 0;
        }
        
        • 0
          @ 2024-5-24 17:09:43
          #include<iostream>
          #include<cstdio>
          #include<cmath>
          #include<iomanip>
          using namespace std;
          int main()
          {
              long long a;
              int b;
              cin >> a;
              b = pow(2,a);
              cout << b;
              return 0;
          }
          
          • 0
            @ 2024-4-14 21:36:20
            #include <iostream>
            #include <cmath>
            #include <cstdio>
            using namespace std;
            int main()
            {
                int a;
                cin>>a;
                printf("%.0f",pow(2,a));//此处四舍五入到整数位
                return 0;
            }
            
            • 0
              @ 2023-10-30 17:24:55

              op[1]=1;

              int n,len=1;

              cin >> n;

              for(int i=1; i<=n; i++){

              int x=0;

              for(int j=1; j<=len; j++){

              op[j]=op[j]*2+x;

              x=op[j]/10;

              op[j]=op[j]%10;

              if(x!=0 && j==len)len++;

              }

              }

              for(int i=len; i>=1; i--)cout << op[i];

              高精度做法!!!

              • 0
                @ 2023-9-17 9:05:23

                这题简单诶!位运算听说过吗?没听说过?题这么多年都没有变过难度的,你不会位运算,有没有好好学C++啊(佳里佳气

                位运算,把十进制数转化为二进制数进行运算

                e.g.:1<<2 乘2的2次方

                10>>1 除以2(有1自动省略,例如:3>>1=1)

                其他的csp复习课都会讲,我就不说啦,直接上代码喽

                #include <bits/stdc++.h>
                using namespace std;
                int x;
                int main()
                {
                    cin>>x;
                    cout<<(int)(1<<x);
                    return 0;
                }
                

                オーケー!(わん にあん ぶ びあん の くぃう ざん)

                • 0
                  @ 2023-8-23 10:34:34
                  #include<bits/stdc++.h>
                  using namespace std;
                  int main()
                  {
                      int i;
                      cin >> i;
                      cout << int(pow(2,i));
                      return 0;
                  }
                  
                  • 0
                    @ 2023-8-5 21:42:17
                    #include <iostream>
                    using namespace std;
                    int main(){
                        int n;
                        cin>>n;
                        cout<<(1<<n)<<endl;
                        return 0;
                    }
                    
                    • 0
                      @ 2023-7-31 13:32:04
                      #include <bits/stdc++.h>
                      using namespace std;
                      int main()
                      {
                          int n, m = 1;
                          cin >> n;
                          for (int i = 1; i <= n; i++)
                          {
                              m *= 2;
                          }
                          cout << m;
                          return 0;
                      }
                      
                      • 0
                        @ 2023-7-6 21:02:27

                        解析

                        计算 2^n2n 的结果。

                        #include<iostream>
                        #include<cmath>
                        using namespace std;
                        int main()
                        {
                        	int n,mi;    //定义变量
                        	cin>>n;      //输入变量
                        	mi=pow(2,n); //幂运算
                        	cout<<mi;
                        	return 0;
                        }
                        
                        • 0
                          @ 2023-6-11 14:08:35

                          这里会有两种作法,一种使用prinf,一种用cout强转int。

                          第一种(prinf法) 1.导入cmath,这样才能成功使用pow 2.导入cstdio,使用printf 3.用int创建一个变量a 4.用cin接收输进来的数字 5.使用printf,注意占位符“%d”是int的占位符,调用pow函数的返回值是double,并非int,占位符应为“%0lf” (printf("0lf",pow(2,a))) 6.试一试吧

                          第二种(cout转int法) 1.一样的,先导入cmath,注意不需要导入cstdio了 2.用int创建变量a 3.使用cin接收数字 4.使用cout打印出pow(2,a),这里注意一下,打印出的数字后面有0.000000,将pow(2,a)在cout中直接强转int即可。(cout << (int)(pow(2,a))) 5.试一试吧

                          • 0
                            @ 2023-6-11 13:53:15

                            题解

                            思路:

                            第一种:用cmath</strong</matk>的pow函数 tip:记得输出时要加上<matk>“(int)”强制类型转换

                            第二种:用位运算符“<<”

                            偷懒简化

                            直接输出结果,不参与除输入外的其它变量

                            参考代码

                            #include <iostream>
                            using namespace std;
                            int main()
                            {
                               int n;
                               cin >> n;
                               cout << (1 << n);
                               return 0;
                            }
                            
                            • 0
                              @ 2023-6-10 9:57:56

                              思路:

                              2的0次方 = 1

                              转二进制为1

                              2的1次方 = 2

                              转二进制为10

                              2的2次方 = 4

                              转二进制为100

                              得出结论:

                              2的n次方 = 将1左移n位

                              所以只需要获取n再输出1 << n就行力(喜)

                              #include <iostream>
                              using namespace std;
                              
                              int main()
                              {
                                  int n;
                                  cin >> n;
                                  cout << (1 << n);
                                  return 0;
                              }
                              
                              • 1

                              信息

                              ID
                              135
                              时间
                              1000ms
                              内存
                              256MiB
                              难度
                              6
                              标签
                              递交数
                              3424
                              已通过
                              1076
                              上传者