• hihocoder1257(构造)(2015北京ACM/ICPC)


    题意:

    给你n条蛇,a[i]的长度为i,要求组成一个矩形。奇数蛇可折叠奇数次,偶数蛇折叠偶数次,然后按蛇的次序输出

    (即一条蛇的输出只能是一个方向的)

    2 3

    1 2

    1 3 2 3

    1 1 2 1 2 2

    2 5

    1 4

    1 5 2 5

    1 1 2 1 2 2

    1 2 1 3 2 3 2 4

    3 5

    3 4

    1 4 1 5

    2 4 2 5 3 5

    2 2 2 3 3 3 3 2

    3 1 2 1 1 1 1 2 1 3

    思路:

    构造的话一般都是找规律,通过前面的推出后面的:

    首先我们可以发现矩形的长宽是取决于n

    1: 1 1                          2:1 2

    3: 2 3                          4:2 5

    5: 3 4                          6:3 7

    然后是找矩形的关系

    我们可以发现偶数矩形可以由它的前一个组成,即在后面加上

    3: 1 3 3              4:1 3 3 4 4

       2 2 3                2 2 3 4 4

    然后看奇数矩形,通过长宽不停的从前找规律可以发现f[n]与f[n-3]有一定的关系

    1 3 3 4 4                    1 3 3 4 4 5 7

    2 2 3 4 4         -->        2 2 3 4 4 5 7

                                 6 6 6 5 5 5 7

                                 6 6 6 7 7 7 7


    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    
    void fin(int cur)
    {
        if(cur == 1)
        {
            printf("1 1
    ");
            return ;
        }
        if(cur == 2)
        {
            printf("1 1
    ");
            printf("1 2 1 3
    ");
            return;
        }
        if(cur == 3)
        {
            printf("2 1
    ");
            printf("1 1 1 2
    ");
            printf("1 3 2 3 2 2
    ");
            return;
        }
        int tx = (cur+1)/2;
        int ty = (cur%2)? tx*2-1:tx*2+1;
    
        if(cur % 2 == 0)
        {
            fin(cur-1);
            for(int i = 1; i <= cur/2; i ++)
                printf("%d %d ",i,ty-1);
            for(int i = cur/2; i >= 1; i--)
                printf("%d %d ",i,ty);
            printf("
    ");
            return ;
        }
        else
        {
            fin(cur-3);
            for(int i = 1; i <= (cur-2)/2; i++)
                printf("%d %d ",i,ty-1);
            for(int i = 1; i <= (cur-2)/2+1; i++)
                printf("%d %d ",tx-1,ty-i);
            printf("
    ");
    
            for(int i = 1; i <= (cur-1)/2; i++)
                printf("%d %d ",tx-1,i);
            for(int i = 0; i <= (cur-1)/2-1; i++)
                printf("%d %d ",tx,(cur-1)/2-i);
            printf("
    ");
    
            for(int i = 0; i < cur/2+1; i++)
                printf("%d %d ",tx,ty-cur/2+i);
            for(int i = 0; i < cur/2; i++)
                printf("%d %d ",tx-i-1,ty);
            printf("
    ");
            return ;
        }
    }
    
    int main()
    {
        int n;
        while(scanf("%d",&n) != EOF)
        {
            printf("%d %d
    ",(n+1)/2,(n%2)? (n+1)/2*2-1:(n+1)/2*2+1);
            fin(n);
        }
        return 0;
    }
    

      

  • 相关阅读:
    求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
    3,具体例子
    二十八.享元模式
    三十一.设计模式总结创建型模式
    1,学习LinQ
    软件公司需要具备什么能力的大学毕业生?
    二十九.解释器模式
    三十三.设计模式总结行为型模式
    三十二.设计模式总结结构型模式
    三十.访问者模式
  • 原文地址:https://www.cnblogs.com/Przz/p/5409673.html
Copyright © 2020-2023  润新知