# 一、程序控制结构

## 1、输出斐波拉契数

• 源代码
``````#include "stdio.h"

int main()
{
int i, a, b, c, f[40];

printf("方法一：利用简单变量进行迭代n");
a = 1;
b = 1;
printf("%-8d %-8d ", a, b);

for (i = 3; i <= 40; i++)
{
c = a + b;
printf("%-8d ", c);
a = b;
b = c;
if (i % 5 == 0)
printf("n");
}

printf("nn方法二：利用一维数组进行递推n");

f[0] = 1;
f[1] = 1;
for (i = 2; i < 40; i++)
{
f[i] = f[i - 1] + f[i - 2];
}

for (i = 0; i < 40; i++)
{
printf("%-8d", f[i]);
if ((i + 1) % 5 == 0)
printf("n");
}
return 0;
}
``````
• 运行结果

## 2、打印杨辉三角形

• 源代码
``````#include "stdio.h"
int main()
{
int i, j, k, m[14][14];

for (i = 0; i < 14; i++)
{
for (j = 0; j < 15; j++)
{
m[i][j] = 1;
}
}

for (i = 2; i < 14; i++)
{
for (j = 1; j < i; j++)
{
m[i][j] = m[i - 1][j - 1] + m[i - 1][j];
}
}

for (i = 0; i < 14; i++)
{
for (k = 1; k < 40 - 3 * i; k++)
{
printf(" ");
}

for (j = 0; j <= i; j++)
{
printf("%-5d ", m[i][j]);
}
printf("n");
}
return 0;
}
``````
• 运行结果

## 3、打印乘法九九表

• 源代码
``````#include <stdio.h>

int main()
{
int i, j;

for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d×%d=%-2d ", i, j, i * j);
}
printf("n");
}
}
``````
• 运行结果

## 4、百钱买百鸡问题

• 源代码
``````#include "stdio.h"
int main()
{

int cock, hen, chick, count = 0;

for (cock = 0; cock <= 20; cock++)
{
for (hen = 0; hen <= 34; hen++)
{
for (chick = 0; chick <= 100; chick++)
{
if (cock + hen + chick == 100 &&
cock * 5 + hen * 3 + chick / 3.0 == 100)
{
count++;
printf("cock = %-2d, hen = %-2d, chick = %-2d n", cock, hen, chick);
}
}
}
}
printf("百钱买百鸡购买方案数：%dn", count);
}
``````
• 运行结果

THE END