杨辉三角形.c 641 B

123456789101112131415161718192021222324252627282930
  1. #include <stdio.h>
  2. int main() {
  3. int n, i, j;
  4. scanf("%d", &n);
  5. int triangle[n + 1][n + 1];
  6. for (i = 0; i <= n; i++) {
  7. for (j = 0; j <= i; j++) {
  8. if (j == 0 || j == i)
  9. triangle[i][j] = 1;
  10. else
  11. triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
  12. }
  13. }
  14. int max_width = 4 * triangle[n][n];
  15. for (i = 0; i <= n; i++) {
  16. for (j = 0; j < n - i; j++)
  17. printf("%*s", max_width + 1, "");
  18. for (j = 0; j <= i; j++)
  19. printf("%*d", max_width, triangle[i][j]);
  20. printf("\n");
  21. }
  22. return 0;
  23. }