九度OJ 1511-1520(10/10)

1511

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

struct node {
    int key;
    struct node *next;
};

struct node *insert(struct node *head, int key)
{
    if (head == NULL)
    {
        head = (struct node *)malloc(sizeof(struct node));
        head->key = key;
        head->next = NULL;
        return head;
    }
    struct node *p = (struct node *)malloc(sizeof(struct node));
    p->key = key;
    p->next = head;
    return p;
}

void print(struct node *head)
{
    if (head == NULL)
        return;
    printf("%d\n", head->key);
    print(head->next);
}

int main()
{
    int n;
    struct node *head = NULL;
    while(scanf("%d", &n) != EOF)
    {
        if (n == -1)
            break;
        head = insert(head, n);
    }

    print(head);

    return 0;
}
/************************************************************** Problem: 1511 User: liangrx06 Language: C Result: Accepted Time:90 ms Memory:3948 kb ****************************************************************/

1512

#include <stdio.h>

#define N 100000

int push(int *s, int top, int x)
{
    s[top] = x;
    top ++;
    return top;
}

int pop(int *s, int top, int *x)
{
    top --;
    *x = s[top];
    return top;
}

int main(void)
{
    int n, i, x;
    int s1[N], s2[N], top1, top2;
    char op[10];

    while (scanf("%d", &n) != EOF)
    {
        top1 = top2 = 0;
        for(i=0; i<n; i++)
        {
            scanf("%s", op);
            if (op[1] == 'U')
            {
                scanf("%d", &x);
                top1 = push(s1, top1, x);
            }
            else
            {
                if (top2 == 0)
                {
                    if (top1 == 0 && top2 == 0)
                        printf("-1\n");
                    else
                    {
                        while(top1 != 0)
                        {
                            top1 = pop(s1, top1, &x);
                            top2 = push(s2, top2, x);
                        }
                        top2 = pop(s2, top2, &x);
                        printf("%d\n", x);
                    }
                }
                else
                {

                    top2 = pop(s2, top2, &x);
                    printf("%d\n", x);
                }
            }
        }
    }

    return 0;
}
/**************************************************************
    Problem: 1512
    User: liangrx06
    Language: C
    Result: Accepted
    Time:60 ms
    Memory:1620 kb
****************************************************************/

1513

#include <stdio.h>

int main()
{
 int i, j, n;
 int a;
 while(scanf("%d", &n) != EOF)
 {
 for (i=0; i<n; i++)
 {
 scanf("%d", &a);
 int count = 0;
 for (j=0; j<32; j++)
 {
 if ( (a & (1<<j)) != 0 )
 count += 1;
 }
 printf("%d\n", count);
 }
 }
 return 0;
}
/**************************************************************
 Problem: 1513
 User: liangrx06
 Language: C
 Result: Accepted
 Time:80 ms
 Memory:912 kb
****************************************************************/

1514

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

int main()
{
 int i, t, n;
 double a;
 while(scanf("%d", &t) != EOF)
 {
 for (i=0; i<t; i++)
 {
 scanf("%lf%d", &a, &n);
 if (a==0.000 && n<=0 )
 {
 printf("INF\n");
 continue;
 }
 double res = pow(a, n);
 printf("%.2ef\n", res);
 }
 }
 return 0;
}
/**************************************************************
 Problem: 1514
 User: liangrx06
 Language: C
 Result: Accepted
 Time:80 ms
 Memory:1004 kb
****************************************************************/

1515

#include <stdio.h>

int main()
{
 int i, n;
 int a;
 while(scanf("%d", &n) != EOF)
 {
 a = 1;
 for (i=1; i<=n; i++)
 a *= 10;
 for (i=1; i<a; i++)
 printf("%d\n", i);
 }
 return 0;
}
/**************************************************************
 Problem: 1515
 User: liangrx06
 Language: C
 Result: Accepted
 Time:40 ms
 Memory:912 kb
****************************************************************/

1516

#include <stdio.h>

#define N 100000

int main(void)
{
 int n, i, j, k, m;
 int a[N], b[N], c[N];

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

 j = 0;
 k = 0;
 for(i=0; i<n; i++)
 {
 if (a[i] % 2 == 1)
 b[j++] = a[i];
 else
 c[k++] = a[i];
 }
 i = 0;
 m = 0;
 while (m < j)
 a[i++] = b[m++];
 m = 0;
 while (m < k)
 a[i++] = c[m++];

 for (i=0; i<n-1; i++)
 printf("%d ", a[i]);
 printf("%d\n", a[i]);
 }

 return 0;
}
/**************************************************************
 Problem: 1516
 User: liangrx06
 Language: C
 Result: Accepted
 Time:80 ms
 Memory:2012 kb
****************************************************************/

1517

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

struct node {
    int key;
    struct node *next;
};

struct node *insert(struct node *head, int key)
{
    if (head == NULL)
    {
        head = (struct node *)malloc(sizeof(struct node));
        head->key = key;
        head->next = NULL;
        return head;
    }
    struct node *p = (struct node *)malloc(sizeof(struct node));
    p->key = key;
    p->next = head;
    return p;
}

void print(struct node *head, int k)
{
    if (head == NULL)
        return;
    while (--k)
        head = head->next;
    printf("%d\n", head->key);
}

