演算法筆記_198:曆屆試題 列印十字圖(Java)

來源:互聯網
上載者:User

標籤:控制   方案   class   void   分享   href   blog   技術   head   

目錄

1 問題描述

2 解決方案

 1 問題描述問題描述

小明為某機構設計了一個十字型的徽標(並非紅十字會啊),如下所示:

..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..

對方同時也需要在電腦dos視窗中以字元的形式輸出該標誌,並能任意控制層數。

輸入格式一個正整數 n (n<30) 表示要求列印圖形的層數。輸出格式對應包圍層數的該標誌。範例輸入11範例輸出1..$$$$$..
..$...$..
$$$.$.$$$
$...$...$
$.$$$$$.$
$...$...$
$$$.$.$$$
..$...$..
..$$$$$..範例輸入23範例輸出2..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..提示請仔細觀察範例,尤其要注意句點的數量和輸出位置。

 

 

 

 

 

2 解決方案

 

 

具體代碼如下:

import java.util.Scanner;public class Main {        public void getResult(int n) {        int len = 5 + 4 * n;        int mid = len / 2;        char[][] result = new char[len][len];        for(int i = 0;i < len;i++)            for(int j = 0;j < len;j++)                result[i][j] = ‘.‘;        for(int i = mid - 2;i <= mid + 2;i++) {            result[mid][i] = ‘$‘;            result[i][mid] = ‘$‘;        }        for(int i = 1;i <= n;i++) {            for(int j = mid - 2 * i;j <= mid + 2 * i;j++) {                result[mid - 2 * (i + 1)][j] = ‘$‘;                result[mid + 2 * (i + 1)][j] = ‘$‘;                result[j][mid - 2 * (i + 1)] = ‘$‘;                result[j][mid + 2 * (i + 1)] = ‘$‘;            }            //左上方            result[mid - 2 * i][mid - 2 * i] = ‘$‘;            result[mid - 2 * i][mid - 2 * i - 1] = ‘$‘;            result[mid - 2 * i - 1][mid - 2 * i] = ‘$‘;            //右上方            result[mid - 2 * i][mid + 2 * i] = ‘$‘;            result[mid - 2 * i - 1][mid + 2 * i] = ‘$‘;            result[mid - 2 * i][mid + 2 * i + 1] = ‘$‘;            //左下角            result[mid + 2 * i][mid - 2 * i] = ‘$‘;            result[mid + 2 * i][mid - 2 * i - 1] = ‘$‘;            result[mid + 2 * i + 1][mid - 2 * i] = ‘$‘;            //右下角            result[mid + 2 * i][mid + 2 * i] = ‘$‘;            result[mid + 2 * i][mid + 2 * i + 1] = ‘$‘;            result[mid + 2 * i + 1][mid + 2 * i] = ‘$‘;        }        for(int i = 0;i < len;i++) {            for(int j = 0;j < len;j++) {                System.out.print(result[i][j]);            }            System.out.println();        }    }        public static void main(String[] args) {        Main test = new Main();        Scanner in = new Scanner(System.in);        int n = in.nextInt();        test.getResult(n);    }}

 

演算法筆記_198:曆屆試題 列印十字圖(Java)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.