九度OJ 1431-1440(10/10)

1431

#include <stdio.h>
#include <stdlib.h>

#define N 1000000

int cmp(const void *a, const void *b)
{
    return *(int *)b - *(int *)a;
}

int main()
{
    int i, n, m;
    int a[N];
    while(scanf("%d%d", &n, &m) != EOF)
    {
        for (i=0; i<n; i++)
            scanf("%d", &a[i]);
        qsort(a, n, sizeof(a[0]), cmp);
        if (m > n)
            m = n;
        for (i=0; i<m-1; i++)
            printf("%d ", a[i]);
        printf("%d\n", a[i]);
    }
    return 0;
}
/**************************************************************
    Problem: 1431
    User: liangrx06
    Language: C
    Result: Accepted
    Time:490 ms
    Memory:8656 kb
****************************************************************/

1432

#include <stdio.h>
#include <stdlib.h>

#define N 80

int main()
{
    int n;
    char s[N][N+1];
    char a, b;
    int i, j;
    int count = 0;
    while(scanf("%d %c %c", &n, &a, &b) != EOF)
    {
        int m = n/2;
        int ii;
        for (i=0; i<n; i++)
        {
            for (j=0; j<n; j++)
            {
                ii = abs(i-m)>abs(j-m) ? abs(i-m) : abs(j-m);
                if (ii % 2 == 0)
                    s[i][j] = a;
                else
                    s[i][j] = b;
            }
            s[i][n] = '\0';
        }
        s[0][0] = s[0][n-1] = s[n-1][0] = s[n-1][n-1] = ' ';
        if (n == 1)
            s[0][0] = a;
        if (count != 0)
            printf("\n");
        count ++;
        for (i=0; i<n; i++)
            printf("%s\n", s[i]);
    }
    return 0;
}
/**************************************************************
    Problem: 1432
    User: liangrx06
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:912 kb
****************************************************************/

1433

#include <stdio.h>
#include <stdlib.h>

#define N 1000

typedef struct node {
    int j;
    int f;
    double w;
} Room;

int cmp(const void *a, const void *b)
{
    return (((Room *)a)->w > ((Room *)b)->w) ? -1 : 1;
}

int main(void)
{
    int m, n, i;
    Room r[N];
    double max;

    while (scanf("%d%d", &m, &n) != EOF)
    {
        if (m == -1 && n == -1)
            break;

        for(i=0; i<n; i++)
        {
            scanf("%d%d", &r[i].j, &r[i].f);
            r[i].w = (double)(r[i].j)/r[i].f;
        }
        qsort(r, n, sizeof(r[0]), cmp);
        //for (i=0; i<n; i++)
        //  printf("%.3lf ", r[i].w);
        //printf("\n");

        max = 0;
        for (i=0; i<n; i++)
        {
            if (m >= r[i].f)
            {
                m -= r[i].f;
                max += r[i].j;
            }
            else
            {
                max += m*r[i].w;
                break;
            }
        }

        printf("%.3lf\n", max);
    }

    return 0;
}
/**************************************************************
    Problem: 1433
    User: liangrx06
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:912 kb
****************************************************************/

1434

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N 100
#define M 1000

typedef struct node {
    int s;
    int e;
} TV;

int cmp(const void *a, const void *b)
{
    return ((TV *)a)->e - ((TV *)b)->e;
}

int main(void)
{
    int n, i, j;
    TV b[N];
    int m[M+1];
    int tmax;

    while (scanf("%d", &n) != EOF && n)
    {
        tmax = 0;
        for(i=0; i<n; i++)
        {
            scanf("%d%d", &b[i].s, &b[i].e);
            tmax = (b[i].e > tmax) ? b[i].e : tmax;
        }
        qsort(b, n, sizeof(b[0]), cmp);

        memset(m, 0, sizeof(m));
        for(i=0; i<n; i++)
        {
            int tmp = m[b[i].s]+1;
            j = b[i].e;
            m[j] = (tmp > m[j]) ? tmp : m[j];
            for (j=b[i].e+1; j<=tmax; j++)
                m[j] = m[j-1];
        }

        printf("%d\n", m[tmax]);
    }

    return 0;
}
/**************************************************************
    Problem: 1434
    User: liangrx06
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:912 kb
****************************************************************/

1435

#include <stdio.h>
#include <stdlib.h>

int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

int main()
{
    int i, c, n, v, w;
    int p[100];
    scanf("%d", &c);
    while(c--)
    {
        scanf("%d %d %d", &n, &v, &w);
        for (i=0; i<n; i++)
            scanf("%d", &p[i]);
        qsort(p, n, sizeof(p[0]), cmp);
        double sum = 0;
        for (i=0; i<n; i++)
        {
            double pnew = (sum+p[i])/(i+1);
            if (pnew > w && pnew-w>1e-8)
                break;
            sum += p[i];
        }
        if (i == 0)
            printf("0 0.00\n");
        else
            printf("%d %.2lf\n", i*v, sum/i/100);
    }
    return 0;
}
/**************************************************************
    Problem: 1435
    User: liangrx06
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:912 kb
****************************************************************/

1436

#include <stdio.h>
#include <stdlib.h>

int cmp(const void *a, const void *b)
{
    return *(int *)b - *(int *)a;
}

