当前位置:网站首页>Experiment 7-2-6 print Yanghui triangle (20 points)

Experiment 7-2-6 print Yanghui triangle (20 points)

2022-06-12 21:24:00 YYY speaker

Print Yanghui triangle

This question is required to be printed according to the specified format N Go Yanghui triangle .( Difficulty four stars )
Ideas :
Use a two-dimensional array

Code :

#include<stdio.h>

int main(){
    
	
	
	int a[10][10];// Use a two-dimensional array  
	
	int n;
	
	scanf("%d",&n);
	
	for(int i=0;i<n;i++){
    // peripheral  
		
		
		a[i][0]=1;
		a[i][i]=1;
		
		
				}
				
					
	
	for(int i=2;i<n;i++){
    
	
		for(int j=1;j<i;j++){
    
			
			a[i][j]=a[i-1][j-1]+a[i-1][j]; 
			// middle  
			
			
					
		}
	}
		
		
	for(int i=0;i<n;i++){
    
		
		
		for(int j=i+1;j<n;j++)
		printf(" ");
	
	for(int j=0;j<=i;j++)
	{
    
		
		
		printf("%4d",a[i][j]); 
		
		
		//printf("\t");
	}
	
	printf("\n");
	
	
	
	
	 }
	

	
	return 0;
} 

result :

 Insert picture description here

原网站

版权声明
本文为[YYY speaker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281407464388.html