标签:fine and 输出 pre std style can cpp distrib
题目大意:把$x$个糖果分给$n$个人,必须分完,如果第$i$个人拿到$a_i$个糖果,就会开心,输出最多多少人开心
题解:从小到大排序,判断是否可以让他开心,注意最后判断是否要少一个人(没分完)
卡点:无
C++ Code:
#include <cstdio> #include <algorithm> #define maxn 111 int n, x, ans; int a[maxn]; int main() { scanf("%d%d", &n, &x); for (int i = 0; i < n; i++) scanf("%d", a + i); std::sort(a, a + n); for (int i = 0; i < n; i++) { if (x >= a[i]) x -= a[i], ans++; else break; } if (x && ans == n) ans--; printf("%d\n", ans); return 0; }
[AtCoder AGC27A]Candy Distribution Again
标签:fine and 输出 pre std style can cpp distrib
原文地址:https://www.cnblogs.com/Memory-of-winter/p/9771793.html