int main()
{
    int i, n, w;
    int p[600];
    while(scanf("%d%d", &w, &n) != EOF)
    {
        for (i=0; i<n; i++)
            scanf("%d", &p[i]);
        qsort(p, n, sizeof(p[0]), cmp);
        int sum = 0;
        for (i=0; i<n; i++)
        {
            sum += p[i];
            if (sum >= w)
                break;
        }
        if (i == n)
            printf("impossible\n");
        else
            printf("%d\n", i+1);
    }
    return 0;
}
/**************************************************************
    Problem: 1436
    User: liangrx06
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:912 kb
****************************************************************/

1437

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define N 500

typedef struct node {
    double Pi;
    double Di;
} Station;

int cmp(const void *a, const void *b)
{
    return (((Station *)a)->Di > ((Station *)b)->Di) ? 1 : -1;
}

void printStation(Station s[], int n)
{
    for (int i=0; i<n; i++)
        printf("%.2lf %.2lf\n", s[i].Pi, s[i].Di);
}

int main(void)
{
    int n, i, j;
    double Cmax, D, Davg, Dmax;
    Station s[N+1];
    double curC, minPrice;

    while (scanf("%lf%lf%lf%d", &Cmax, &D, &Davg, &n) != EOF)
    {
        for(i=0; i<n; i++)
            scanf("%lf%lf", &s[i].Pi, &s[i].Di);
        s[n].Pi = 1000000;
        s[n].Di = D;
        qsort(s, n, sizeof(s[0]), cmp);
        //printStation(s, n+1);

        if (s[0].Di > 0){
            printf("The maximum travel distance = 0.00\n");
            continue;
        }

        Dmax = Cmax*Davg;
        minPrice = 0;
        curC = 0;
        for (i=0; i<n; i++)
        {
            if (i>0)
                curC -= (s[i].Di - s[i-1].Di)/Davg;
            j = i+1;
            while (j<n && s[j].Pi >= s[i].Pi)
                j++;
            if (s[j].Di - s[i].Di > Dmax)
            {
                minPrice += (Cmax-curC)*s[i].Pi;
                curC = Cmax;
            }
            else
            {
                double needC = (s[j].Di-s[i].Di)/Davg;
                if (needC > curC)
                {
                    minPrice += (needC-curC) * s[i].Pi;
                    curC = needC;
                }
            }
            if (s[i+1].Di - s[i].Di > Dmax)
            {
                printf("The maximum travel distance = %.2lf\n", s[i].Di+Dmax);
                break;
            }
        }

        if (i == n)
            printf("%.2lf\n", minPrice);
    }

    return 0;
}
/**************************************************************
    Problem: 1437
    User: liangrx06
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:912 kb
****************************************************************/

1438

#include <stdio.h>

int gongyueshu(int a, int b)
{
    if (b == 0)
        return a;
    return gongyueshu(b, a%b);
}


int main(void)
{
    int a, b;

    while (scanf("%d%d", &a, &b) != EOF)
    {
        if (a <= b)
        {
            int tmp = a;
            a = b;
            b = tmp;
        }
        int gys = gongyueshu(a, b);
        printf("%d\n", gys * a/gys * b/gys);
    }

    return 0;
}
/**************************************************************
    Problem: 1438
    User: liangrx06
    Language: C
    Result: Accepted
    Time:0 ms
    Memory:912 kb
****************************************************************/

1439

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define M 100000
#define N 100000

int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

void swap(int *x, int *y)
{
    int tmp;
    tmp = *x;
    *x = *y;
    *y = tmp;
}

int GCD(int x, int y)
{
    if (y == 0)
        return x;
    return GCD(y, x%y);
}

int LCM(int x, int y)
{
    if (x == y)
        return x;
    if (x < y)
        swap(&x, &y);
    int gcd = GCD(x, y);
    return x/gcd*y;
}

int main()
{
    int i, j, t, n;
    int a[N];
    int lcm;

    scanf("%d\n", &t);
    for (i=0; i<t; i++)
    {
        scanf("%d", &n);
        for (j=0; j<n; j++)
            scanf("%d", &a[j]);

        qsort(a, n, sizeof(n), cmp);
        //for (j=0; j<n-1; j++)
        //  printf("%d ", a[j]);
        //printf("%d\n", a[j]);

        lcm = 1;
        for (j=0; j<n; j++)
        {
            lcm = LCM(lcm, a[j]);
            //printf("%d\n", lcm);
        }
        printf("%d\n", lcm);
    }
    return 0;
}
/**************************************************************
    Problem: 1439
    User: liangrx06
    Language: C
    Result: Accepted
    Time:0 ms
    Memory:1232 kb
****************************************************************/

1440

#include <stdio.h>
#include <math.h>
 
int isprime(int n) //n>=3, odd
{
    int i;
    for (i=3; i<=sqrt(n); i+=2)
    {
        if (n%i == 0)
            return 0;
    }
    return 1;
}
 
int main()
{
    int i, n;
    int count;
    while(scanf("%d", &n) != EOF && n)
    {
        if (n == 4)
        {
            printf("1\n");
            continue;
        }
        count = 0;
        for (i=3; i<=n/2; i+=2)
        {
            if (isprime(i) && isprime(n-i))
                count ++;
        }
        printf("%d\n", count);
    }
    return 0;
}
/**************************************************************
    Problem: 1440
    User: liangrx06
    Language: C
    Result: Accepted
    Time:20 ms
    Memory:928 kb
****************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值