简体   繁体   English

在满足这些条件的同时如何打印以下顺序

[英]How to print the following sequence, while satisfying these conditions

This was actually an interview question. 这实际上是一个面试问题。 I had to print the following using Java: 我必须使用Java打印以下内容:

9
9 8 9
9 8 7 8 9
9 8 7 6 7 8 9
. . .
. . .

During the interview, I wrote an embarrassing piece of code, but it worked nonetheless - using an outer loop, two inner loops (one for the decrementing sequence and one for the incrementing sequence!) and a ton of variables. 在采访中,我编写了一段令人尴尬的代码,但仍然有效-使用一个外循环, 两个内循环(一个用于递减序列,一个用于递增序列!)和大量变量。 One of the variables was the length of each row. 变量之一是每一行的长度。

The interviewer asked me to try and rewrite it using 面试官让我尝试使用

  • just one outer and one inner loop 只有一个外环和一个内环

  • without the row length variable. 没有行长变量。

Note: After looking at the answers, I think the interviewer didn't really mean the second condition. 注意:看完答案后,我认为面试官并不是真正的第二个条件。 He might have just wanted me to simplify my code and the second point just bumbled out of his mouth. 他可能只是想让我简化我的代码,而第二点就从他的嘴里跌了下来。

So, later back home, I arrived at this: 所以,后来回到家,我来到了这里:

int rowCnt = 5;

for(int i = 1; i <= rowCnt; i++)
{
    int val = 9;
    int delta = -1;
    int rowLen = i * 2 - 1;

    for(int j = 1; j <= rowLen; j++)
    {
        System.out.print(val + " ");

        val += delta;

        if(j >= rowLen / 2) delta = 1;
    }

    System.out.println();
}

Here, I am using just one inner loop. 在这里,我仅使用一个内部循环。 I'm using a delta value to determine whether increment or decrement happens. 我正在使用delta值来确定是递增还是递减。 For each row, I compare the current index to the midpoint of the row and change the delta. 对于每一行,我将当前索引与该行的中点进行比较,并更改增量。

I satisfied the first condition - just one inner loop. 我满足了第一个条件-只是一个内循环。 But I am not able to do it without using the row length. 但是,如果不使用行长,我将无法做到这一点。

How can we print this without finding out the row length? 我们如何在不找出行长的情况下打印出来?

Many answers were acceptable, But I had to choose one, and picked the one that was simplest to understand for me. 许多答案都是可以接受的,但是我必须选择一个,然后选择一个对我来说最简单的答案。

How about: 怎么样:

    int start = 9;
    for (int i = 0; i <= start; i++) {
        StringBuilder sb = new StringBuilder((start - i) + " ");
        for (int j = start - i; j < start; j++) {
            sb.insert(0, (j + 1) + " ");
            sb.append((j + 1) + " ");
        }
        System.out.println(sb.toString());
    }

They probably wanted to hear the word ' recursion '. 他们可能想听听“ 递归 ”一词。

Here's a recursive solution that doesn't need length: 这是一个不需要长度的递归解决方案:

countDownInMiddle("", 9, "");

private static void countDownInMiddle(String start, int n, String end) {
    if (n < 0) {
        return;
    }
    System.out.println(start + n + end);
    countDownInMiddle(start + n, n - 1, n + end);
}

This is simple PHP, hope logic is clear and easily portable to Java: 这是简单的PHP,希望逻辑清晰并易于Java移植:

$rowCount = 10;
$startNum = 9;

for ($idx =0; $idx <$rowCount; $idx ++) {

    for ($jdx=0; $jdx < (2*$idx +1); $jdx++) {

        if ($idx < $jdx)
            echo $startNum -(2*$idx) + $jdx.' ';
        else
            echo $startNum - $jdx.' ';
    }
    echo '<br/>';
}
public class Pyramid {
    public static void main(String[] args) {
        int start = 9;
        String left = "";
        String right = "";
        for (int i=start; i>=0; i--) {
            System.out.println(left+i+right);
            left = left+i;
            right = i+right;
        }
    }
}

Sample output: 样本输出:

9
989
98789
9876789
987656789
98765456789
9876543456789
987654323456789
98765432123456789
9876543210123456789

This iterative solution is equivalent to the recursive solution. 此迭代解决方案等效于递归解决方案。 I would prefer to use iteration over recursion since the extra stack memory needed by the recursive solution could be huge when the number of rows grows big. 我宁愿使用迭代而不是递归,因为当行数增加时,递归解决方案所需的额外堆栈内存可能会很大。

My non-recursive solution: 我的非递归解决方案:

    for(int i = 0; i < 9; i++) {
        for(int j = 0; j < 2*i+1; j++)
            System.out.print((Math.abs(j - i) + 9 - i) + " ");
        System.out.println();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM