zoukankan      html  css  js  c++  java
  • C++字符画圈

    样例输入

    11Xo

    6*E

    样例输出

    代码

    #include <cstdio>
    #define MAX 80 
    
    char buf[MAX][MAX];
    void mySwap(int& a, int& b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
    void drawLine(int x1, int y1, int x2, int y2, char ch)
    {
        if (x1 == x2) {
            // 画直线 
            if (y1 > y2) mySwap(y1, y2);
            for (int j = y1; j <= y2; ++j) {
                buf[x1][j] = ch;
            }
        } else {
            // 画竖线
            if (x1 > x2) mySwap(x1, x2);
            for (int i = x1; i <= x2; ++i) {
                buf[i][y1] = ch;
            }         
        }
    }
    void drawCircle(int si, int sj, int width, char ch)
    {
        drawLine(si, sj, si, sj + width - 1, ch);
        drawLine(si, sj + width - 1, si + width - 1, sj + width - 1, ch);
        drawLine(si, sj, si + width - 1, sj, ch);    
        drawLine(si + width - 1, sj, si + width - 1, si + width - 1, ch);    
    }
    int main()
    {
        int width;
        char a, b; 
        
        while (scanf("%d", &width) != EOF) {
            scanf("%c%c", &b, &a);
            int x = 0, y = 0, cur = a;
            
            int tmpWidth = width;
            while (tmpWidth > 0) {
                drawCircle(x++, y++, tmpWidth, cur);
                tmpWidth -= 2;
                cur = (cur == a ? b : a);
            }
            
            buf[0][0] = buf[width - 1][width - 1] = buf[0][width - 1] = buf[width - 1][0] = ' ';
            for (int i = 0; i <= width - 1; ++i) {
                for (int j = 0; j <= width - 1; ++j) {
                    printf("%c", buf[i][j]);
                }
                printf("
    ");
            }
        }
        
        return 0;
    }
  • 相关阅读:
    Mercury产品介绍
    操纵txt文本文件
    MOSS开发辅助小工具
    Notes 8/8.5 超慢解决之道的最佳实践
    实战OO设计——OO设计原则
    SQL Server XML 拆分示例
    认识IL
    javascript 面向对象特性与编程实现
    MTV
    C#轻松仿造Vista风格窗体_cici 自娱自乐
  • 原文地址:https://www.cnblogs.com/xkxf/p/14455945.html
Copyright © 2011-2022 走看看