九度OJ 1501-1510(10/10)

1501

#include <stdio.h>

#define LEN 100000

int N;
double data[LEN];
double max[LEN];
double min[LEN];

double Max(double a, double b){
    return (a > b) ? a : b;
}

double Min(double a, double b){
    return (a < b) ? a : b;
}

double MMS(){
    int i;
    double ans;
    max[0] = min[0] = data[0];
    ans = data[0];
    for (i = 1; i < N; ++i){
        max[i] = Max(Max(data[i], max[i-1]*data[i]), min[i-1]*data[i]);
        min[i] = Min(Min(data[i], max[i-1]*data[i]), min[i-1]*data[i]);
        ans = Max(ans, max[i]);
    }
    return ans;
}

int main(void){
    int i;
    double ans;
    while (scanf("%d", &N) != EOF){
        for (i = 0; i < N; ++i)
            scanf("%lf", &data[i]);
        ans = MMS();
        if (ans < 0)
            printf("-1\n");
        else if (ans - (int)ans <= 1e-5)
            printf("%lld\n", (int)ans);
        else
            printf("%.2lf\n", ans);
    }

    return 0;
}
/************************************************************** Problem: 1501 User: liangrx06 Language: C Result: Accepted Time:120 ms Memory:3256 kb ****************************************************************/

1502

#include <stdio.h>

#define N 500

