簡體   English   中英

如何產生以下輸出?

[英]How to produce the following output?

由於我是編程領域的新手,因此為這種模式編寫程序時面臨的問題很少。我嘗試了很多次,但結果不是我想要的? 模式是:

 1 
 23 
 456 
 78910 

我寫的是:

#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
    int num = 1 , j = 1 , x = 1 , i = 1 ; 
    while( j <= 4 ) { 
        while( i <= num ) { 
            printf( "%d", x ) ; 
            x++ ; 
            i++ ; 
        } 
        num++ ; 
        i = ( i + 1 ) - num ; 
        j++ ; 
    } 
    getch() ; 
} 
#include <stdio.h>

int main()
{
    printf("1\n23\n456\n78910\n");
    return 0;
}

產生您想要的輸出

您需要在內循環之后打印換行符:

#include<stdio.h> 
#include<conio.h>
int main() 
{ 
    int num = 1 , j = 1 , x = 1 , i = 1 ; 
    while( j <= 4 ) { 
        while( i <= num ) { 
            printf( "%d", x ) ; 
            x++ ; 
            i++ ; 
        } 
        printf("\n");
        num++ ; 
        i = ( i + 1 ) - num ; 
        j++ ; 
    } 
    getch();
    return(0);
} 

還有另一個例子:

int main()
{
    int i, j, num = 1, line = 4;
    for(i = 1; i <=  line ; i++)
    {
        for(j = 0; j < i; j++)
        {
            printf("%d", num);
            num++;
        }
        printf("\n");
    }
    return 0;
}

單循環:

# include<stdio.h>
# define LIMIT 100

int main(){
int i, prev=0, next=0, diff=1;
for(i=1;i<LIMIT;i++){
    printf("%d", i);next++;
    if(diff == next-prev){
        printf("\n");
        diff++;prev = next = 0;
    }
}
}

暫無
暫無

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

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