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 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件