zj_maddog 2016-10-20 05:30 采纳率: 40%
浏览 2716
已采纳

阵列打印,给定一个正整数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
请完成以下方法体实现上述功能
public void printNum(int n){

}

  • 写回答

5条回答 默认 最新

  • SeaTalks 2016-10-20 06:06
    关注

    你可以参考下

     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);
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题