zoukankan      html  css  js  c++  java
  • (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	
    ");
     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	", a[i][j]);
    11         }
    12         printf("
    ");
    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	", PascalTr[row][col]);
    26         }
    27         printf("
    ");
    28     }
    29 }
    30 #endif /* triangle_h */
    31  

    问题

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

    需要学习的地方

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

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

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

  • 相关阅读:
    什么是惯性释放
    hyperworks2019x中模型简化
    optistruct如何将多个约束置于一个约束集合中
    optistruct对称约束设置
    optistruct非线性分析步子步设置
    optistruct怎么调用多核
    ConcurrentHashMap中节点数目并发统计的实现原理
    K:leetcode 5381.查询带键的排列 这题简单,但我还能优化。精益求精,才是算法的乐趣所在!
    K:缓存相关问题
    K:剑指offer-56 题解 谁说数字电路的知识不能用到算法中?从次数统计到逻辑表达式的推导,一文包你全懂
  • 原文地址:https://www.cnblogs.com/iamxiaoyubei/p/5039858.html
Copyright © 2011-2022 走看看