码迷,mamicode.com
首页 > 其他好文 > 详细

AGC027 A - Candy Distribution Again

时间:2018-09-25 22:56:44      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:define   cpp   break   inline   line   clu   ||   read   cst   

目录



题目链接

AGC027 A - Candy Distribution Again

题解

贪心即可

代码

#include<cstdio> 
#include<cstring> 
#include<algorithm> 
#define gc getchar() 
#define pc putchar 
inline int read() {
    int x = 0,f = 1; 
    char c = gc; 
    while(c < '0' || c > '9') {if(c == '-')f = -1; char c = getchar();} 
    while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar(); 
    return x * f; 
} 
void print(int x) { 
    if(x < 0) { 
        pc('-'); 
        x = -x; 
    } 
    if(x >= 10) print(x / 10); 
    pc(x % 10 + '0'); 
} 
const int maxn = 100007; 
int a[maxn]; 
int main() {
    int n = read(),k = read();      
    for(int i = 1;i <= n;++ i)  a[i] = read(); 
    std::sort(a + 1, a + n + 1); 
    int ans = 0; 
    for(int i = 1;i <= n;++ i) { 
        if(i < n) { if(a[i] <= k ) k -= a[i],ans ++;} 
        else if(i == n && a[i] == k) ans ++;     
        else break; 
    } 
    print(ans); 
    pc('\n'); 
}

AGC027 A - Candy Distribution Again

标签:define   cpp   break   inline   line   clu   ||   read   cst   

原文地址:https://www.cnblogs.com/sssy/p/9703791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!