当前位置:   article > 正文

排版问题算法_排版算法

排版算法

排版问题算法的基本思路:将排版转化为一个二维char数组或者int数组,挖掘所给要求中存在的二维数组内部的数学、逻辑关系,并用代码表示出来。
在这里插入图片描述
这里采用的代码如下:

/*画图问题——叠框*/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

int main()
{
    int len;char B;char A;
    while(scanf("%d %c %c",&len,&B,&A)!=EOF){
        if(((len+1)/2)%2!=0){
            char tmp;
            tmp = B;B = A;A = tmp;
        }
        char ch[100][100]={' '};
        int hlen = (len+1)/2;
        for(int i=2;i<=len-1;i++){
            ch[1][i] = A;
        }
        for(int i=2;i<=len-1;i++){
            ch[len][i] = A;
        }
        for(int row=2;row<=hlen;row+=2){  //偶数行
            for(int i=1;i<=len;i++){
                ch[row][i] = B;
            }
            for(int i=1;i<=row/2;i++){
                ch[row][2*i-1] = A;
                ch[row][len-2*i+2] = A;
            }
        }
        if(hlen>=3){                        //奇数行
            for(int row=3;row<=hlen;row+=2){
                for(int i=1;i<=len;i++){
                    ch[row][i] = A;
                }
                for(int i=1;i<=(row-1)/2;i++){
                    ch[row][2*i] = B;
                    ch[row][len+1-2*i] = B;
                }
            }
        }
        for(int row=1;row<=hlen;row++){
            for(int col=1;col<=len;col++){
                cout<<ch[row][col];
            }
            cout<<endl;
        }
        for(int row=hlen+1;row<=len;row++){
            for(int col=1;col<=len;col++){
                cout<<ch[2*hlen-row][col];
            }
            cout<<endl;
        }
    }
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/755138
推荐阅读
相关标签
  

闽ICP备14008679号