我被要求为以下结构编写代码:
1 3 2 4 5 6 10 9 8 7 11 12 13 14 15
我尝试了下面的代码,它有效,但我想知道是否有更好的方法:
public static void main(String[] args) {
int no_of_rows=5;
int c=0;
for(int i=1;i<=no_of_rows;i++){
for(int k=i;k<no_of_rows;k++){
System.out.print(" ");
}
if(i%2!=0){
for(int j=0;j<i;j++){
c++;
System.out.print(c +" ");
}
}
else{
int a[] = new int [i+1];
for(int j=0;j<i;j++){
c++;
a[j]=c;
}
for(int j=i-1;j>=0;j--){
System.out.print(a[j]+" ");
}
}
System.out.println("");
}
}
有没有更好的实施或我做得对吗?
答案 0 :(得分:2)
我认为唯一的改进是删除此块中的数组:
else{
int a[] = new int [i+1];
for(int j=0;j<i;j++){
c++;
a[j]=c;
}
for(int j=i-1;j>=0;j--){
System.out.print(a[j]+" ");
}
}
我们可以轻松地计算出反向使用的值,而无需额外的数组:
c += i;//Update c
for(int j = 0; j < i ; j++){
System.out.print((c - j) + " " );
}
答案 1 :(得分:1)
虽然您的解决方案确实有效,但如果不仔细研究,目前尚不清楚它的作用。
为了更加清晰,我会使用像Iterable
这样更复杂的东西:
/**
* Generates a range of numbers from start to end.
*
* Automatically reverses the sequence if end < start.
*/
public static class Range implements Iterable<Integer> {
private final int start;
private final int end;
private final int step;
public Range(int start, int end) {
this.start = start;
this.end = end;
// Automagically pick the direction.
this.step = start > end ? -1 : 1;
}
@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
// Start there.
private int n = start;
// Have we finished yet?
private boolean finished = false;
@Override
public boolean hasNext() {
return !finished;
}
@Override
public Integer next() {
int next = n;
if (n != end) {
// Step one more.
n = n + step;
} else {
// We're emitting the `end` value - time to stop.
finished = true;
}
return next;
}
};
}
}
public void test() {
int rows = 5;
int start = 1;
int count = 1;
for (int row = 1; row <= rows; row++) {
// Gap at the start.
for (int k = row; k < rows; k++) {
System.out.print(" ");
}
// Odd rows go forward.
Range r = (row & 1) == 0 ? new Range(start + count - 1, start) : new Range(start, start + count - 1);
// Print a row.
for (int i : r) {
System.out.print(i + " ");
}
// Start after last row finished.
start += count;
// One more number.
count += 1;
System.out.println("");
}
}
public static void main(String args[]) {
try {
new Test().test();
} catch (Throwable t) {
t.printStackTrace(System.err);
}
}
当n增加时,此解决方案也不会占用额外的内存。
答案 2 :(得分:1)
IMO,首先,您应该计算rows
而不是将其硬编码为5
我写了这个片段,打印输出:
public static void main(String[] args) throws ParseException {
int x = 105;
int rows = Double.valueOf(Math.sqrt(2 * x)).intValue();
int n = 1;
for (int r = 1; r <= rows; r++) {
System.out.print(new String(new char[rows - r]).replace("\0", " "));
if ((r & 1) != 0)
for (int i = n; i < n + r && n + r <= x; i++)
System.out.printf("%s%s", i, i == n + r - 1 || i == x ? "\n" : " ");
else
for (int i = n + r - 1 > x ? x : n + r-1; i >= n; i--)
System.out.printf("%s%s", i, i == n ? "\n" : " ");
n = n + r;
}
}
x
来表示数字,可以很容易地将其更改为带参数的方法。x
,输出可能看起来不那么好,因为我将单个空格设置为数字之间的分隔符,但是大数字可能更长。但无论如何都要保持布局。 x=105
的输出:
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
56 57 58 59 60 61 62 63 64 65 66
78 77 76 75 74 73 72 71 70 69 68 67
79 80 81 82 83 84 85 86 87 88 89 90 91
105 104 103 102 101 100 99 98 97 96 95 94 93 92