(eden)Pascal triangle

题目名称

Pascal triangle

题目描述

By using two-dimensional array, write C program to display a table that represents a Pascal triangle of any size. In Pascal triangle, the first and the second rows are set to 1. Each element of the triangle (from the third row downward) is the sum of the element directly above it and the element to the left of the element directly above it. See the example Pascal triangle(size=5) below:

1        
1 1      
1 2 1    
1 3 3 1  
1 4 6 4 1

我的代码

“triangle.h”&"main.c"

 1 #include<stdio.h>
 2 int a[100][100] = {0};
 3 void printPascalTr(int size) {
 4     a[1][1] = 1;
 5     int i, j;
 6     if (size >= 1) printf("1\t\n");
 7     for (i = 2; i <= size; i++) {
 8         for (j = 1; j <= i; j++) {
 9             a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
10             printf("%d\t", a[i][j]);
11         }
12         printf("\n");
13     }
14 }
 1 #include <stdio.h>
 2 #include "triangle.h"
 3 void printPascalTr(int size);
 4
 5 int main() {
 6     int size;
 7     printf("Enter Pascal triangle size:");
 8     scanf("%d", &size);
 9     printPascalTr(size);
10     getchar();
11     return 0;
12 }
13  

标程代码

 1 #ifndef triangle_h
 2 #define triangle_h
 3 #include <stdio.h>
 4 void printPascalTr(int size) {
 5     int PascalTr[size][size];
 6     int row, col;
 7     // assign zero to every array element
 8     for (row = 0; row < size; row++)
 9         for (col = 0; col < size; col++)
10             PascalTr[row][col] = 0;
11     // first and second rows are set to 1s
12     PascalTr[0][0] = 1;
13     PascalTr[1][0] = 1;
14     PascalTr[1][1] = 1;
15     for (row = 2; row < size; row++) {
16         PascalTr[row][0] = 1;
17         for (col = 1; col <= row; col++) {
18             PascalTr[row][col] = PascalTr[row - 1][col - 1]
19             + PascalTr[row - 1][col];
20         }
21     }
22     // display the Pascal Triangle
23     for (row = 0; row < size; row++) {
24         for (col = 0; col <= row; col++) {
25             printf("%d\t", PascalTr[row][col]);
26         }
27         printf("\n");
28     }
29 }
30 #endif /* triangle_h */
31  

问题

主要是格式问题,题目中也给的不太清楚,同时自己不知道制表符“\t”是什么意思,感觉标程略复杂了点。

需要学习的地方

①制表符还有空格这些什么的;

②这种只打一个定义文件里面的还有很多不清楚,为什么在main.c里面已经有include"stdio.h"在子程序里面"triangle.h"还要打一次呢?还有变量是否公用这些问题,要去学习。

③标称里面这些define"",ifndef,endif是什么意思呢,要去学习一下。

时间: 2024-12-18 21:35:48

(eden)Pascal triangle的相关文章

杨辉三角(Pascal Triangle)的几种C语言实现及其复杂度分析

说明 本文给出杨辉三角的几种C语言实现,并简要分析典型方法的复杂度. 本文假定读者具备二项式定理.排列组合.求和等方面的数学知识. 一  基本概念 杨辉三角,又称贾宪三角.帕斯卡三角,是二项式系数在三角形中的一种几何排列.此处引用维基百科上的一张动态图以直观说明(原文链接http://zh.wikipedia.org/wiki/杨辉三角): 从上图可看出杨辉三角的几个显著特征: 1. 每行数值左右对称,且均为正整数. 2. 行数递增时,列数亦递增. 3. 除斜边上的1外,其余数值均等于其肩部两数

Pascal triangle 帕斯卡三角形 杨辉三角形 二项式定理

Pascal triangle 等段时间再扯pascal triangle- 先把一年多以前打印杨辉三角形的方法贴出来(简直不认直视,越来越不敢看以前自己写的东东了) C语言实现: /*************************************************************** Code writer : EOF Code date : 2013.05.16 e-mail : [email protected] Code description : Here is

leetcode-pascal triangle I&amp;&amp;II

对于第2个pascal triangle,通过观察可以发现,其实只需要2个额外的变量来记录,于是就设了个tmp数组. 整体有点DP问题中的滚动数组的感觉. 1 #include <vector> 2 #include <iostream> 3 using namespace std; 4 5 class Solution { 6 public: 7 vector<vector<int> > generate(int numRows) { 8 vector&l

GDUFE ACM-1020

The Rascal Triangle Time Limit: 1000ms Problem Description: The Rascal Triangle definition is similar to that of the Pascal Triangle. The rows are numbered from the top starting with 0. Each row n contains n+1 numbers indexed from 0 to n. Using R(n,m

compute Binomial Coefficient or combinations with dynamic programming

The ProblemWrite a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2. 1 def ComputeBinomialCoe

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

118. Pascal&#39;s Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 分析 第k层有k个元素,每层第一个及最后一个元素值为1,对于(k>2)层,第n(n>1 && n < k)个元素A[k][n] = A[k-1][n-1]+A[k-1][n];

119. Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 5 vect