这段代码是否能进一步减少,也许可以不使用变量”k”(在这个示例中)。

huangapple go评论76阅读模式
英文:

Can this code be further reduce , maybe without variable "k" (in this example)

问题

import java.util.Scanner;

public class PracticePattern6 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        for (int i = 0; i < 2 * n + 1; i++) {
            if (i < n + 1) {
                for (int j = i; j < n; j++) {
                    System.out.print("* ");
                }
                for (int j = 0; j < 2 * i + 1; j++) {
                    System.out.print("  ");
                }
                for (int j = i; j < n; j++) {
                    System.out.print("* ");
                }
            } else {
                for (int j = 0; j < i - n; j++) {
                    System.out.print("* ");
                }
                for (int j = 0; j < 2 * n - k; j++) {
                    System.out.print("  ");
                }
                for (int j = 0; j < i - n; j++) {
                    System.out.print("* ");
                }
                k += 2;
            }
            System.out.println();
        }
        sc.close();
    }
}

Click here for output

英文:

Click here for output

JAVA

In this code, I have to use 7 for loops and assign the variable k. Is there any way to reduce the time complexity of this code, maybe without variable "k"

Here is my code:

import java.util.Scanner;
public class PracticePattern6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k=1;
for(int i=0;i&lt;2*n+1;i++)
{
if(i&lt;n+1)
{
for(int j = i ;j&lt;n;j++)
{ 
System.out.print(&quot;* &quot;);
}
for(int j =0;j&lt;2*i+1;j++)
{
System.out.print(&quot;  &quot;);
}
for(int j = i ;j&lt;n;j++)
{
System.out.print(&quot;* &quot;);
}
}
else
{
for(int j=0;j&lt;i-n;j++)
{
System.out.print(&quot;* &quot;);
}
for(int j=0;j&lt;2*n-k;j++)
{
System.out.print(&quot;  &quot;);
}
for(int j=0;j&lt;i-n;j++)
{
System.out.print(&quot;* &quot;);
}
k+=2;
}
System.out.println();
}
sc.close();
}
}

答案1

得分: 1

我认为这样会更简单一些不过我没有逐行对照检查以确保它们的输出完全相同如果输出不同输出结果也非常相似稍微调整一下你就可以使这段代码得到正确的输出

public static void main(String[] args) throws InterruptedException {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - i; j++) {
            System.out.print("* ");
        }
        for (int j = 0; j < i * 2; j++) {
            System.out.print("  ");
        }
        for (int j = 0; j < n - i; j++) {
            System.out.print(" *");
        }
        System.out.println();
    }
    for (int i = n; i >= 0; i--) {
        for (int j = n; j > i; j--) {
            System.out.print("* ");
        }
        for (int j = n; j > n - i * 2; j--) {
            System.out.print("  ");
        }
        for (int j = n; j > i; j--) {
            System.out.print(" *");
        }
        System.out.println();
    }
}
英文:

I believe this is a little simpler, however I did not do a side by side check to make sure they have the exact same output, if they do not the outputs are very similar and with a little tweaking you could make this code work for the exact correct output I believe.

public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0;i&lt;n;i++) {
for(int j = 0;j&lt;n-i;j++) {
System.out.print(&quot;* &quot;);
}
for(int j = 0;j&lt;i*2;j++) {
System.out.print(&quot;  &quot;);
}
for(int j = 0;j&lt;n-i;j++) {
System.out.print(&quot; *&quot;);
}
System.out.println();
}
for(int i = n;i&gt;=0;i--) {
for(int j = n;j&gt;i;j--) {
System.out.print(&quot;* &quot;);
}
for(int j = n;j&gt;n-i*2;j--) {
System.out.print(&quot;  &quot;);
}
for(int j = n;j&gt;i;j--) {
System.out.print(&quot; *&quot;);
}
System.out.println();
}

答案2

得分: 1

