使用single for loop在java中打印三角形

使用single for loop在java中打印三角形,java,algorithm,Java,Algorithm,我必须用单for循环打印三角形 我尝试过使用两个for循环,我成功地做到了,但我必须使用单for循环来解决它 试试这个 1 2 3 4 5 6 公共类金字塔7floyds{ 公共静态void main(字符串[]args){ int nextNumber=1; 对于(inti=1;ipublic类HelloWorld{ 公共静态void main(字符串[]args){ int j=1; 对于(int i=1;i而言,诀窍在于当前行的最后一个数是前一行最后一个数和该行的数之和 换句话说:las

我必须用单for循环打印三角形

我尝试过使用两个for循环,我成功地做到了,但我必须使用单for循环来解决它

试试这个

1
2 3
4 5 6
公共类金字塔7floyds{
公共静态void main(字符串[]args){
int nextNumber=1;
对于(inti=1;i
public类HelloWorld{
公共静态void main(字符串[]args){
int j=1;

对于(int i=1;i而言,诀窍在于当前行的最后一个数前一行最后一个数和该行的数之和

换句话说:
lastNum=prevlastnume+rowNum

public class HelloWorld{

     public static void main(String []args){
        int j=1;
        for(int i=1;i<=6;i++){
            System.out.print(i+" ");
            if (i==j){
                System.out.print('\n');
                j=2*j+1;
            }
        }
     }
}

为什么需要在单循环中执行此操作?即使使用一个循环或多个循环执行相同的操作,复杂性仍然相同!!
public class HelloWorld{

     public static void main(String []args){
        int j=1;
        for(int i=1;i<=6;i++){
            System.out.print(i+" ");
            if (i==j){
                System.out.print('\n');
                j=2*j+1;
            }
        }
     }
}
int row = 1;
int last = 0;
for (int i = 1; i < 37; i++) {
    if (i < (row + last)) {
        System.out.print(i + " ");
    } else {
        System.out.print(i + "\n");
        row++;
        last = i;
    }
}
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36