打印下面提到的数字模式的逻辑是什么

时间:2014-10-25 07:40:35

标签: java numbers logic

class Num {
    public static void main(String[] args) {
        for(int i=1;i<=5;i++) {
            for(int j=1;j<=i;j++) {
                if(j==1) {
                    System.out.print(i);
                }
                else if(j==2) {
                    System.out.print(" "+(i+j+2));
                }
                else {
                    System.out.print(" "+(i+j+4));
                }
            }
            System.out.println(" ");
        }
    }
}

输出:

1
2 6
3 7 10 
4 8 11 12
5 9 12 13 14

预期:

1
2 6
3 7 10 
4 8 11 13
5 9 12 14 15

我尝试了很多,并提出了这个逻辑

when j=1 then i
when j=2 then i+j+2
when j=3 then i+j+4
when j>=4 then i+j+5

这里共有4个条件,我如何在嵌套for循环中做到这一点。任何其他逻辑也很明显。

5 个答案:

答案 0 :(得分:2)

int lineCount = 5;
for (int i = 1; i <= lineCount; i++) {
    int value = i;
    for (int j = 1; j <= i; j++) {
        System.out.print(value +  " ");
        value += lineCount -j;
    }
    System.out.println("");
}

答案 1 :(得分:0)

你忘了你的j = 3案件

for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                if (j == 1) {
                    System.out.print(" " + i);
                } else if (j == 2) {
                    System.out.print(" " + (i + j + 2));
                } 
                else if(j == 3){
                    System.out.print(" " + (i + j + 4));
                }
                else {
                    System.out.print(" " + (i + j + 5));
                }
            }
        }

答案 2 :(得分:0)

我建议并相信我们的代码最好是动态的,这意味着它可以在user的任何输入上工作。如果问题是从5行更改为6行,则必须将for构造更改为好吧添加一个额外的if结构。 其次,如果构造符合你需要的列,那么保持尽可能多的数据并不是一个好主意。

  Take the no. of rows the user wants to a variable say 'noofrows'. Then its just the same as your code with little changes:
        int noofrows;
    Scanner s=new Scanner(System.in);
    System.out.println("\nGive the no. of rows needed:\n");
    noofrows=(int)s.nextInt();
    System.out.println("Number give is:"+noofrows);
    int emptyspaces;
     for(int i = 1; i <= noofrows; i++) 
        {
        for (int j = 1; j <= i; j++) 
        {
            emptyspaces=(j)*(j-1)/2;   //counting the no. of emptspace which is an arithmetic progression 
        System.out.print(((j-1)*noofrows+i)-emptyspaces +" ");
        }
        System.out.print("\n");
       s.close();  //edited version

如果出现任何问题,请告诉我

答案 3 :(得分:0)

我不是java开发人员,所以在C#中发送解决方案。您将不得不替换“使用系统;”使用一些带有相应System.out.print或println的import语句和Console.WriteLine语句。

using System;

class Program
{
    static void Main()
    {
        PrintNumbers(1, 5); 
    }

    static void PrintNumbers(int level, int MaxLevel)
    {
        if( MaxLevel < level )
        {
            return;
        }

        int num = 0;
        for(int i = 0, diff=MaxLevel; i < level; i++)
        {
          num += (i == 0)?level:(diff-i);
          Console.Write("{0} ",num);
        }
        Console.WriteLine("");
        PrintNumbers(level+1, MaxLevel);
    }
}

答案 4 :(得分:0)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n=5;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            if(j==1)
                cout<<i<<" ";
            else
                cout<<i+j+j<<" ";
        }
        cout<<endl;
    }
}

/*OUTPUT :-

1 
2 6 
3 7 9 
4 8 10 12 
5 9 11 13 15 

*/