如果允许使用字符串/字符的中间数组在打印之前构建整个图案,可以按照以下方式完成:

  1. 创建整个场地并用左半部分和右半部分所需的图案填充它(使用标准的Arrays.fill方法)。
  2. 为场地的顶部和底部的半部分设置“空”单元格,计算空白空间的位置。
  3. 打印场地。

示例:

public static void printRhombPattern(int size, String leftPattern, String rightPattern, String empty) {
    int cols = 2 * size;
    int rows = cols + 1;

    String[][] field = new String[rows][cols];
    for (int i = 0; i < rows; i++) {
        field[i] = new String[cols];
        Arrays.fill(field[i], 0, size, leftPattern);
        Arrays.fill(field[i], size, cols, rightPattern);
    }

    int left = cols / 2 - 1;
    // 从 i == 1 开始,跳过第一行和最后一行
    for (int i = 1, right = left + 1; i <= size; i++, left--, right++) {

        for (int j = left; j <= right; j++) {
            field[i][j] = empty;             // 清除顶半部分的单元格
            field[rows - 1 - i][j] = empty;  // 清除底半部分的单元格
        }
    }

    // 主要打印
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            System.out.print(field[i][j]);
        }
        System.out.println();
    }
}

// 测试
public static void main(String[] args) {
    printRhombPattern(5, "* ", " *", "  ");
}

输出:

* * * * *  * * * * *
* * * *      * * * *
* * *          * * *
* *              * *
*                  *
*                  *
* *              * *
* * *          * * *
* * * *      * * * *
* * * * *  * * * * *
英文:

If it is allowed to use an intermediate array of Strings/chars to build the entire picture before printing it, it can be done in the following way:

  1. Create entire field and populate it with required patterns for the left and right halves (using standard Arrays.fill method).
  2. Set "empty" cells for top and bottom halves of the field, calculating the left and right position of the empty space.
  3. Print the field.

Example:

public static void printRhombPattern(int size, String leftPattern, String rightPattern, String empty) {
    int cols = 2 * size;
    int rows = cols + 1;

    String[][] field = new String[rows][cols];
    for (int i = 0; i &lt; rows; i++) {
        field[i] = new String[cols];
        Arrays.fill(field[i], 0, size, leftPattern);
        Arrays.fill(field[i], size, cols, rightPattern);
    }
        
    int left = cols / 2 - 1;
    // starting from i == 1 to skip the first and the last rows
    for (int i = 1, right = left + 1; i &lt;= size; i++, left--, right++) {
            
        for (int j = left; j &lt;= right; j++) {
            field[i][j] = empty;             // clean cells in the top half
            field[rows - 1 - i][j] = empty;  // clean cells in the bottom half
        }
    }

    // main printing
    for (int i = 0; i &lt; rows; i++) {
        for (int j = 0; j &lt; cols; j++) {
            System.out.print(field[i][j]);
        }
        System.out.println();
    }
}

// test
public static void main(String[] args) {
    printRhombPattern(5, &quot;* &quot;, &quot; *&quot;, &quot;  &quot;);
}

Output:

* * * * *  * * * * *
* * * *      * * * *
* * *          * * *
* *              * *
*                  *
*                  *
* *              * *
* * *          * * *
* * * *      * * * *
* * * * *  * * * * *

答案3

得分: 0

短回答:不。

如果您谈论时间复杂度,特定代码用于打印菱形空格的大O符号。

当前解决方案的时间复杂度为O(N^2),这是打印二维形状的最小复杂度。
此外,您可以减少一些常数,这不会影响您的时间复杂度。

如果您刚开始编程,以下资源可能会对您有所帮助:

https://github.com/joney000/Java-Competitive-Programming
英文:

Short Answer: No.

If you talk about the time complexity, the big-O of the specified code for printing the diamond whitespace.

 https://ideone.com/foelhX

The current solution is O(N^2) which is minimal to print 2-D shape.
moreover, you can reduce some constants, that would not impact your time complexity.
<br>
In case you are starting programming, this would be a helpful resource:

https://github.com/joney000/Java-Competitive-Programming

huangapple
  • 本文由 发表于 2020年9月27日 13:46:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64085228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定