循环算法

8
我该如何制作这个内容:
*******
-*****-
--***--
---*---
--***--
-*****-
*******

以下是我编写的代码,旨在实现上述功能,但实际效果并不符合预期。
    public static void stars(/*int jmlBaris*/) {
    for ( int i = 7; i >= 1; i-=2) {
        for (int j = 1; j <= i; j++) {

            System.out.print("*");
        }
        System.out.println("");
    }

    for (int i = 1; i <= 7; i+=2) {
        for (int j = 1; j <= i; j++){
            System.out.print("*");
            }
        System.out.println("");
    }
}
public static void main(String[] args) {
    stars();
}
}

由于这不再被标记为C++,我删除了我的解决方案。如果您将函数分解,将会很有帮助。有时候,一个大函数会让问题难以解决。此外,请考虑使用递归解决方案。 - andre
8个回答

9
这是我可能会写的方式。
// three loops
public static void stars(int size) {
    for (int y = 0; y < size; y++) {
        for (int i = 0; i < y && i < size - y - 1; i++)
            System.out.print(' ');
        for (int i = Math.min(y, size - y - 1); i < Math.max(y + 1, size - y); i++)
            System.out.print('*');
        System.out.println();
    }
}

或者

// two loops
public static void stars(int size) {
    for (int y = 0; y < size; y++) {
        for (int x = 0; x < size; x++)
            System.out.print(
                    (x >= y && x < size - y) ||
                            (x >= size - y - 1 && x <= y) ? '*' : ' ');
        System.out.println();
    }
}

或者

// one loop
public static void stars(int size) {
    for (int i = 0; i < size * size; i++) {
        int y = i / size, x = i % size;
        System.out.print(
                (x >= y && x < size - y) ||
                        (x >= size - y - 1 && x <= y) ? '*' : ' ');
        if (x == size - 1)
            System.out.println();
    }
}

注意:无论使用一,两个还是三个循环,时间复杂度均为O(N^2)。一个简单的方法确定这一点是生成的星号数量为O(N^2),不管怎样生成。

你的意思是你在编译/运行它时遇到了困难吗?如果你给出大小为7,它会打印出预期的输出。 - Peter Lawrey
是的,正在运行它,已经创建了psvm,但它显示错误类型=星号。 - alexkirkland
1
我无法想象你做错了什么。这是一个带有一个参数的简单静态方法调用。在你的主函数中输入 stars(7); 即可。 - Peter Lawrey
想象一下你在纸上用 \ 和 / 画一个 X。其中 y 代表 \,而 size - y - 1 代表 /。你希望 X 从这些值中的最小值(min)开始,以最大值(max)结束。 - Peter Lawrey
关于那个 Math 的东西。它是不可更改的吗? - alexkirkland
显示剩余3条评论

4
我会使用子字符串来完成类似这样的操作。
String a = "*******";  //7 stars
String blank = "        "; //7 spaces
int j = 7;
for (int i = 0; i < 7; i++) {
    if (i > j){
        System.out.print(blank.substring(0,i));
        System.out.println(a.substring(i,j));
        }
    else{
        System.out.print(blank.substring(0,j));
        System.out.println(a.substring(j,i));
        }
    j--;
}
System.out.println(a);

之前的编辑方式不起作用,已经进行修改。

这个可以使用。


1
首先,这并不是很通用化的,因为您硬编码了这些值。 - Rahul Thakur
True...一个变量可以作为整数传递给方法,用于生成String a的长度。然后该变量可以在for循环语句中用于i和j的边界。 - Clark Kent
1
这并不是很通用,首先你硬编码了值,而且在这里使用子字符串是完全不必要的,会增加额外的开销。这是一个非常简单的学校级程序,可以使用循环完成。 - Rahul Thakur
我认为这样一个简单的程序不需要使用子字符串和它的开销 - 它可以更容易地通过循环和字符来完成。 - ihsoy ih
@alexkirkland 子字符串是来自另一个字符串的字符串。例如,对于字符串“Hello world!”,如果我们取子字符串0,5(意思是从字符串的第0个位置取5个字符),我们将得到一个返回的子字符串:“Hello”。更多信息请参见:http://docs.oracle.com/javase/6/docs/api/java/lang/String.html - ihsoy ih

2

你可以尝试像我在IDEOne上编译的这段代码一样(尽管它似乎可行): http://ideone.com/9xZ1YB

class Main
{
    public static void main(String[] args)
    {
        stars();
    }

    static void stars()
    {
        final int MAX_WIDTH = 7;

        for (int i = 0; i < 7; ++i)
        {
            int width;

            if (i < 3) width = MAX_WIDTH - i * 2;
            else if (i > 3) width = (i - 3) * 2 + 1;
            else width = 1;

            // Before spaces

            for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j)
            {
                System.out.print(" ");
            }

            // Stars

            for (int j = 0; j < width; ++j)
            {
                System.out.print("*");
            }

            // After spaces

            for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j)
            {
                System.out.print(" ");
            }

            System.out.println();
        }
    }
}

