1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
請完成以下方法體實現上述功能
public void printNum(int n){
}
你可以參考下
public class printmatrix {
public static int helper[] = new int[100];
public static void calc(int n){
if(n==1){
helper[n]=1;
return;
}
if(helper[n-1]==0)
calc(n-1);
helper[n] = helper[n-1]+n;
}
public static void printNum(int n){
if(helper[n]==0){
calc(n);
}
String[] strs = new String[n+1];
int row = 1;
for(;row<=n;row++){
String str = new String();
for(int i=helper[row-1]+1;i<=helper[row];i++){
if(!str.equals(""))str+='*';
str += i;
}
strs[row] = str;
}
for(String str : strs){
if(str==null)continue;
System.out.println(str);
}
for(int i=n;i>0;i--){
System.out.println(strs[i]);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
printmatrix.printNum(4);
}
}