Since n ≤ 21 n\le 21 n≤21, O ( n 3 ) \mathcal O (n^3) O(n3) brute force enumeration would suffice. Lexicographic order are trivial in this case with nested for loops.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
int n;
inline int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0' || ch>'9'){if(ch=='-')w-=1;ch=getchar();}
while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
int main()
{
n=read();
for(int i=0;i<=n;++i)
for(int j=0;j<=n;++j)
for(int k=0;k<=n;++k)
if(i+j+k<=n)
{
printf("%d %d %d\n",i,j,k);
}
return 0;
}