C Exercise Example 9
Topic: Request to output a chess board.
Program analysis: The chess board consists of 64 black and white grids, divided into 8 rows and 8 columns. Use i to control the rows, j to control the columns, and control whether to output black squares or white squares according to the change of the sum of i+j.
Program source code:
#include<stdio.h>
int main()
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
if((i+j)%2==0)
printf("%c%c",219,219);
else printf(" ");
printf("\n");
}
return 0;
}
The output result of the above example is:
C Exercise Example 10
Topic: Classical problem (rabbits give birth to babies): There is a pair of rabbits, and they give birth to a pair of rabbits every month from the third month after birth. After the rabbits grow to the third month, they give birth to a pair of rabbits every month. If Even if the rabbits don't die, what is the total number of rabbits every month? (40 months before export is sufficient)
Program analysis: The pattern of rabbits is the sequence 1,1,2,3,5,8,13,21...., that is, the next month is the sum of the previous two months (starting from the third month).
Program source code:
#include<stdio.h>
int main()
{
int f1=1,f2=1,i;
for(i=1;i<=20;i++)
{
printf("%12d%12d",f1,f2);
if(i%2==0) printf("\n");
f1=f1+f2;
f2=f1+f2;
}
return 0;
}
The output result of the above example is:
1 1 2 3
5 8 13 21
34 55 89 144
233 377 610 987
1597 2584 4181 6765
10946 17711 28657 46368
75025 121393 196418 317811
514229 832040 1346269 2178309
3524578 5702887 9227465 14930352
24157817 39088169 63245986 102334155