一道笔试题!请求帮忙! 手动输入一个整数n然后对n*n个整数进行蛇型排列如:n=3排列为1 2 36 5 47 8 9如:n=4排列为1 2 3 4 8 7 6 59 10 11 1216 15 14 13 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class b{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.BufferedInputStream;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; } } }} import java.io.*;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(""); } } }} class Table{ 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 class number{ 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); }} mahb520(玉狐) 的程序实在是写得哼哼哈嘻......... -_-||| /* * 创建日期 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 javaSe 基础 web start 启动的应用程序如何发送HTTP请求 JInternalFrame的焦点问题 画动画的小问题 请教一个关于JAVA读取数据库的问题? 请问在java中如何调用窗口最大化和最小化方法? 初学者报道(二) 急!关于消息传递的问题,今天必须解决 请问谁有JRUN企业版的SN JFrame 改图标~~困扰! 请助:各位高手请问如何用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