模拟立方体

本文介绍了一种用于在控制台输出3D立方体图形的算法,通过使用不同的字符来表示立方体的不同面,包括顶面、底面、侧面和边框。该算法能够根据输入的长、宽、高参数生成不同大小的立方体图形,适用于解决三维几何问题的可视化需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
  • }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值