中兴17 春招大题一道

输入一个整数返回阵列

package com.base;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int N = sc.nextInt();
            for (int i = 0; i < N; i++) {
                int a = i * (i + 1) / 2;
                int b = (i + 1) * (i + 2) / 2;
                if (i % 2 == 0) {
                    for (int j = a + 1; j <= b; j++) {
                        if (j == b) {
                            System.out.println(j);
                            break;
                        }
                        System.out.print(j+"*");
                    }
                } else {
                    for(int j = b; j >= a + 1; j--){
                        if(j == a + 1){
                            System.out.println(j);
                            break;
                        }
                        System.out.print(j+"*");
                    }
                }
            }
        }
    }
}

结果:

10
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
21*20*19*18*17*16
22*23*24*25*26*27*28
36*35*34*33*32*31*30*29
37*38*39*40*41*42*43*44*45
55*54*53*52*51*50*49*48*47*46

你可能感兴趣的:(程序员笔试)