2
那实际上是打印一个新行 :) 或许应该使用 print 调用。 - effeffe
@alexkirkland 这是因为当它垂直打印时,空格对您来说是不可见的。 - ihsoy ih
抱歉,我完全误解了这篇文章。我会编辑我的回答。 - ihsoy ih
1
哦,好的。i < 3 意味着中间星号之前的部分,i > 3 是中间星号之后的部分,而任何其他情况下,i == 3 只有1个宽度。 - ihsoy ih
让我们在聊天中继续这个讨论 - alexkirkland
显示剩余4条评论

1
对于算法初学者,我建议您将结构分解为子部分,然后尝试解决模式。
对于这个特定的模式,它可以分解成几个三角形。每个三角形都可以通过不同的for循环来解决,如下图所示。

dividing the pattern into substructures

public static void printPattern(int num) {
    // this loop generates first 4 lines
    for (int i = 0; i < num / 2 + 1; i++) {
        // draws the red triangle of '-'
        for (int j = 0; j < i; j++) {
            System.out.print("-");
        }
        // draws the green triangle of '*'
        for (int j = i; j < num / 2 + 1; j++) {
            System.out.print("*");
        }
        // draws the blue triangle of '*'
        for (int j = i + 1; j < num / 2 + 1; j++) {
            System.out.print("*");
        }
        // draws the orange triangle of '-'
        for (int j = 0; j < i; j++) {
            System.out.print("-");
        }
        System.out.println();
    }

    /* this loop generates last 3 lines */
    for (int i = 0; i < num / 2; i++) {
        // draws the green triangle of '-'
        for (int j = i + 1; j < num / 2; j++) {
            System.out.print("-");
        }
        // draws the red triangle of '*'
        for (int j = 0; j < i + 2; j++) {
            System.out.print("*");
        }
        // draws the orange triangle of '*'
        for (int j = 0; j < i + 1; j++) {
            System.out.print("*");
        }
        // draws the blue triangle of '-'
        for (int j = i + 1; j < num / 2; j++) {
            System.out.print("-");
        }
        System.out.println();
    }
}

使用类似的技术,您可以生成任何模式。


0

如果我理解得正确,你的问题是如何在第2-7行中打印缩进。

假设使用字符“x”代替星号符号,并用“-”代替空格的相同问题。然后您需要绘制

xxxxxxx
-xxxxx-
--xxx--
---x---
--xxx--
-xxxxx-
xxxxxxx

这意味着你应该在第一、第二和第三个字符串中,在星号前输出0、1、2个空格。我留下了细节让你自己去解决。


我怀疑行末的空格是可选的。 - Peter Lawrey
是的,必须是这样的,我对第四行中的'-'以及如何使x变为1感到困惑。 - alexkirkland
@PeterLawrey,当然!我只是为了对称性而包含它。 - Artem Sobolev
2
@alexkirkland,你可以数每行中“-”的数量并找到一个公式。我建议你自己做这件事。对于算法初学者来说,这是一个很好的脑力挑战。 - Artem Sobolev
@Barmaley.exe,是的,我必须自己做,这就是为什么我要求有人解释如何使用循环制作“-”符号。 - alexkirkland

0
    public static void stars(/*int jmlBaris*/){
    String starstr = "*";
    String blank = "_";
    int spaceBlank;;
    for(int i=7; i>=1;i-=2){
        spaceBlank = (7-i)*.5;
        String starrep = StringUtils.repeat(starstr, i);
        String blankrep = StrinUtils.repeat(blank, spacesBlank);
        system.out.println(blankrep + starrep + blankrep);
    }
    for(int j=3 j<=7; j+=2){
        spaceBlank = (7-j)*.5;
        starrep = StringUtils.repeat(starstr, j);
         String blankrep = StrinUtils.repeat(blank, spacesBlank);
        system.out.println(blankrep + starrep  + blankrep);
    }
}    
public static void main(String[] args){
    stars();
}

谢谢,我不知道那个网站,我一定会用它的,但是我的雇主阻止了所有有用的东西。我不能从这里访问那个网站。 - PatrickW

0

你的代码中缺少一些空格。我不在意正确的空格,谁会注意到呢?但左侧的空格非常重要!

试试这个:

public static void stars(/*int jmlBaris*/) {
    for ( int i = 7; i >= 1; i-=2) {
        for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */
            System.out.print(" "); /* Missing Here */
        } /* Missing Here */

        for (int j = 1; j <= i; j++) {
            System.out.print("*");
        }

        System.out.println("");
    }

    for (int i = 1; i <= 7; i+=2) {
        for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */
            System.out.print(" "); /* Missing Here */
        } /* Missing Here */

        for (int j = 1; j <= i; j++){
            System.out.print("*");
        }

        System.out.println("");
    }
}

0
    int N = 7;
    for (int y=0; y<N; y++)
    {
        for (int x=0; x<N; x++)
            System.out.print( (y-x)*(N-y-x-1)<=0 ? '*' : '-');
        System.out.println();
    }

或者,更对称地说,

    int n = 3;
    for (int y=-n; y<=n; y++)
    {
        for (int x=-n; x<=n; x++)
            System.out.print( y*y>=x*x ? '*' : '-');
        System.out.println();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接