ACM준비/기타

Stacks of Flapjacks

조규현15 2015. 1. 8. 10:28
반응형

https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=56 

 

Online Judge

Our Patreons Diamond Sponsors Steven & Felix Halim Reinardus Pradhitya  Gold Sponsors --- YOUR NAME HERE ----  Silver Sponsors --- YOUR NAME HERE ----   Bronze Sponsors Christianto Handojo Krzysztof Adamek Fatima Broom Amal Augustine Contribute Bitcoin

onlinejudge.org

문제풀이

>팬케이크를 올림차순으로 재 정렬하는게 목표

>문제에서 재공하는 뒤집기의 의미를 이해하고 구현하는게 핵심

 

배울점

>뒤집기를 구현할 때 최대값의 케이크를 맨 처음에 옮기고 뒤집는게 알고리즘의 핵심

 

소스>성공

#include <stdio.h>

unsigned int reverse(unsigned int n)
{

    unsigned int n2 = 0;

    while (n)
    {
        n2 = (n2 * 10) + (n % 10);
        n /= 10;
    }
    return n2;
}

int main()
{

    int Case, count;
    unsigned int n, n2;

    scanf("%d", &Case);
    while (Case--)
    {

        scanf("%lu", &n);
        count = 0;

        while (1)
        {

            n2 = reverse(n);

            if (n2 == n)
                break;

            n += n2;
            count++;
        }

        printf("%d %lu\n", count, n);
    }
}

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

int big();
void flip();

int main()
{

    char string[5000];
    int num[100];
    int p, c, biggest;
    int i;
    char *next;

    while (!feof(stdin))
    {
        p = 0;
        if (!gets(string))
            break;
        printf("%s\n", string);

        next = strtok(string, " ");
        while (next != NULL)
        {
            num[p++] = atoi(next);
            next = strtok(NULL, " ");
        }
        c = p;

        while (c > 0)
        {
            biggest = big(num, c);

            if (biggest == c - 1)
                c--;
            else if (biggest == 0)
            {
                printf("%d ", p - (c - 1));
                flip(num, c - 1);
            }
            else
            {
                printf("%d ", p - biggest);
                flip(num, biggest);
            }
        }
        printf("0\n");
    }
    return 1;
}

int big(int *arr, int p)
{
    int i, big;

    big = 0;
    for (i = 0; i < p; i++)
        if (arr[i] > arr[big])
            big = i;

    return big;
}

void flip(int *arr, int num)
{

    int count, i, temp;
    count = (num + 1) / 2;

    for (i = 0; i < count; i++)
    {
        temp = arr[num - i];
        arr[num - i] = arr[i];
        arr[i] = temp;
    }
}​

 

 

반응형

'ACM준비 > 기타' 카테고리의 다른 글

Bicoloring  (0) 2015.01.08
Complete Tree Labeling  (0) 2015.01.08
Self-describing Sequence  (0) 2015.01.08
Reverse and Add  (0) 2015.01.08
TheArcheologist'sDilemma  (0) 2015.01.08