4 条题解

  • 1
    @ 2023-4-22 19:50:28

    一道比较简单的递归题

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    void out(long long x)
    {
        for(int i = 60;i >= 0;i--) //最大2^60(咋可能,我随便蒙的,啥都行,别太小)
        {
            if(pow(2,i) <= x)//如果2的i次方小于x,因为是逆着枚举的,所以这里是在找当2的i次方小于x时,最大的i
    		{
                if(i == 1) //如果是2的一次方不用输出2(1),直接输出2
    			{
    				cout << 2;
    			}
    			
                else if(i == 0)//如果是二的0次方也是直接输出
    			{
    				cout << "2(0)";
    			}
    			
                else//正常情况,还要继续找
    			{
                    cout << "2(";
    	            out(i);//再继续往下搜
    	            cout << ')';
                }
                
                x -= pow(2,i);//x减去这个数
                
                if(x != 0)//如果x减完后居然还有(???),简单地输出一个'+'
    			{
    				cout << '+';
    			}
            }
        }
    }
    
    int main()
    {
    	long long n;//输入,要开long long,别学我还用int
        cin >> n;
        out(n);
        return 0;//返回
    }
    
    • 1
      @ 2022-4-9 11:01:29
      #include <queue> 
      #include <math.h> 
      #include <stack> 
      #include <stdio.h>
      #include <iostream>
      #include <vector> 
      #include <iomanip> 
      #include <string.h> 
      #include <algorithm>
      #include<bits/stdc++.h>
      using namespace std;
      #define LL long long
      const int N=1e5+10;
      const int INF=0x3f3f3f3f;
      void f(int n)
      {
      	int a[30];
      	int len = 0;
      	while(n)
      	{
      		a[len++]=n%2;
      		n/=2;	
      	}
      	int flag = 0;
      	for(int i = len-1;i >= 0;i--)
      	{
      		if(a[i]!=0)
      		{
      			if(flag == 1)
      				cout <<"+";
      			flag=1;
      			if(i==0)
      				cout <<"2(0)";
      			else if(i == 1)
      				cout << "2";
      			else if(i == 2)
      				cout << "2(2)";
      			else
      			{
      				cout << "2(";
      				f(i);
      				cout << ")";
      			} 
      		}
      		
      	}
      }
      int main()
      {
      	int n;
      	cin>>n;
      	f(n);
      	return 0;
      }
      
      • 0
        @ 2024-7-18 11:25:29
        using namespace std;
        const int N=1e5+520;
        void f(int n)
        {
        	int a[30];
        	int len = 0;
        	while(n)
        	{
        		a[len++]=n%2;
        		n/=2;	
        	}
        	int k= 0;
        	for(int i = len-1;i >= 0;i--)
        	{
        		if(a[i]!=0)
        		{
        			if(k == 1)
        				cout <<"+";
        			k=1;
        			if(i==0)
        				cout <<"2(0)";
        			else if(i == 1)
        				cout << "2";
        			else if(i == 2)
        				cout << "2(2)";
        			else
        			{
        				cout << "2(";
        				f(i);
        				cout << ")";
        			} 
        		}
        		
        	}
        }
        int main()
        {
        	int n;
        	cin>>n;
        	f(n);
        	return 0;
        }
        
        
        • -1
          @ 2021-12-11 18:44:47
          #include <iostream>
          #include <stdio.h>
          #include <string.h>
          #include <queue>
          #include <math.h>
          #include <vector>
          #include <algorithm>
          #include <iomanip>
          #include <stack>
          
          using namespace std;
          
          #define LL long long
          const int N =1e5+10;
          const int INF =0x3f3f3f3f;
          void f(int n){
          	int a[40];
          	int len=0;
          	while(n){
          		a[len++]=n%2;
          		n/=2;
          	}
          	for(int i=len-1,flag=0;i>=0;i--){
          		if(a[i]!=0)
          		{
          			if(flag)
          				cout<<"+";
          			flag=1;
          			if(i==0)
          				cout<<"2(0)";
          			else if(i==1)
          				cout<<"2";
          			else if(i==2)
          				cout<<"2(2)";
          			else{
          				cout<<"2(";
          				f(i);
          				cout<<")";
          			}
          		}
          	}	
          }
          int main(){
          	int n;
          	cin>>n;
          	f(n);
          return 0;
          }
          
          • 1

          信息

          ID
          1226
          时间
          1000ms
          内存
          256MiB
          难度
          3
          标签
          递交数
          94
          已通过
          52
          上传者