给定一个整数N,打印2*N行,如果N=4,打印结果如下:
1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
Input:n(整数行数)
Output:相应阵列
@author zhouzhihu
private static void printNum(int N) {
if(N==0) return;
int count = 1;
String[] str = new String[N];
//String sb;
StringBuilder sb;
for (int i = 1; i <= N; i++) {
//sb = new String();
sb=new StringBuilder();
for (int j = 0; j < i; j++) {
if (j < i - 1)
//sb += count + "*";
sb.append(count).append("*");
else
//sb += count;
sb.append(count);
count++;
}
//str[i - 1] = sb;
str[i - 1] = sb.toString();
}
for (int i = 0; i < 2 * N; i++) {
if (i < N)
System.out.println(str[i]);
else
System.out.println(str[2 * N - i - 1]);
}
}
如果有更好解法,欢迎交流。