http://acm.hdu.edu.cn/showproblem.php?pid=6330
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.
Sample Input
2
1 1 1
6 2 4
Sample Output
..+-+
././|
+-+.+
|.|/.
+-+..
....+-+-+-+-+-+-+
.../././././././|
..+-+-+-+-+-+-+.+
./././././././|/|
+-+-+-+-+-+-+.+.+
|.|.|.|.|.|.|/|/|
+-+-+-+-+-+-+.+.+
|.|.|.|.|.|.|/|/|
+-+-+-+-+-+-+.+.+
|.|.|.|.|.|.|/|/.
+-+-+-+-+-+-+.+..
|.|.|.|.|.|.|/...
+-+-+-+-+-+-+....
代码1,
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
int a,b,c,t;
cin>>t;
while(t--)
{
cin>>a>>b>>c;
for(int i=0;i<c+b+1;i++)
{
if(i<2*b)
{
for(int j=1;j<=2*a+1+2*b;j++)
{
if(j<=2*b-i&&j>0)
{
cout<<".";
}
else if(j>2*b-i&&j<=2*a+1+2*b-i&&2*b-i>0)
{
if(i%2==0)
{
if(j%2==0)
{
cout<<"-";
}
else
cout<<"+";
}
else
{
if(j%2==0)
{
cout<<"/";
}
else
cout<<".";
}
}
else
{
if(i%2==0)
{
if(j%2==0)
{
cout<<".";
}
else
cout<<"+";
}
else
{
if(j%2==0)
{
cout<<"/";
}
else
cout<<"|";
}
}
}
cout<<endl;
}
else
{
for(int j=1;j<=2*a+1+2*b;j++)
{
if(j<=2*a+1)
{
if(i%2==0)
{
if(j%2==0)
{
cout<<"-";
}
else
cout<<"+";
}
else
{
if(j%2==0)
{
cout<<"|";
}
else
cout<<".";
}
}
else
{
if(i%2==0)
{
if(j%2==0)
{
cout<<".";
}
else
cout<<"+";
}
else
{
if(j%2==0)
{
cout<<"/";
}
else
cout<<"|";
}
}
}
cout<<endl;
}
}
for(int i=c+b-1;i>=0;i--)
{
for(int j=1;j<=2*a+1+2*b;j++)
{
if(2*b-i>=0&&j>2*a+1+2*b-(2*b-i))
{
cout<<".";
}
else if(j>=1&&j<=2*a+1)
{
if(i%2==0)
{
if(j%2==0)
{
cout<<"-";
}
else
cout<<"+";
}
else
{
if(j%2==0)
{
cout<<".";
}
else
cout<<"|";
}
}
else
{
if(i%2==0)
{
if(j%2==0)
{
cout<<".";
}
else
cout<<"+";
}
else
{
if(j%2==0)
{
cout<<"/";
}
else
cout<<"|";
}
}
}
cout<<endl;
}
}
}//此代码,采用上下分开打印的方法,因为宽比长大的话,“.”打印不全,行数也会出现错误
代码2;
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
int a,b,c,s1,s2,t,l,h;
void san1(int i,int j)
{
if(i-(2*c+1)>0&&j>2*a+1+2*b-(i-(2*c+1))&&s1>0)
{
cout<<".";
return;
}
if(s1>0)
{
if(j>s1&&j<=s1+2*a+1)
{
if(i%2==0)
{
if(j%2==0)
{
cout<<"/";
}
else
cout<<".";
}
else
{
if(j%2==0)
{
cout<<"-";
}
else
cout<<"+";
}
}
else if(j>s1+2*a+1)
{
if(i%2==0)
{
if(j%2==0)
{
cout<<"/";
}
else
cout<<"|";
}
else
{
if(j%2==0)
{
cout<<".";
}
else
cout<<"+";
}
}
else
{
cout<<".";
}
}
}
void san2(int i,int j)
{
if(i-(2*c+1)>0&&j>2*a+1+2*b-(i-(2*c+1))&&s1<=0&&j<=2*a+1+2*b)
{
cout<<".";
return;
}
if(s1<=0&&j<=2*a+1)
{
if(i%2==0)
{
if(j%2==0)
{
cout<<".";
}
else
cout<<"|";
}
else
{
if(j%2==0)
{
cout<<"-";
}
else
cout<<"+";
}
}
else if(s1<=0&&j>2*a+1)
{
if(i%2==0)
{
if(j%2==0)
{
cout<<"/";
}
else
cout<<"|";
}
else
{
if(j%2==0)
{
cout<<".";
}
else
cout<<"+";
}
}
}
int main()
{
cin>>t;
while(t--)
{
cin>>a>>b>>c;//长宽高
h=2*b+1+2*c;
l=2*a+2*b+1;
s1=2*b,s2=0;
for(int i=1;i<=h;i++)
{
for(int j=1;j<=l;j++)
{
san1(i,j);
san2(i,j);
}
cout<<endl;
if(s1>0)
s1--;
if(i>2*c+1&&s2<4)
s2++;
}
}
}
//此代码整体打印
模拟过程请看代码
代码3;
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- const int maxn = 1000;
- char Map[maxn][maxn];
- void print(int n, int m)
- {
- for(int i = 0; i < n; i++)
- {
- for(int j = 0; j < m; j++)
- {
- cout << Map[i][j];
- }
- cout << endl;
- }
- }
- int main()
- {
- int a,b,c,T;
- cin>>T;
- while(T--)
- {
- cin>>a>>b>>c;
- int n = 2*b+1 + 2*c;//计算整个大正方行的边长
- int m = 2*b+1 + 2*a;
- for(int i = 0; i < n; i++)//将整个地图赋值为点
- {
- for(int j = 0; j < m; j++)
- {
- Map[i][j] = '.';
- }
- }
- int flag = 0;
- for(int i = 2*b+1; i < n; i++)//将正视图赋值,开始条件和结束条件很重要
- {
- if(flag%2==0)
- {
- for(int j = 0; j < 2*a; j+=2)
- {
- Map[i][j] = '|';
- }
- }
- else
- {
- for(int j = 0; j < 2*a; j++)
- {
- if(j%2==0)
- {
- Map[i][j] = '+';
- }
- else
- {
- Map[i][j] = '-';
- }
- }
- }
- flag++;
- }
- for(int i = 0; i < 2*b+1; i++)//将俯视图赋值
- {
- if(i%2==0)
- {
- for(int j = 2*b+1-i-1; j < m-i-1; j ++)
- {
- if(j%2==0)
- {
- Map[i][j] = '+';
- }
- else
- {
- Map[i][j] = '-';
- }
- }
- }
- else
- {
- for(int j = 2*b+1-i-1; j < m-i-1; j +=2)
- {
- Map[i][j] = '/';
- }
- }
- }
- flag = 0;
- for(int j = m-1; j >= 2*a; j--)//将右视图赋值
- {
- if(flag %2 == 0)
- {
- for(int i = flag; i <= 2*c+flag; i++)
- {
- if(i%2==0)
- {
- Map[i][j] = '+';
- }
- else
- {
- Map[i][j] = '|';
- }
- }
- }
- else
- {
- for(int i = flag; i <= 2*c+flag; i+=2)
- {
- Map[i][j] = '/';
- }
- }
- flag++;
- }
- print(n,m);
- }
- return 0;
- }