当前位置:首页 > Java趣味程序(二)
/**
大家好,我现在正在学习java,虽然在这之前我已经学习过一遍了,但是现在再重新来学,才发现以前学的太肤浅了,而且学的质量也很不好,所以,现在我又重新站在了新的起跑线上,开始了我的java学习之旅,喜欢java的朋友和想学习java的朋友来和我一起前进吧。我会及时的把自己学的一些东西总结出来,并传送到文库中和大家一起分享的。 所以Make The Change的时候到了,Everyone,Come On! (我的QQ号jiaziming1990@qq.com,愿意交流的同学可以加我呦) */
/*
1.螺旋方阵:例: 1 2 3 16 17 18 15 …… 14 22 13 12 11 代码实现如下: */
import java.util.Scanner;
4 19 20 21 10 5 6 7 8 9 public class SpiralSquare {
public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println(\请输入螺旋方阵的边长\);
int index=s.nextInt(); if(index<=0){
System.out.println(\输入的数字不合法!\);
return; }
int[][] square=new int[index][index]; int col=-1;//define column int row=0;//define row
for(int i=1;i<=index*index;){
while(col+1 while(row+1 while(col-1>=0&&square[row][col-1]==0) square[row][--col]=i++; while(row-1>=0&&square[row-1][col]==0) square[--row][col]=i++; } for(int i=0;i System.out.print(square[i][j]+\); } System.out.println(); } } } /* 2.螺旋矩阵:例: 1 2 3 14 15 16 13 20 19 4 17 18 5 6 7 12 11 10 代码实现如下: */ import java.util.Scanner; public class SpiralSquare01{ 9 8 public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println(\请输入螺旋方阵的长\); int indexY=s.nextInt(); System.out.println(\请输入螺旋方阵的宽\); int indexX=s.nextInt(); if(indexX<=0||indexY<=0){ System.out.println(\输入的数字不合法!\); return; } int[][] square=new int[indexX][indexY]; int x=0; int y=0; for(int i=1;i<=indexX*indexY;){
共分享92篇相关文档