4 条题解

  • 5
    @ 2022-9-3 0:33:48

    每次拆出一个最大的22xx次方没利用loglog函数硬拆存进数组,然后赋值intint变量,反着算回去就知道拆的谁,最后输出的时候特判一下,nn拆成0,肯定就是没问题的。

    #include <bits/stdc++.h>
    using namespace std;
    int n, a[1005], i = 1;
    int main()
    {
    
        cin >> n;
        while (n > 1)
        {
            int c = log(n) / log(2);
            n = n - pow(2, c);
            a[i] = pow(2, c);
            i += 1;
        }
        int k = i;
        if (n == 0)
        {
            for (int j = 1; j < k; j++)
            {
                cout << a[j] << " ";
            }
        }
        else
            cout << -1;
        return 0;
    }
    
    • 2
      @ 2023-6-30 11:55:54

      直接算出小于p的最大2次方数并输出就行了

      注:若不知为何p%2!=0就不可能有优秀拆分可查看说明
      AC代码,请查收
      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
          int p;
          cin >> p;
          if (p % 2 != 0)
          {
              cout << -1;
          }
          else
          {
              while (p >= 2)
              {
                  int y = 1,j = p;
                  while (j > 1)
                  {
                      j /= 2;
                      y *= 2;
                  }
                  p -= y;
                  cout << y << " ";
              }
          }
          
      }
      
      • @ 2023-10-6 14:52:46

        刚想写题解,发现有人和我思路一样pwp

    • 0
      @ 2024-4-23 17:06:13

      谁能比我更简单?

      #include <bits/stdc++.h>
      using namespace std;int main(){int n,a[25];for(int i=1;i<25;i++){a[i]=1<<i;cin>>n;}if(n&1){cout<<-1;return 0;}for(int i=24;i>0;i--)if(n>=a[i]{cout<<a[i]<<' ';n-=a[i];}
      
      • 0
        @ 2023-9-17 9:45:49

        发现我的最简单~~~ a数组存储2的n次方 倒着枚举 n比它大就输出 n减掉它 (当n为奇数时就输出-1,为什么就看题目吧)

        #include <bits/stdc++.h>
        using namespace std;
        int main()
        {
        int n,a[25];
        for(int i=1;i<25;i++)
        a[i]=1<<i;
        cin>>n;
        if(n&1){cout<<-1;return 0;}
        for(int i=24;i>0;i--)
        if(n>=a[i]){cout<<a[i]<<' ';n-=a[i];}
        return 0;
        }
        
        
        
        • 1

        [普及][CSP-J2020] 优秀的拆分

        信息

        ID
        1350
        时间
        1000ms
        内存
        256MiB
        难度
        6
        标签
        递交数
        611
        已通过
        190
        上传者