手动输入一个整数n
然后对n*n个整数进行蛇型排列
如:
n=3
排列为
1 2 3
6 5 4
7 8 9
如:n=4
排列为
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
然后对n*n个整数进行蛇型排列
如:
n=3
排列为
1 2 3
6 5 4
7 8 9
如:n=4
排列为
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
解决方案 »
- java发送邮件,能发出去,但有些邮箱却收不到附件
- for循环还有这种特例???for(int i:a) 大家有见过吗??
- 有可以令java的.class文件恢复成.java文件的工具吗?
- 高分请教A*算法(来着有分)
- 刚弄懂了jb+weblogic+oracle+win2k,下一步该研究些什么?请大虾赐教!
- 高手帮忙!关于SOCKET问题
- 初学者的初学问题?
- linux下不支持java的多线程吗?80分送上
- s7110110(铁) 快去看我帖子呀~~~~~~~~~~~~~~~~~
- 急 jstl怎么遍历list啊?
- 请助:各位高手请问如何用java远程访问access
- 我郁闷死了,我觉得没问题的东西,却一直提示错误,希望大哥们,来看看呀~~~
static public void main(String args[]){
b a=new b();
a.display(4);
}
public void display(int Num){
String Temp="";
for(int i=1;i<=Num*Num;i++){
if((i/(Num+1))%2==0){
Temp=Temp+i;
}
else{
Temp=i+Temp;
}
if(i%Num==0){
System.out.println(Temp);
Temp="";
}
}
}
}
基本上就这个样子 可能Num大点会出错 写得比较仓促 思路就是这样
import java.io.DataInputStream;
import java.io.IOException;/**
* @author lbfan
*
*/
public class Snake { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
boolean even = false;
int temp;
// BufferedInputStream in = new BufferedInputStream(new DataInputStream(System.in));
DataInputStream in = new DataInputStream(new BufferedInputStream(System.in));
int n = Integer.parseInt(in.readLine());
if((n <= 0) || (n > 100)){
System.exit(0);
}
temp = n -1;
for(int i = 1; i <= n * n; i ++){
if(even == false){
System.out.print(i + " ");
} else if(even == true){
System.out.print(i + temp + " ");
temp -= 2;
}
if((i % n) == 0){
System.out.println();
temp = n -1;
if(even == true)
even = false;
else
even = true;
}
} }}
public class Test{
public static void main(String args[]) throws Exception{
System.out.print("Please input a number:");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
int length=Integer.parseInt(br1.readLine());
for(int i=0;i<length;i++){
if(i%2==0){
for(int j=0;j<length;j++){
System.out.print((i*length+j+1)+" ");
}
System.out.println("");
}
else{
for(int j=0;j<length;j++){
System.out.print(((i+1)*length-j)+" ");
}
System.out.println("");
}
}
}
}
int num=0;
int t[][]=null;
public void setNum(int i){
this.num=i;
}
public int getNum(){
return num;
}
public void setT(){
t=new int[num][num];
for(int i=0;i<num;i++){
for(int j=0;j<num;j++){
if(i%2==0)
t[i][j]=i*num+j+1;
else t[i][j]=(i+1)*num-j;
}
}
}
public void printT(){
for(int i=0;i<num;i++){
for(int j=0;j<num;j++){
System.out.print(t[i][j]+" ");
}
System.out.println();
}
}
}
public class Test {
public static void main(String[] args) {
Table table=new Table();
table.setNum(5);
table.setT();
table.printT();
}
}
{
public static void main(String[] args)
{
int n,i,j;
String str;
int a[];
System.out.println("Input N :");
BuffereReader br=new BuffereReader(new InputstracReader(System.in));
str=br.readLine();
n=Integer.parseInt(str);
for(i=1;i<=n*n;i++)
{
a[]=new a[n];
a[i]=i+1;
System.out.print(a[i]);
if(i%n==0)
System.out.println();
}
}
}
程序大概没错。就是InputstracReader()方法你再查查。我记得不是太清楚。
奇数行: row*len + col + 1
偶数行:(row+1) - col
是楼上的楼上不过,楼上兄弟的程序太疯狂
for里面的东东,太夸张了吧
--------------
a[]=new a[n];//n值固定
a[i]=i+1;//i的值 i<n*n
-------------
public class a{
public static void aa(int n){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(i%2==1){ System.out.print((i-1)*n+j); if(j==n) System.out.println();}
else { System.out.print(i*n-j+1); if(j==n) System.out.println();}
}
}
public static void main(String[] args){
aa(4);
}
}
* 创建日期 2005-9-29
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package yhf;/**
* @author hf.Yao
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class SnakeRank {
public static void display(int n){
for(int i = 0; i < n; i++){
if(i%2 != 0){
for(int j = n; j > 0; j--){
System.out.print(i*n+j+" ");
}
System.out.println("\n");
}else{
for(int k = 1; k < n+1; k++){
System.out.print(i*n+k+" ");
}
System.out.println("\n");
}
}
} public static void main(String[] args) {
display(9);
}
}测试结果
1 2 3 4 5 6 7 8 9 18 17 16 15 14 13 12 11 10 19 20 21 22 23 24 25 26 27 36 35 34 33 32 31 30 29 28 37 38 39 40 41 42 43 44 45 54 53 52 51 50 49 48 47 46