第1个回答 2011-10-07
int i=0,j=1,n,t,m=1;
scanf("%d",&n);
t=n*n;
while(m<=t)
{if(j%2)
for(i=0;i<n;i++)
printf("%3d",m+i);
else
for(i=0;i<n;i++)
printf("%3d",m+n-i-1);
m+=n;
printf("\n");
j++;
}
第2个回答 2011-10-07
import java.util.Scanner;
public class SnakePrint {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please input the row to genereate: ");
int row = scanner.nextInt();
System.out.print("Please input the column to generate: ");
int col = scanner.nextInt();
int number = row * col;
int i = 1;
boolean isRight = true;
while(i <= number){
if(isRight){
for(int j = 0; j < col; j++){
System.out.print(i++ + " ");
}
}else{
StringBuffer sb = new StringBuffer();
for(int j = col; j > 0; j--){
sb.insert(0, " "+i++);
}
System.out.print(sb.toString().trim());
}
System.out.println();
isRight = !isRight;
}
}
}
----------------testing
Please input the row to genereate: 3
Please input the column to generate: 4
1 2 3 4
8 7 6 5
9 10 11 12