Jack's Blog

流淌的心,怎能阻拦,吹来的风,又怎能阻挡。

acm-2

Jacob posted @ Sep 12, 2017 07:41:48 PM in C语言 with tags acm , 481 阅读

吃糖果问题

复杂方法:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
main()
{
    int i,t,j,N,f[50]={0};
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf(" %d",&N);
        for(j=0;j<N;j++)
        {
            scanf(" %d",&f[j]);
        }
        int k;
        for(j=0;j<N;j++)
        {
            for(k=0;k<N;k++)
            {
                if(f[i]<f[i+1])
                {
                    int middle = f[i];
                    f[i]=f[i+1];
                    f[i+1]=middle;
                }
            }
        }
        int flag=f[0];
        for(k=0;k<N;k++)
        {
            flag=flag-f[k+1];
        }
        if(flag>=2)
            printf("No\n");
        else
            printf("Yes\n");
    }

}

简便算法:

# include<stdio.h>
int main()
{
    long T, N;
    double candy, max,sum;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d", &N);
        max = sum = 0;
        while(N--)
        {
            scanf("%lf", &candy);
            max = max > candy ? max : candy;
            sum += candy;
        }
    sum -= max;
    if(max <= sum + 1)
        printf("Yes\n");
    else
        printf("No\n");
 }
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter