1. 程式人生 > >AGC027 A - Candy Distribution Again

AGC027 A - Candy Distribution Again

string etc 題目 contest cstring getchar \n clu while

目錄

  • 題目鏈接
  • 題解
  • 代碼


題目鏈接

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