Java rotates the matrix 45-degree output

Source: Internet
Author: User

For example:

A B C D E F G H I J K L M N O P Q R S T U V W X Y     E       D J     C I O   B H N T A G M S Y F L R X   K Q W     P V       U    

Ideas such as:
Outside the diamond is a space, within the diamond (can use function to judge) There are two kinds of points, one is a character, one is a space
It can be found that there is a character position (col-row)%2 = = 0; next look for a 45 degree diamond and rectangle correspondence, row ' = (col-row)/2, col ' =col-row '

The complete code is as follows:

 Public  class printmatrixtrans45degree {     Public Static void Main(string[] args) {System.out.println ("Hello world!");intC=0;Char[] arr =New Char[5][5]; for(inti =0; I <5; i++) { for(intj =0; J <5; J + +) {arr[i][j]= (Char)(' A '+ c);            C + +; }        } for(inti =0; I <5; i++) { for(intj =0; J <5; J + +) {System.out.print (arr[i][j]+" ");        } System.out.println (); } for(inti =4; I >=-4; i--) {introw =0;intCol =0; for(intj =0; J <=8; J + +) {if(Isarea (I,J)) {if((j-i)%2==0){//Print lettersrow = (j-i)/2;                        col = J-row;                    System.out.print (Arr[row][col]); }Else{System.out.print (" "); }                                   }Else{//Print spaceSystem.out.print (" ");        }} System.out.println (); }    } Public Static Boolean Isarea(intRow,intCol) {if(Row <= col && Row >= Col-8&& row >=-col && row <=-col +8){//system.out.println ("(" +row+ "," +col+ ")");            return true; }return false; }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java rotates the matrix 45-degree output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.