a数组开小了,$n \le 5000$
by StarsTwinkle @ 2024-02-14 12:03:18
@[sunyuchen1](/user/754119)
by StarsTwinkle @ 2024-02-14 12:03:28
@[sunyuchen1](/user/754119)
楼上非正解,倘若你这个程序满足 `i > 10` 但不满足后面的条件,它就会无限递归。
by spire001 @ 2024-02-14 12:15:57
@[sunyuchen1](/user/754119)
把我~~码了三分钟~~的代码给你吧:
```cpp
# include <iostream>
using namespace std;
constexpr int N = 12;
constexpr int m = 10;
int n;
int ans[10000][N] , cnt;
void dfs(int step , int sum)
{
if(step == m && sum == 0)
{
cnt++;
for(int i = 1; i <= m; i++) ans[cnt][i] = ans[0][i];
return;
}
if(step == m) return;
if(sum <= 0) return;
for(int i = 1; i <= 3 && sum >= i; i++)
{
ans[0][step + 1] = i;
dfs(step + 1 , sum - i);
}
return;
}
void solve()
{
dfs(0 , n);
cout << cnt << '\n';
for(int i = 1; i <= cnt; i++)
{
for(int j = 1; j <= m; j++)
cout << ans[i][j] << ' ';
cout << '\n';
}
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
if(n > 30) cout << 0;
else solve();
return 0;
}
```
by spire001 @ 2024-02-14 12:18:38
@[StarsTwinkle](/user/1267405) @[spire001](/user/998707)
已过,感谢
此贴结
by sunyuchen1 @ 2024-02-15 11:33:21