A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

原题不公布了,有兴趣私聊,一下是原题演化过来的,编写代码实现图形输出

新建图片.jpg (16.53 KB, 下载次数: 2)

新建图片.jpg

2 个回复

倒序浏览
这是另一个图形,不太好看

新建图片1.jpg (7.66 KB, 下载次数: 0)

新建图片1.jpg
回复 使用道具 举报
  1. public class Pic {       
  2.         public static void main(String[] args) {
  3.                 pic1(27);
  4.                 pic2(27);
  5.         }
  6.         //第一行没空格的情况
  7.         public static void pic1(int i)
  8.         {               
  9.                 //将第一行输出
  10.                  for(int x=0;x<i;x++){
  11.                           System.out.print("*");
  12.                   }               
  13.                 System.out.println("");//换行
  14.                 //x控制行数,a为循环当前行数
  15.                 for(int x=i,a=1;x>2;a++)
  16.                 {                       
  17.                         //循环改变x的值,该值代表该行*个数
  18.                         x=x/3;
  19.                         //列数和第一行一样,符合条件输出*,其他位置输出空格
  20.                         for(int y=1;y<=i;y++)
  21.                         {
  22.                                 int z=1;
  23.                                 boolean b=false;
  24.                                 //判断是否满足输出*条件
  25.                                 while(z<=x)
  26.                                 {
  27.                                         if(y==(contr1(a)+contr2(a,z)))
  28.                                         {
  29.                                                 System.out.print("*");
  30.                                                 b=true;
  31.                                                 break;
  32.                                         }                                               
  33.                                         z++;
  34.                                 }
  35.                                 //输出了*就不要输出空格
  36.                                 if(b)
  37.                                         continue;
  38.                                 System.out.print(" ");                               
  39.                         }                       
  40.                         System.out.println("");
  41.                 }
  42.         }
  43.         //第一行有空格情况
  44.         public static void pic2(int i){
  45.                 //输出第一行,由于加入了空格,其长度变为i+i/3
  46.                 for(int x=1;x<=(i+i/3);x++)
  47.                 {
  48.                         //在余4为零的位置输出空格,其他位置输出*
  49.                         if(x%4==0)
  50.                                 System.out.print(" ");
  51.                         else
  52.                                 System.out.print("*");
  53.                 }
  54.                 System.out.println("");//换行
  55.                 //x控制行数,a为当前行号
  56.                 for(int x=i,a=1;x>1;a++)
  57.                 {                       
  58.                         x=x/3;
  59.                         //遍历i+i/3次,在满足条件的位置输出*,其他位置输出空格
  60.                         for(int y=1;y<=(i+i/3);y++)
  61.                         {
  62.                                 int z=1;
  63.                                 boolean b=false;
  64.                                 //每行输出的*个数由x控制
  65.                                 while(z<=x)
  66.                                 {
  67.                                         if(y==(contr11(a)+contr22(a,z)))
  68.                                         {
  69.                                                 System.out.print("*");
  70.                                                 b=true;
  71.                                                 break;
  72.                                         }               
  73.                                         z++;
  74.                                 }                       
  75.                                 if(b)
  76.                                         continue;
  77.                                 System.out.print(" ");                               
  78.                         }                       
  79.                         System.out.println("");
  80.                 }
  81.         }
  82.         //递归实现3的幂次运算方法
  83.         public static int mi3(int i)
  84.         {
  85.                 if(i==0)
  86.                         return 1;
  87.                 else
  88.                         return 3*mi3(i-1);
  89.         }
  90.         //行的变化规律
  91.         public static int contr1(int a)
  92.         {
  93.                 if(a==1)
  94.                         return 2;
  95.                 else               
  96.                         return mi3(a-1)+contr1(a-1);                               
  97.         }
  98.         //列的变化规律
  99.         public static int contr2(int a,int z)
  100.         {
  101.                 return mi3(a)*(z-1);
  102.         }
  103.         //行的变化规律
  104.         public static int contr11(int a)
  105.         {
  106.                 if(a==1)
  107.                         return 2;
  108.                 else
  109.                         return mi3(a-1)+mi3(a-2)+contr11(a-1);
  110.         }
  111.         //列的变化规律
  112.         public static int contr22(int a,int z)
  113.         {
  114.                 return (mi3(a)+mi3(a-1))*(z-1);
  115.         }
  116. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马