题目分析:
Problem Description
Little Q likes solving math problems very much. Unluckily, however, he does not have good spatial ability. Everytime he meets a 3D geometry problem, he will struggle to draw a picture.
Now he meets a 3D geometry problem again. This time, he doesn't want to struggle any more. As a result, he turns to you for help.
Given a cube with length a , width b and height c , please write a program to display the cube.
Input
The first line of the input contains an integer T(1≤T≤50) , denoting the number of test cases.
In each test case, there are 3 integers a,b,c(1≤a,b,c≤20) , denoting the size of the cube.
Output
For each test case, print several lines to display the cube. See the sample output for details.
这个题目理解起来挺简单,就是输入一个数字n控制循环,接着输入n组数据,每组数据由三个数字组成,然后“画”出由这三个数字组成的长方体。
解题:
然后我们开始正式解析这个长方体的结构。
由于' . '出现的位置多而且控制起来很麻烦,所以我选择用' . '初始化整个矩阵:
#define rep(i,a,n) for (int i = a; i < n; ++i)
#define MAXN 82
char cfx[MAXN][MAXN];
rep(i, 0, MAXN)
rep(j, 0, MAXN)
cfx[i][j] = '.';
这个长方体由三个面来表现:正面是一个长方形,由字符' . ' , ' + ' , ' - ' , ' | ' 组成。我们用两个for循环控制这个长方形的行和列,用最常见的i控制行,j控制列。(注意i 和 j是从零开始计数)
当i为偶数,j为偶数时是' + ',i为偶数,j为奇数时是' - ',i为奇数,j为偶数时是' | ',i和j都为奇数时是' . '。
正面i和j的循环范围相比很简单就能看出来。
int a, b, c;
cin >> a >> b >> c;
for (int i = 2 * b; i < 2 * b + 2 * c+1; ++i)
for (int j = 0; j < 2 * a + 1; ++j)
{
if (i % 2 != 0 && j % 2 == 0)
cfx[i][j] = '|';
else if (i % 2 == 0 && j % 2 == 0)
cfx[i][j] = '+';
else if (i % 2 == 0 && j & 2 != 0)
cfx[i][j] = '-';
}
接着看长方体的顶面。
我选择仍然用两个for循环,但是与正面不同的是每次在行循环的时候需要控制两个变量,从而影响j。
for(int k = 2 * b,i = 0 ; i < 2*b;++i,--k)
for (int j = k; j < 2 * a + k ;j++)
{
if (i % 2 == 0 && j % 2 == 0)
cfx[i][j] = '+';
else if (i % 2 == 0 && j % 2 != 0)
cfx[i][j] = '-';
else if (i % 2 != 0 && j % 2 != 0)
cfx[i][j] = '/';
}
在“画”侧面的时候,先让列循环,再让行循环,与顶面同理,在列循环的时候控制两个变量,从而影响行i。
for(int k = 2 * b, j = 2 * a + 2 * b; j >= 2 * a + 1; --j,--k)
for (int i = 2 * b - k ; i < 2 * b + 2 * c - k+1; ++i)
{
if (i % 2 != 0 && j % 2 != 0)
cfx[i][j] = '/';
else if (i % 2 == 0 && j % 2 == 0)
cfx[i][j] = '+';
else if (i % 2 != 0 && j % 2 == 0)
cfx[i][j] = '|';
}
其中字符的关系也和正面一样可以找出来,而i,j的循环范围稍微有点麻烦,在找规律的同时要时刻提醒自己i,j是从0开始的。
完整代码:
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<cstring>
#include<cstdlib>
typedef long long ll;
#define rep(i,a,n) for (int i = a; i < n; ++i)
#define per(i,a,n) for (int i = n-1; i >= a; --i)
#define SZ(x) ((int)x.size())
using namespace std;
//head
#define MAXN 82
int main()
{
char cfx[MAXN][MAXN];
int _;
cin >> _;
while (_--)
{
rep(i, 0, MAXN)
rep(j, 0, MAXN)
cfx[i][j] = '.';
int a, b, c;
cin >> a >> b >> c;
for (int i = 2 * b; i < 2 * b + 2 * c+1; ++i)
for (int j = 0; j < 2 * a + 1; ++j)
{
if (i % 2 != 0 && j % 2 == 0)
cfx[i][j] = '|';
else if (i % 2 == 0 && j % 2 == 0)
cfx[i][j] = '+';
else if (i % 2 == 0 && j & 2 != 0)
cfx[i][j] = '-';
}
for(int k = 2 * b,i = 0 ; i < 2*b;++i,--k)
for (int j = k; j < 2 * a + k ;j++)
{
if (i % 2 == 0 && j % 2 == 0)
cfx[i][j] = '+';
else if (i % 2 == 0 && j % 2 != 0)
cfx[i][j] = '-';
else if (i % 2 != 0 && j % 2 != 0)
cfx[i][j] = '/';
}
//for (int k = 2 * b, j = 2 * a + 1; j < 2 * a + 2 * b+1; --k, j++)
for(int k = 2 * b, j = 2 * a + 2 * b; j >= 2 * a + 1; --j,--k)
for (int i = 2 * b - k ; i < 2 * b + 2 * c - k+1; ++i)
{
if (i % 2 != 0 && j % 2 != 0)
cfx[i][j] = '/';
else if (i % 2 == 0 && j % 2 == 0)
cfx[i][j] = '+';
else if (i % 2 != 0 && j % 2 == 0)
cfx[i][j] = '|';
}
for (int i = 0; i < 2 * c + 2 * b + 1; ++i)
{
for (int j = 0; j < 2 * a + b * 2 + 1; ++j)
cout << cfx[i][j];
cout << endl;
}
}
return 0;
}
实例输出: