1 solutions

  • 0
    @ 2025-11-5 15:24:34

    C++ :

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    
    using namespace std;
    
    int a[10001]={1},n,total;
    int search(int,int);
    int print(int);
    
    int main()
    {
    	cin>>n;
    	search(n,1);
    	//cout<<"total="<<total<<endl;
    } 
    int search(int s,int t)
    {
    	int i;
    	for(i=a[t-1];i<=s;++i)
    	if(i<n)
    	{
    		a[t]=i;
    		s-=i;
    		if(s==0)print(t);
    			else search(s,t+1);
    		s+=i;
    	}
    }
    
    int print(int t)
    {
    	//cout<<n<<"=";
    	for(int i=1;i<=t-1;i++)
    	cout<<a[i]<<"+";
    	cout<<a[t]<<endl;
    	total++;
    }
    

    Pascal :

    var
       n:longint;
       a:array[0..1000]of longint;
    procedure dfs(t,s:longint);
    var
       i:longint;
    begin
     if (s>n) then exit;
     if (s=n)and(t<>2) then
      begin
       for i:=1 to t-2 do write(a[i],'+');
       writeln(a[t-1]);
       exit;
      end;
     if s=n then exit;
     for i:=a[t-1]to n do
      begin
       a[t]:=i;
       dfs(t+1,s+i);
      end;
    end;
    begin
     a[0]:=1;
     readln(n);
     dfs(1,0);
    end.
    
    • 1

    Information

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