簡體   English   中英

簡單的C問題

[英]Simple C question

我剛開始學習C,我在書中使用的一個問題是:
使用嵌套循環可產生以下模式:

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

當然,我被卡住了。

#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;
}

您需要做的邏輯很簡單:您需要打印5行,第i行的行中有i '$'

偽代碼如下所示:

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

print '$' times i可能看起來像這樣:

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

使用C語法重寫它應該並不難。

由於用代碼回答這個問題很容易,因此這里有一些提示:

  • 對於您要打印的每一行,您要打印一個等於行號的$ s。
  • 除非您告知,否則printf不會添加換行符,因此連續調用printf可以將字符置於同一行。

如果您的代碼行不通,請將其發布。 我們很樂意幫助您解決問題。

編輯 :根據您的示例代碼,您有一些非常小的問題。

首先,在外循環中,您需要<=而不是< 這樣您就可以達到5。其次,在您的內部循環中, j <= 5應該是j <= i 雖然我會用0到< i開頭的j來編寫內部循環,但這只是一個樣式偏好。

如果不確定, printf("%c", ch)也等同於printf("$")

供參考,這是我的第一個答案。 與您的非常相似:

#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;

}

描述:第一個for循環用於打印該行..而嵌套的for循環用於不打印'$'

請嘗試使用此代碼,如果要泛化(意味着用戶可以提供任何編號來打印圖案,則可以從用戶那里輸入)。 像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;
    }
  • 您的外循環需要從1到5運行,它只按編寫的從1到4運行。 for( i = 1; i <= 5; i++ )
  • 內部循環需要從0到i-1 (如果願意,則從1到i)運行。 for( j = 0; j < i; i++ )
  • 變量ch是冗余的,因為它永遠不會更改,您可以在printf()調用中使用文字字符串直接打印字符,或者最好通過putchar()使用字符常量。
  • 解決此類簡單問題的最佳方法是在調試器中逐步執行代碼,並觀察代碼流以及變量在每一步中的變化情況。
#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;
}

這很簡單,為什么,因為您只想以行和列的形式打印,並且應該以遞增的順序打印。 在第一個for循環中,您將要打印行,而其中包含for循環,則需要對列進行打印。

首先,關於循環的一些基本知識是-當外循環執行一次時,內循環將完成其整個迭代。 在您的情況下-for(i = 1; i <5; i ++)// i的每個變化值的外循環,例如i = 1,2,3,4內循環for(j = 1; j <= 5; j ++)//將完成整個迭代(即5次,因為您使用的是j = 1到j <= 5。

現在,您遇到的問題是-

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");
}

所以這里是修改后的代碼

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;

}

可能對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM