1 solutions

  • 0
    @ 2024-12-11 0:16:56

    C++ :

    #include<cstdio>
    #include<string>
    #include<iostream>
    using namespace std;
    long n,a[10],m,total=0;
    bool c[10];
    long p(long t)
    {
    	int ii;total++;
    	cout<<n<<"=";
    	for (ii=1; ii<=t-1; ii=ii+1)
    	  cout<<a[ii]<<"+";
    	cout<<a[t]<<endl;
    	return 0;
    }
    long se(long s,long t)
    {
        int k;
        if (s==0) {p(t-1); return 0;} 
        for (k=1; k<=s; k++)
          if ((a[t-1]<=k)&&(k<n)){
    			a[t]=k;
    			s=s-k;
    			se(s,t+1);
    			s=s+k;
          }
    	return 0;
    }
    int main()
    {
        int i;
        cin>>n;
    
        for (i=1; i<=10; i++) c[i]=0; 
        se(n,1);
    	cout<<"total="<<total<<endl;
    	return 0;
    }
    
    
    • 1

    Information

    ID
    682
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By