“弓”字排列 1-100

描述:
“弓”字排列1-100,要求时间复杂度O(n)
结果输出:

1 2 3 4 5 6 7 8 9 10
20 19 18 17 16 15 14 13 12 11
21 22 23 24 25 26 27 28 29 30
40 39 38 37 36 35 34 33 32 31
41 42 43 44 45 46 47 48 49 50
60 59 58 57 56 55 54 53 52 51
61 62 63 64 65 66 67 68 69 70
80 79 78 77 76 75 74 73 72 71
81 82 83 84 85 86 87 88 89 90
100 99 98 97 96 95 94 93 92 91

代码参考:

public class Num {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] num = new int[100];
		for(int i=1;i<=num.length;i++){
			if((i-1)/10%2==1){  //偶数行
				int x=0;
				x=i;
				for(int j=9;j>=0;j--){
					num[x-1]=i+j;
					x++;
				}
				i=i+9;
			}else{              //奇数行
				num[i-1]=i;
			}
		}
		for(int n=1;n<=num.length;n++){
			System.out.print(num[n-1]+" ");
			if(n%10==0){
				System.out.println();
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/wzr0823/article/details/84989899