【问题标题】:C: alternatives for loopsC:循环的替代品
【发布时间】:2013-10-07 20:21:24
【问题描述】:

我需要创建一个空心矩形,但我只能使用一个循环。该程序按原样工作,但我在代码中使用了两个循环,不知道如何继续减少最后一个循环。 (我们只学习了 printf、scanf、if/else 和循环,所以没有数组等) 程序扫描框架的高度、宽度和厚度。

谁能给我指出正确的方法?

代码如下:

row = 0;
while(row < height)
{
    column = 0;
    while(column < width)
    {
        if(thickness > row)    // upper border
            { printf("*");};
        if( some conditions )  // left border
            { printf("*");};
        if( conditions )    // hollow
            { printf(" ");};
        if( conditions )   // right border
            { printf("*");};
        if( conditions )     // bottom border
            { printf("*");};

        column++;
    };

puts("");
row++;
};

【问题讨论】:

  • 为什么if 的每个} 后面都多了一个分号?
  • @hacks,它们就在那里,语言不需要,我想他只是把它们放进去。
  • 任何给定的 mxn "grid" 都可以被认为是一个单独的字符串,即 mxn,因为您使用的是 printf,我建议将每一行视为一个字符串块。字符串的第一部分和最后一部分都是“”,而中间部分大部分是“”......所以实际上你可以只使用 2 个字符串“*****”和“ * " 并检查它是否是第一行/最后一行。
  • 顺便说一下,如果您将发布您的完整代码,那么它将帮助我们回答。
  • 你能使用AND、OR等逻辑运算符吗?

标签: c loops


【解决方案1】:

这里有个线索:在 0...m 循环中执行 0...n 循环与执行 0...(n*m) 循环相同。您可以使用除法和模数来计算行和列。

【讨论】:

    【解决方案2】:

    仅当您完全被卡住或想看到不同的解决方案时才阅读它。
    如您所见,没有扫描输入。

    #include <stdio.h>
    
    int main(void)
    {
      int width=5;
      int height=6;
      int thick=1;
      int x=1;
      int y=height;
    
      while(y>0)
      {
        if(y>(height-thick) || y<=thick || x<=(thick) || x>(width-thick))
          printf("*");
        else
          printf(" ");
        if(x==width)
        {
          x=1;
          printf("\n");
          y--;
        }
        else
        {
          x++;
        }
      }
      return 0;
    }
    

    【讨论】:

      【解决方案3】:

      使用下面的代码你可以print frame 使用1loop+ if-else 和迭代次数是2*column+width-2

          int i, column = 6,width=5;
      
              for(i=1;i<=2*column+(width-2);i++) 
              {
                     if( i <= column || i-column>=width-1) 
                     printf("* ");
                     else    
                     printf("\n*%*s\n",2*(column-1),"*");  // prints newline and `*` then Width of 2*(colomn-1) times space and  again * and newline.  
                     //if you don't want newline two times, remove trailing one add if statement inside else check i==column+width-2 print newline. 
              };  
      

      广义的。

      #include <stdio.h>
      
      int main(void) {
      int i, column ,width;
      printf("Enter two");
      scanf("%d%d",&column,&width);
          for(i=1;i<=2*column+(width-2);i++)
          {
                 if(i <= column || i-column>=width-1)
                 printf("*");
                 else
                  {
                  printf("\n*%*s",(column-1),"*");
                  if (i-column==width-2)
                  printf("\n");
                  }
          };
      
          printf("\n");
              return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-04-22
        • 1970-01-01
        • 1970-01-01
        • 2013-09-11
        • 2018-04-22
        • 1970-01-01
        • 2016-09-27
        • 2020-03-21
        • 2010-12-10
        相关资源
        最近更新 更多