【问题标题】:You have to print all the numbers in the range from1 to N*N, such that there are exactly N numbers on each line您必须打印从 1 到 N*N 范围内的所有数字,这样每行正好有 N 个数字
【发布时间】:2022-08-06 03:06:49
【问题描述】:

您必须打印从 1 到 N*N 范围内的所有数字,这样每行正好有 N 个数字。 例如,如果 N = 3 中存储的值,则需要打印范围内的所有数字,从 [1,9] 开始,每行有 3 个数字。因此,所需的输出是

输出 1 2 3 4 5 6 7 8 9

    标签: loops nested


    【解决方案1】:
       function patternOfN(N){
        let a = 1;
        for (let i = 1; i <= N; i++) {
          let main = "";
          let prood = N * i;
          for (j = a; j <= prood; j++) {
            main = main + j + " ";
          }
          a = prood + 1;
          console.log(main);
        }
        }
        patternOfN(3);
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:
    let N=3;
    
    for(let i = 1; i<=N*N; i=i+N ){
      let bag = "";
      for(let j = I ; j< i + N; j++){
        bag+= j+" ";
      }
      console.log(bag);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多