C++ 代码找到表示 n 的最小不同数字

阅读 104 收藏 0 点赞 0 评论 0

假设我们有一个数字 n。我们想将其拆分为一些总和为 n 的非零数字。我们希望找到一个尽可能少的不同数字的解决方案。

所以,如果输入像 n = 13,那么输出将是 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

脚步

为了解决这个问题,我们将遵循以下步骤 -

for initialize i := 0, when i < n, update (increase i by 1), do:
   print 1

示例

让我们看看以下实现以更好地理解 -

#include <bits/stdc++.h>
using namespace std;
void solve(int n){
   for (int i = 0; i < n; i++)
   printf("1, ");
}
int main(){
   int n = 13;
   solve(n);
}

输入

13
输出结果
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号