Print the number of loop

2010-09-16  来源:本站原创  分类:Java  人气:175 

public class LoopPrinter {

        /**
         *  To obtain a store back to the number of turns of the two-dimensional array
         *
         * @param row
         *             The number of rows
         * @param col
         *             The number of columns
         * @return
         */
        public static int[][] loopArray(final int row, final int col) {
                int[][] array = new int[row][col];
                int maxCount = row * col;
                int count = 1;
                int less = row < col ? row : col;
                int loopLimit = less / 2; //  The maximum number of turns
                if (less % 2 > 0) {
                        if (row == col) {
                                array[loopLimit][loopLimit] = maxCount;
                        }
                        loopLimit++;
                }

                for (int loop = 1, i = 0, j = 0; loop <= loopLimit; loop++, i++, j++) {
                        for (; j < col - loop; j++) {
                                array[i][j] = count++;
                        }
                        for (; i < row - loop; i++) {
                                array[i][j] = count++;
                        }
                        for (; j > loop - 1 && count <= maxCount; j--) {
                                array[i][j] = count++;
                        }
                        for (; i > loop - 1 && count <= maxCount; i--) {
                                array[i][j] = count++;
                        }
                }

                return array;
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
                int row = 6;
                int col = 5;
                long nano = System.nanoTime();
                int[][] array = loopArray(row, col);
                System.out.println(" Available  :" + (System.nanoTime() - nano) + " ns");

                for (int i = 0; i < array.length; i++) {
                        for (int j = 0; j < array[i].length; j++) {
                                System.out.print(array[i][j] + "\t");
                        }
                        System.out.println();
                }
        }

}

 Output results  :
int row=6, col=5;
1       2       3       4       5
18      19      20      21      6
17      28      29      22      7
16      27      30      23      8
15      26      25      24      9
14      13      12      11      10
相关文章
  • Print the number of loop 2010-09-16

    public class LoopPrinter { /** * To obtain a store back to the number of turns of the two-dimensional array * * @param row * The number of rows * @param col * The number of columns * @return */ public static int[][] loopArray(final int row, final int

  • PHP print the number of ring ring 2010-06-29

    Such as the title: int i = [number]; Print the following graphic: 1 2 3 4 1,213,145 1116156 10987 PHP code is as follows function sort_recursive(&$array){ foreach($array as $key=>$edge){ ksort($edge,true); $array[$key]=$edge; } ksort($array,true);

  • POJ 1728 A flea on a chessboard [number theory - loop modulo circuit judge issues +] 2010-05-31

    A flea on a chessboard Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1509 Accepted: 491 Description Security Infinite chessboard is obtained by extending a Finite chessboard to the right and up Infinitely. Each square of the chessboard i

  • [QQ] for several less upset determine what number? 2010-03-17

    1. Question: 100 Number of consecutive upset, the random out a number, ask how best to quickly determine which less? Analysis: For all 100 consecutive numbers, as long as the addition of more than 100. Sure between 0 and 99. In general, the more conv

  • Find the prime numbers 1-1000 & find all the factors of a few of a number & find all the prime factors 2010-12-02

    The current confusion is, suShu1 () number of cycles is also more than suShu2, why? ! public class SuShu { /** * @param args */ public static void main(String[] args) { suShu1(30); System.out.println("\r\n---------------------"); suShu2(5, 30);

  • Java Print program design 2010-03-29

    SUN company has been committed to the improvement of Java printing capabilities, while the Java2 platform to finally come up with a robust model of the beginning of printing, the print mode and the Java2D graphics package fully integrated into one. E

  • 3 thread Print N times the order of ABC 2010-04-04

    I remember a thread a while ago to discuss the order of JE printing surface examination questions, and now also practice very empty. Say it is, three threads, its a print A, its a print B, its a print C, in order to request the opening three threads

  • Print inverted triangle 2010-12-26

    / * * Print the following graphic: * ********* * ******* * ***** * *** * * * / # Include <stdlib.h> # Include <stdio.h> void printblank (int n) { for (int i = 0; i <n; i + +) { printf (""); } } void printgra (int n) { for (int i =

  • Bash string processing (and Java control) - 15. Calculated the number of times substring appears 2011-09-28

    Bash string processing (and Java control) - 15. Calculated the number of times substring appears In Java StringUtils.countMatches org.apache.commons.lang.StringUtils countMatches method writes public static int countMatches (String str, String sub) C

  • java programming optimization 2010-03-29

    1.JVM manage two types of memory: heap memory (heap), stack memory (stack), stack is mainly used within stored procedures at run time to create or instantiate objects and variables. While the stack memory is used to store programs code declared as a

  • poj1979 deep traversal 2010-04-04

    Reiteration Problem Description: There is a rectangular room is covered with square tiles. Each square tile color is red or black. A man was standing on a black tile. He stood from his square tiles, it can be transferred to the adjacent one of the fo

  • jdk 1.5 New Features 2010-01-14

    "JDK1.5" An important theme is through to add some features to simplify the development of these features, including generics, for-else loop, auto-install package / unpacking, enumeration, variable parameters, static import. The use of these fea

  • jdk 1.5 New Features Description 2010-03-21

    "JDK1.5" An important theme is through to add some features to simplify the development of these features, including generics, for-each loop, auto-install package / unpacking, enumeration, variable parameters, static import. The use of these fea

  • JDK1.5 introduces new features 2010-03-31

    "JDK1.5" (code-named Tiger) is an important theme is by adding some features to simplify development, these features include generics, for-each loop, auto-install package / unpacking, enum, variable parameters, static Import. Use these features

  • Perl Language Overview 2010-02-21

    1.1 from scratch, we believe that Perl is an easy to learn and use language, and we hope to prove that we are right. Simple one aspect of Perl is that you do not need to say before you say something that a lot of other things. In many other programmi

  • php (grammar) flow control 2010-03-31

    1: if statement IF statement it under the terms of the implementation of the segment. The IF statement in PHP is similar to C: if (expr) statement As discussed in the expressions, expr is evaluated for its true value. If expr is TRUE, PHP implementat

  • Shangxue Tang Ma Shibing Oracle Tutorial Notes 2010-03-02

    :wink: First, log on to super administrator oracle sqlplus sys/bjsxt as sysdba Then, lift scott User lock alter user scott account unlock; Then the user name can be used . ( The default global database name orcl) 1.select ename, sal * 12 from emp; //

  • Burnt-out acrylic flocking carpet bump printing process (2) 2010-02-18

    Keywords: acrylic carpet flocking, embossing Burnt-out printing Burnt-out acrylic flocking carpet bump printing process (2) 4 common defects and printing solutions 4.1 Flowers are not allowed Reason 4.1.1 (1) patch is not strong intermittent flat scr

  • C-common mistake for beginners 2010-03-09

    C language is the biggest characteristic: strong functions, convenient and flexible. C compiled program on the grammar check is not as strict as other high-level language, which gives programmers left "room for flexibility", but still flexible a

  • Use ResultSetMetaData display information such as field name 2010-04-13

    package com.test; import java.sql .*; / ** *//**************************************** * This example illustrates the problem: JDBC development process - using ResultSetMetaData display information such as field name * Note: This example uses the dri