nyoj 791 Color the fence(贪心)
Color the fence
時間限制:1000?ms ?|? 內存限制:65535?KB 難度:2 描述Tom?has?fallen?in?love?with?Mary.?Now?Tom?wants?to?show?his?love?and?write?a?number?on?the?fence?opposite?to?
Mary’s?house.?Tom?thinks?that?the?larger?the?numbers?is,?the?more?chance?to?win?Mary’s?heart?he?has.
Unfortunately,?Tom?could?only?get?V?liters?paint.?He?did?the?math?and?concluded?that?digit?i?requires?ai?liters?paint.?
Besides,Tom?heard?that?Mary?doesn’t?like?zero.That’s?why?Tom?won’t?use?them?in?his?number.
Help?Tom?find?the?maximum?number?he?can?write?on?the?fence.
輸入Each case the first line contains a nonnegative integer V(0≤V≤10^6).
The second line contains nine positive integers a1,a2,……,a9(1≤ai≤10^5).
解題思路:
首先要肯定一點,數字位數越多,肯定會越大。所以這里可以采用貪心的思想,假設最小的顏料的用量為min,那么最長的位數可以達到V / min,接下來我們就枚舉每一位可能的數字即可。這里還需要考慮,取了某一個數后,剩下的是否還可以保證最大。。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int a[10]; int main() {int n;while(~scanf("%d", &n)){int Min = 0x3f3f3f3f;for(int i = 1; i < 10; i++){scanf("%d", &a[i]);Min = min(Min, a[i]);}if(n < Min){puts("-1");continue;}for(int i = n/Min-1; i >= 0; i--){for(int j = 9; j > 0; j--){if(n >= a[j] && (n - a[j])/Min >= i) //保證去了j之,還可以最長。。{printf("%d", j);n -= a[j];break;}}}puts("");}return 0; }總結
以上是生活随笔為你收集整理的nyoj 791 Color the fence(贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UI标签库专题九:JEECG智能开发平台
- 下一篇: jeecg 如何重新加载当前渲染tab页