在中文Windows环境下,控制台窗口中也可以用特殊符号拼出漂亮的表格来。
比如:
┌─┬─┐
│    │   │
        ├─┼─┤
 │    │   |
└─┴─┘
其实,它是由如下的符号拼接的:
左上 = ┌
上 =  ┬
右上 =  ┐
左 =  ├
中心 =  ┼
右 =  ┤
左下=  └
下 =  ┴
右下 =  ┘
垂直 =  │
水平 =   ─
本题目要求编写一个程序,根据用户输入的行、列数画出相应的表格来。


例如用户输入:
3 2
则程序输出:
┌─┬─┐
│    │   │
├─┼─┤
│    │    │
├─┼─┤
│    │   │
└─┴─┘


用户输入:
2 3
则程序输出:
┌─┬─┬─┐
 │   │   │   │
├─┼─┼─┤
 │  │   │    │
└─┴─┴─┘

#include <iostream>

using namespace std;
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=0; i<=2*n; i++) //行的遍历
        {
            if(i==0)//第一行
            {
                for(int j=0; j<=2*m; j++)
                {
                    if(j==0)cout<<"┌";//第一列
                    else if(j%2==1)cout<<"─";//奇数列
                    else if(j==2*m)cout<<"┐";//最后一列
                    else cout<<"┬"; //偶数列
                }
                cout<<endl;
            }
            else if(i%2==1)//奇数行
            {
                for(int j=0; j<=m; j++)
                {
                    if(j==m)cout<<"│"<<endl;
                    else cout<<"│"<<"  ";
                }
            }
            else if(i==2*n)//最后一行
            {
                for(int j=0; j<=2*m; j++)
                {
                    if(j==0)cout<<"└";
                    else if(j%2==1)cout<<"─";
                    else if(j==2*m)cout<<"┘";
                    else cout<<"┴";
                }
                cout<<endl;
            }
            else//偶数行
            {
                for(int j=0; j<=2*m; j++)
                {
                    if(j==0)cout<<"├";
                    else if(j%2==1)cout<<"─";
                    else if(j==2*m)cout<<"┤";
                    else cout<<"┼";
                }
                cout<<endl;
            }
        }
    }
    return 0;
}
posted on 2015-04-08 16:25  星斗万千  阅读(237)  评论(0编辑  收藏  举报