int main()
{
    int n, k;
    int key;
    struct node *head = NULL;
    while(scanf("%d%d", &n, &k) != EOF)
    {
        for (int i=0; i<n; i++)
        {
            scanf("%d", &key);
            head = insert(head, key);
        }
        if (k == 0 || k > n)
        {
            printf("NULL\n");
            continue;
        }
        print(head, k);
    }

    return 0;
}
/************************************************************** Problem: 1517 User: liangrx06 Language: C Result: Accepted Time:100 ms Memory:2496 kb ****************************************************************/

1518

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

struct node {
    int key;
    struct node *next;
};

struct node *insert(struct node *head, int key)
{
    if (head == NULL)
    {
        head = (struct node *)malloc(sizeof(struct node));
        head->key = key;
        head->next = NULL;
        return head;
    }
    struct node *p = (struct node *)malloc(sizeof(struct node));
    p->key = key;
    p->next = head;
    return p;
}

void print(struct node *head)
{
    if (head == NULL)
        return;
    printf("%d", head->key);
    if (head->next != NULL)
        printf(" ");
    else
        printf("\n");
    print(head->next);
}

int main()
{
    int n;
    int a;
    struct node *head = NULL;
    while(scanf("%d", &n) != EOF)
    {
        if (n == 0)
        {
            printf("NULL\n");
            continue;
        }
        head = NULL;
        for (int i=0; i<n; i++)
        {
            scanf("%d", &a);
            head = insert(head, a);
        }
        print(head);
    }

    return 0;
}
/************************************************************** Problem: 1518 User: liangrx06 Language: C Result: Accepted Time:200 ms Memory:2364 kb ****************************************************************/

1519

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

struct node {
    int key;
    struct node *next;
};

struct node *add(struct node *head, int key)
{
    struct node *p = (struct node *)malloc(sizeof(struct node));
    p->key = key;
    p->next = head;
    return p;
}

void print(struct node *head)
{
    if (head == NULL)
        return;
    printf("%d", head->key);
    if (head->next != NULL)
        printf(" ");
    else
        printf("\n");
    print(head->next);
}

int main()
{
    int n, m, i, a;
    struct node *hm, *hn, *head;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        if (n == 0 && m == 0)
        {
            printf("NULL\n");
            continue;
        }
        hm = hn = head = NULL;
        for (i=0; i<n; i++)
        {
            scanf("%d", &a);
            hn = add(hn, a);
        }
        for (i=0; i<m; i++)
        {
            scanf("%d", &a);
            hm = add(hm, a);
        }
        while (hn != NULL || hm != NULL)
        {
            if (hn == NULL)
            {
                head = add(head, hm->key);
                hm = hm->next;
            }
            else if (hm == NULL)
            {
                head = add(head, hn->key);
                hn = hn->next;
            }
            else if (hn->key < hm->key)
            {
                head = add(head, hm->key);
                hm = hm->next;
            }
            else
            {
                head = add(head, hn->key);
                hn = hn->next;
            }
        }
        print(head);
    }

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

1520

#include <stdio.h>
 
#define N 1000
 
void prase(int a[N+1][3], int n)
{
    int i, j, k;
    for(i=1; i<=n; i++)
        scanf("%d", &a[i][0]);
    for(i=1; i<=n; i++)
    {
        scanf("%d", &k);
        for (j=1; j<=k; j++)
            scanf("%d", &a[i][j]);
        for (j=k+1; j<=2; j++)
            a[i][j] = 0;
    }
}
 
void printArray(int a[N+1][3], int n)
{
    int i, j;
    for (j=0; j<3; j++)
    {
        for (i=1; i<=n; i++)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
}
 
int checkSame(int a[N+1][3], int i, int b[N+1][3], int j)
{
    if (j == 0)
        return 1;
    if (i == 0)
        return 0;
    if( a[i][0] != b[j][0])
        return 0;
    if (!checkSame(a, a[i][1], b, b[j][1]))
        return 0;
    if (!checkSame(a, a[i][2], b, b[j][2]))
        return 0;
    return 1;
}
 
int isSon(int a[N+1][3], int i, int b[N+1][3])
{
    if (i == 0)
        return 0;
    if( a[i][0] == b[1][0] && checkSame(a, i, b, 1))
        return 1;
    if (isSon(a, a[i][1], b))
        return 1;
    if (isSon(a, a[i][2], b))
        return 1;
    return 0;
}
 
int main(void)
{
    int n, m;
    int a[N+1][3], b[N+1][3];
 
    while (scanf("%d%d", &n, &m) != EOF)
    {
        prase(a, n);
        prase(b, m);
        //printArray(a, n);
        //printArray(b, m);
        if (n == 0 || m == 0)
        {
            printf("NO\n");
            continue;
        }
        int res = isSon(a, 1, b);
        if (res)
            printf("YES\n");
        else
            printf("NO\n");
    }
 
    return 0;
}
/**************************************************************     Problem: 1520     User: liangrx06     Language: C     Result: Accepted     Time:10 ms     Memory:912 kb ****************************************************************/

你可能感兴趣的:(九度OJ 1511-1520(10/10))