int main(void)
{
    int n, m, k, i;
    int a[N];
    int high, low;

    scanf("%d", &n);
    while(n--)
    {
        scanf("%d%d", &m, &k);
        high = low = 0;
        for(i=0; i<m; i++)
        {
            scanf("%d", &a[i]);
            high += a[i];
            low = (a[i]>low) ? a[i] : low;
        }

        int count;
        while(low <= high)
        {
            count = 1;
            int mid = (low+high)>>1;
            int sum = 0;
            for (i=0; i<m; i++)
            {
                if (sum + a[i] <= mid)
                    sum += a[i];
                else
                {
                    count ++;
                    sum = a[i];
                }
            }
            if (count <= k)
                high = mid - 1;
            else
                low = mid + 1;
        }
        printf("%d\n", low);
    }

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

1503

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

#define N 1000

typedef struct node {
    int key;
    struct node *left;
    struct node *right;
} Node; 

Node *create()
{   
    int key;
    scanf("%d", &key);
    if (key == 0)
        return NULL;

    Node *p = (Node *)malloc(sizeof(Node));
    p->key = key;
    p->left = create();
    p->right = create();
    return p;
}   

void printTree(Node *head)
{
    if (head == NULL)
        return ;
    printf("%d ", head->key);
    printTree(head->left);
    printTree(head->right);
}   

Node *change(Node *head)
{       
    if (head == NULL)
        return NULL; 
    Node *ll = change(head->left);
    Node *rl = change(head->right);
    if (ll)
    {
        Node *lr = ll->left;
        lr->right = head;
        head->left = lr;
    }
    else
        ll = head;
    Node *rr = head;
    if (rl)
    {
        rr = rl->left;
        head->right = rl;
        rl->left = head;
    }
    ll->left = rr;
    rr->right = ll;
    return ll;
}

void printTwo(Node *head)
{
    if (head == NULL)
        return ;
    printf("%d ", head->key);
    Node *p = head->right;
    while (p != head)
    {
        printf("%d ", p->key);
        p = p->right;
    }
    printf("\n");
}

int main(void)
{
    Node *head;
    int t;

    scanf("%d\n", &t);
    while (t--)
    {
        head = create();
        //printTree(head);
        //printf("\n");

        head = change(head);
        printTwo(head);
    }   

    return 0;
}   
/************************************************************** Problem: 1503 User: liangrx06 Language: C Result: Accepted Time:70 ms Memory:1704 kb ****************************************************************/

1504

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

#define N 100

int main(void)
{
    int n, i, j;
    char s[N][10];
    char t1[20], t2[20];
    char *p[N];
    char sa[10*N];

    while (scanf("%d", &n) != EOF)
    {
        for (i=0; i<n; i++)
        {
            scanf("%s", s[i]);
            p[i] = s[i];
        }

        for (i=0; i<n-1; i++)
        {
            for (j=0; j<n-1-i; j++)
            {
                strcpy(t1, p[j]);
                strcat(t1, p[j+1]);
                strcpy(t2, p[j+1]);
                strcat(t2, p[j]);
                if (strcmp(t1, t2) > 0)
                {
                    char *tmp = p[j];
                    p[j] = p[j+1];
                    p[j+1] = tmp;
                }
            }
        }

        sa[0] = '\0';
        for (i=0; i<n; i++)
            strcat(sa, p[i]);
        printf("%s\n", sa);
    }

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

1505

/*该题判断有问题*/

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

int main()
{
    int m, n, i, j;
    int *list1, *list2;

    while(scanf("%d %d", &m, &n) != EOF) {
        list1 = (int *)malloc(sizeof(int)*m);
        list2 = (int *)malloc(sizeof(int)*n);

        i = 0;
        while(i < m)
            scanf("%d", &list1[i++]);
        j = 0;
        while(j < n)
            scanf("%d", &list2[j++]);
        i = j = 0;
        for((m > n) ? (i = m-n) : (j = n-m); i < m && list1[i] != list2[j]; ++i, ++j);
        if(i == m)
            puts("My God");
        else
            printf("%d\n", list1[i]);
        free(list1);
        free(list2);
    }
    return 0;
}
/************************************************************** Problem: 1505 User: liangrx06 Language: C Result: Accepted Time:70 ms Memory:912 kb ****************************************************************/

1506

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

int main()
{
 int n;
 while(scanf("%d", &n) != EOF)
 {
 printf("%d\n", ((int)(pow(n, 2))+n) >> 1);
 }
 return 0;
}
/**************************************************************
 Problem: 1506
 User: liangrx06
 Language: C
 Result: Accepted
 Time:20 ms
 Memory:912 kb
****************************************************************/

1507

#include <stdio.h>

int main()
{
    int m, n, i;
    int res;
    while(scanf("%d%d", &m, &n) != EOF)
    {
        res = 0;
        int up = 0;
        for (i=0; i<31; i++)
        {
            //printf("i=%d, m=%d\n", i, m);
            int cur = 1<<i;
            int mcur = m&cur;
            int ncur = n&cur;
            int count0 = 0;
            if (mcur == 0)
                count0++;
            if (ncur == 0)
                count0++;
            if (up == 0)
                count0++;
            //printf("i=%d, cur=%d, mcur=%d, ncur=%d, up=%d\n", i, cur, mcur, ncur, up);
            m &= ~cur;
            //printf("i=%d, m=%d\n", i, m);
            if (count0 % 2 == 0)
                m |= cur;
            if (count0 <= 1)
                up = cur<<1;
            else
                up = 0;
            //printf("i=%d, m=%d\n", i, m);
        }
        printf("%d\n", m);
    }
    return 0;
}
/**************************************************************
    Problem: 1507
    User: liangrx06
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:912 kb
****************************************************************/

1508

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

int main()
{
    int i, n;
    char s[100];
    while(scanf("%s", s) != EOF)
    {
        int len = strlen(s);
        int symbol = 1;
        n = 0;
        for (i=0; i<len; i++)
        {
            if ( i == 0 && s[i] == '-' )
            {
                symbol = -1;
                continue;
            }
            if (i == 0 && s[i] == '+')
                continue;
            if (s[i] >= '0' && s[i] <= '9')
            {
                n = n*10 + s[i]-48;
                continue;
            }
            printf("My God\n");
            break;
        }
        if (i == len)
            printf("%d\n", symbol*n);
    }
    return 0;
}
/************************************************************** Problem: 1508 User: liangrx06 Language: C Result: Accepted Time:0 ms Memory:912 kb ****************************************************************/

1509

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

#define N 1000

typedef struct node {
    int key;
    struct node *left;
    struct node *right;
    struct node *pre;
} Node;

Node *create()
{
    int key;
    scanf("%d", &key);
    if (key == 0)
        return NULL;

    Node *p = (Node *)malloc(sizeof(Node));
    p->key = key;
    p->left = create();
    if (p->left != NULL)
        p->left->pre = p;
    p->right = create();
    if (p->right != NULL)
        p->right->pre = p;
    return p;
}

void print(Node *head)
{
    if (head == NULL)
        return ;
    printf("%d ", head->key);
    print(head->left);
    print(head->right);
}

Node *search(Node *head, int key)
{
    if (head == NULL)
        return 0;
    if (head->key == key)
        return head;
    Node *p = search(head->left, key);
    if (p != NULL)
        return p;
    p = search(head->right, key);
    if (p != NULL)
        return p;
    return NULL;
}

int road(Node *p, int *r)
{
    if (p == NULL)
        return 0;
    int k = 0;
    while (p!= NULL)
    {
        r[k++] = p->key;
        p = p->pre;
    }
    return k;
}

int main(void)
{
    Node *head;
    int t, m1, m2;
    int k1, k2;
    int r1[N], r2[N];

    scanf("%d\n", &t);
    while (t--)
    {
        head = create();
        head->pre = NULL;
        //print(head);

        scanf("%d%d", &m1, &m2);
        Node *p1 = search(head, m1);
        Node *p2 = search(head, m2);
        k1 = road(p1, r1);
        k2 = road(p2, r2);
        k1 --;
        k2 --;
        int j1 = k1, j2 = k2;
        while (k1>=0 && k2>=0 && r1[k1] == r2[k2])
        { 
            j1 = k1;
            j2 = k2;
            k1 --;
            k2 --;
        }
        if (j1<0 || j2<0 || r1[j1] != r2[j2])
            printf("My God\n");
        else
            printf("%d\n", r1[j1]);
    }

    return 0;
}       
/**************************************************************
    Problem: 1509
    User: liangrx06
    Language: C
    Result: Accepted
    Time:130 ms
    Memory:6720 kb
****************************************************************/

1510

#include <stdio.h>
 
int main()
{
    int i, k;
    char s1[1000000], s2[1000000];
    while(gets(s1))
    {
        for (i=0, k=0; s1[i]; i++)
        {
            if (s1[i] == ' ')
            {
                s2[k++] = '%';
                s2[k++] = '2';
                s2[k++] = '0';
            }
            else
                s2[k++] = s1[i];
        }
        s2[k] = '\0';
        //printf("i=%d, k=%d\n", i, k);
        //for (i=0; s2[i]; i++)
        //    printf("%c", s2[i]);
        printf("%s\n", s2);
    }
    return 0;
}
/**************************************************************     Problem: 1510     User: liangrx06     Language: C     Result: Accepted     Time:10 ms     Memory:2792 kb ****************************************************************/

你可能感兴趣的:(ACM,九度OJ)