2,打印十字图

问题描述
小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:
..

<script id="MathJax-Element-10279" type="math/tex; mode=display"></script>
<script id="MathJax-Element-10280" type="math/tex; mode=display"></script>
<script id="MathJax-Element-10281" type="math/tex; mode=display"></script> .... ........... ..
.
$.
....... .
$.
$.
.
. ... .
..
$.$.
. .. . .... .
.. .
. . .. . .... .
..
$.$.
. .. .... .
$.
$.
.
...……. ...
$.
<script id="MathJax-Element-10324" type="math/tex; mode=display"></script>
$.
.. ........... ....
<script id="MathJax-Element-10328" type="math/tex; mode=display"></script>
<script id="MathJax-Element-10329" type="math/tex; mode=display"></script>
<script id="MathJax-Element-10330" type="math/tex; mode=display"></script>..
对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。
输入格式
一个正整数 n (n<30) 表示要求打印图形的层数。
输出格式
对应包围层数的该标志。
样例输入1
1
样例输出1
..
<script id="MathJax-Element-10331" type="math/tex; mode=display"></script> .... ... ..
.$.
...
.
. ...
$.$.
.. ... ....
<script id="MathJax-Element-10344" type="math/tex; mode=display"></script>..
样例输入2
3
样例输出2
..
<script id="MathJax-Element-10345" type="math/tex; mode=display"></script>
<script id="MathJax-Element-10346" type="math/tex; mode=display"></script>
<script id="MathJax-Element-10347" type="math/tex; mode=display"></script> .... ........... ..
.
$.
....... .
$.
$.
.
. ... .
..
$.$.
. .. . .... .
.. .
. . .. . .... .
..
$.$.
. .. .... .
$.
$.
.
...……. ...
$.
<script id="MathJax-Element-10390" type="math/tex; mode=display"></script>
$.
.. ........... ....
<script id="MathJax-Element-10394" type="math/tex; mode=display"></script>
<script id="MathJax-Element-10395" type="math/tex; mode=display"></script>
<script id="MathJax-Element-10396" type="math/tex; mode=display"></script>..
提示
请仔细观察样例,尤其要注意句点的数量和输出位置。
仔细观察,发现行数 列数与n的关系是4*n+5
通过分析样例,发现奇数行中行数大于行数减去2的打印 1 。上代码
package cn.lml.lanqiao.www;
import java.util.*;
public class LanQiao {

/**
 * @param args
 */
//找规律,实在是无技术,规律难找,找了也不好写,2017年1月21日
public static void main(String[] args) {
    // TODO Auto-generated method stub
        Scanner s=new Scanner (System.in);
        int n=s.nextInt();
        for(int i=1;i<=n*4+5;i++){
            for(int j=1;j<=n*4+5;j++){
                if(f(i,j,n)){
                    System.out.print("$");
                }
                else
                    System.out.print(".");
            }
            System.out.println();
        }
}

private static boolean f(int i, int j, int n) {
    // TODO Auto-generated method stub
    /*
     * 下面考虑如何打印所有部分/
     */


    if(i>2*n+3){//考虑上下两部分
        i=n*4+6-i;
    }
    if(j>2*n+3){
        j=n*4+6-j;
    }
    if(j>i){
        i^=j;
        j^=i;
        i^=j;
    }
    /*
     * 下面打印的是左上部分/
     */
    if(i<=2&&j<=2)return false;
    if(i%2==1&&j>i-2)return true;//奇数行且列数比行数减去2大,就打印$
    if(j%2==1&&j!=i-1)return true;//奇数列且列数不等于行数减去1,就打印.

    return false;
}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值