3 条题解

  • 1
    @ 2025-5-20 16:50:48
    #include <bits/stdc++.h> 
    using namespace std;
    int n , ans , a[114514] ;
    void f(int n , int last , int step , int k )
    {
    	if ( last > n && n != 0 ) return;
    	if( n == 0 && step > 1 )
    	{
    		ans++;
    		cout << k << "=" ;
    		for( int i = 0 ; i < step - 1 ; i++ )
    		{
    			cout << a[ i ] << "+" ;
    		}	
    		cout << a[ step - 1 ] << endl ;
    		return;
    	}
    	for ( int i = last ; i <= n ; i++)
    	{
    		a[ step ] = i ;
    		f ( n - i , i , step + 1 , k );
    	}
    }
    int main()
    {
    	cin >> n ;
    	f( n , 1 , 0 , n);
    	cout << "total=" << ans << endl ;
    	return 0;
    }
    
    
    
    • -1
      @ 2023-6-5 17:40:35
      #include <queue>
      #include <math.h>
      #include <stack>
      #include <vector>
      #include <stdio.h>
      #include <iostream>
      #include <vector>
      #include <iomanip>
      #include <string.h>
      #include<cstring>
      #include <algorithm>
      #define LL long long
      const int N = 1e2 + 10;
      const int INF = 0x3f3f3f3f;
      using namespace std;
      int n , ans , a[N] ;
      void f(int n , int last , int step , int k )
      {
      	if ( last > n && n != 0 ) return;
      	if( n == 0 && step > 1 )
      	{
      		ans++;
      		cout << k << "=" ;
      		for( int i = 0 ; i < step - 1 ; i++ )
      		{
      			cout << a[ i ] << "+" ;
      		}	
      		cout << a[ step - 1 ] << endl ;
      		return;
      	}
      	for ( int i = last ; i <= n ; i++)
      	{
      		a[ step ] = i ;
      		f ( n - i , i , step + 1 , k );
      	}
      }
      int main()
      {
      	cin >> n ;
      	f( n , 1 , 0 , n);
      	cout << "total=" << ans << endl ;
      	return 0;
      }
      
      • -1
        @ 2022-1-9 17:42:38
        #include <queue>
        #include <math.h>
        #include <stack>
        #include <stdio.h>
        #include <iostream>
        #include <vector>
        #include <iomanip>
        #include <string.h>
        #include <algorithm>
        using namespace std;
        #define LL long long
        const int N = 1e6 + 10;
        const int INF = 0x3f3f3f3f;
        int ans[N]; 
        int n, total;
        void pr(int d){ 
            total+=1;
            cout<<n<<"=";
            for(int i=1; i<=d-1; i++)cout<<ans[i]<<"+";
            cout<<ans[d]<<endl;
        }
        void dfs(int dep, int rest){
            if(rest==0){
                if(dep>2){ 
                    pr(dep-1);
                    return;
                }
            }
            for(int i=ans[dep-1]; i<=rest; i++){
                ans[dep]=i;
                dfs(dep+1, rest-i);
            }
        }
        int main()
        {
            cin>>n;
            ans[0]=1;
            dfs(1, n);
            cout<<"total="<<total;
            return 0;
        }
        • 1

        信息

        ID
        1242
        时间
        1000ms
        内存
        256MiB
        难度
        4
        标签
        递交数
        353
        已通过
        158
        上传者