在C ++中,最小数小于或等于N且总和为S

问题陈述

给定N个从1到N的数字和一个数字S。任务是打印总和为S的最小数字数

示例

如果n = 7且s = 10,则至少需要2个数字

(9, 1)
(8, 2)
(7, 3)
(6, 4)

算法

Answer can be calculated using below formula
(S/N) + 1 if { S %N > 0}

示例

#include <bits/stdc++.h>
using namespace std;
int getMinNumbers(int n, int s)
{
   return s % n ? s / n + 1 : s / 2;
}
int main(){
   int n = 7;
   int s = 10;
   cout << "Required minimum numbers = " <<
   getMinNumbers(n, s) << endl;
   return 0;
}

当您编译并执行上述程序时。它产生以下输出

输出结果

Required minimum numbers = 2