简体   繁体   English

简单的C问题

[英]Simple C question

I just started to learn C, and one question in book that I'm using is: 我刚开始学习C,我在书中使用的一个问题是:
Use nested loops to produce the following pattern: 使用嵌套循环可产生以下模式:

$
$$
$$$
$$$$
$$$$$

And of course I got stuck. 当然,我被卡住了。

#include <stdio.h>
int main (void)
{
    int i, j;
    char ch = '$';
    for(i = 1; i < 5; i++)
    {
        for(j = 1; j <=5; j++)
           printf("%c", ch);
        printf("\n");
    }
    return 0;
}

The logic of what you need to do is pretty simple: you need to print 5 rows, where the i -th row has got i '$' . 您需要做的逻辑很简单:您需要打印5行,第i行的行中有i '$'

Pseudo code would look like this: 伪代码如下所示:

for any i from 1 to 5:
  print '$' times i
  print newline

print '$' times i could look like this: print '$' times i可能看起来像这样:

for any j from 1 to i:
  print '$'

It shouldn't be too hard to rewrite this using C syntax. 使用C语法重写它应该并不难。

Since answering this question with code is cheating, here are some hints: 由于用代码回答这个问题很容易,因此这里有一些提示:

  • For each line you're printing, you want to print a number of $ s equal to the line number. 对于您要打印的每一行,您要打印一个等于行号的$ s。
  • printf doesn't add a newline character unless you tell it to, so successive calls to printf can put characters on the same line. 除非您告知,否则printf不会添加换行符,因此连续调用printf可以将字符置于同一行。

If you have code that doesn't work, post it. 如果您的代码行不通,请将其发布。 We'll be happy to help you fix it. 我们很乐意帮助您解决问题。

Edit : Based on your sample code, you have some very small problems. 编辑 :根据您的示例代码,您有一些非常小的问题。

First, in your outer loop, you want a <= instead of a < . 首先,在外循环中,您需要<=而不是< That gets you up to 5. Second, in your inner loop, j <= 5 should be j <= i . 这样您就可以达到5。其次,在您的内部循环中, j <= 5应该是j <= i Though I would have written the inner loop with j starting at 0 and < i , that's just a stylistic preference. 虽然我会用0到< i开头的j来编写内部循环,但这只是一个样式偏好。

The printf("%c", ch) is equivalent to printf("$") too, in case you weren't sure. 如果不确定, printf("%c", ch)也等同于printf("$")

For reference, here's my first crack at an answer. 供参考,这是我的第一个答案。 It's very similar to yours: 与您的非常相似:

#include <stdio.h>

int main()
{
    int line, dollar;
    for (line=1; line <= 5; line++)
    {
        for (dollar = 0; dollar < line; dollar++)
        {
            printf ("$");
        }
        printf ("\n");
    }
    return 0;
}
#include <stdio.h>
int main (void)
{
int i, j;
char ch = '$';

for(i = 1; i < 5; i++)
{
   for(j = 1; j <=i; j++)
      printf("%c", ch);
   printf("\n");
}
 return 0;

}

description: first for loop is for printing the row.. and nested for loop is for no of '$' have to print 描述:第一个for循环用于打印该行..而嵌套的for循环用于不打印'$'

Please try this code, If you want to generalized ( means user can give any number for printing the pattern you can take a input from user). 请尝试使用此代码,如果要泛化(意味着用户可以提供任何编号来打印图案,则可以从用户那里输入)。 like n then replace for(row = 1; row <= 5; row++) to for(row = 1; row <= n; row++) . 像n然后将for(row = 1; row <= 5; row ++)替换为for(row = 1; row <= n; row ++)

#include<stdio.h>
    int main()
    {
       int row, col;
       for(row = 1; row <=5; row++)
       {
           for(col = 0; col < row; col++)
              printf("$");
           printf("\n");
       }
       return 0;
    }
  • Your outer loop needs to run from 1 to 5, it only runs from 1 to 4 as written. 您的外循环需要从1到5运行,它只按编写的从1到4运行。 for( i = 1; i <= 5; i++ )
  • The inner loop needs to run from 0 to i-1 (or 1 to i if you prefer). 内部循环需要从0到i-1 (如果愿意,则从1到i)运行。 for( j = 0; j < i; i++ )
  • The variable ch is redundant since it never changes, you can print the character directly using a literal string in the printf() call, or better a character constant with putchar() . 变量ch是冗余的,因为它永远不会更改,您可以在printf()调用中使用文字字符串直接打印字符,或者最好通过putchar()使用字符常量。
  • The best way to solve such simple problems is to step the code in your debugger and observe the code flow and how the variables change with each step. 解决此类简单问题的最佳方法是在调试器中逐步执行代码,并观察代码流以及变量在每一步中的变化情况。
#include <stdio.h>
int main (void)
{
    for (int i = 1; i <= 6; i++)
    {
        for (int j = 1; j <= 6; j++)
        {
            if (i - j >= 0)
                printf("$");
        }
        printf("\n");
    }
 return 0;
}

it was simple,why because,in it you just want to print in the form of rows and columns and that should be in increasing order. 这很简单,为什么,因为您只想以行和列的形式打印,并且应该以递增的顺序打印。 in the first for loop,you are going to print rows and with included for loop you need to do it for columns. 在第一个for循环中,您将要打印行,而其中包含for循环,则需要对列进行打印。

First some basic about loops is that - When outer loop execute one time then inner loop will complete its whole iteration. 首先,关于循环的一些基本知识是-当外循环执行一次时,内循环将完成其整个迭代。 in your case - for(i = 1; i < 5; i++) // outer loop for each changing value of i such as i= 1,2,3,4 Inner loop for(j = 1; j <=5; j++) // will complete its whole iteration (ie 5 times because you are using j=1 to j<=5. 在您的情况下-for(i = 1; i <5; i ++)// i的每个变化值的外循环,例如i = 1,2,3,4内循环for(j = 1; j <= 5; j ++)//将完成整个迭代(即5次,因为您使用的是j = 1到j <= 5。

Now the come to your problem with your question is that- 现在,您遇到的问题是-

for(i = 1; i < 5; i++) //here is problem this will run only 4 time because i<5, and you require 5 times as according to your output given,replace it with i<=5 
{
    for(j = 1; j <=5; j++) //here is also because you are using j<=5, as I mention above it will run 5 times for each value of i (in outer loop),so replace j<=5 by j<=i, because for each change value of i, you require same time execution of inner loop to print the value of "ch" variable) 
       printf("%c", ch);
    printf("\n");
}

so here is modified code 所以这里是修改后的代码

int main() { int main(){

// your code goes here
int i, j;
char ch = '$';
for(i = 1; i < =5; i++) // outer loop
{
    for(j = 1; j <=i; j++) // inner loop
       printf("%c", ch);
    printf("\n"); // to move on next line after the complition of inner loop 
}
return 0;

} }

May be this is helpful for you. 可能对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM