题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int a = sc.nextInt(); int lastElementInRowEnd = 0;
for(int i = 0; i <= a; i++) {
lastElementInRowEnd += i;
}
int firstElementInRowHead = 1;
int firstElementAddInRowHead = 1;
for(int row = 0; row < a; row++) {
it(firstElementInRowHead, lastElementInRowEnd, firstElementAddInRowHead);
lastElementInRowEnd--;
firstElementInRowHead += (1 + row);
firstElementAddInRowHead++;
}
}
}
//Used to print every row element
private static void it(int a, int b, int c) {
for(int i = a; i <= b; i += c ) {
System.out.print(i + " ");
c++;
}
System.out.println();
}
}
//Above all, We can tell all information from variables name, so I don't need to talk any more

查看12道真题和解析