[Program 21]
Problem: Monkey Eating Peach Problem: A monkey picks several peaches on the first day, eats half of them immediately, and then eats one more.
On the second morning, it again eats half of the remaining peaches and one more. Every morning thereafter, it eats half of what was left from the previous day plus one more. On the morning of the tenth day, when it wants to eat again, it finds only one peach left. Find out how many peaches were picked on the first day.
1. Program Analysis: Use reverse thinking to deduce from the end to the beginning.
2. Program Source Code:
main()
{
int day, x1, x2;
day = 9;
x2 = 1;
while(day > 0)
{
x1 = (x2 + 1) * 2; /* The number of peaches on the first day is twice the number of peaches on the second day plus one */
x2 = x1;
day--;
}
printf("the total is %d\n", x1);
}
==============================================================
[Program 22]
Problem: Two table tennis teams compete, each sending three players. Team A consists of players a, b, c, and Team B consists of players x, y, z. The match list has been determined by drawing lots. Someone asks the players about the match list. Player a says he will not compete against player x, player c says he will not compete against players x and z. Please write a program to find out the match list of the three players.
1. Program Analysis: The method to determine prime numbers: Use a number to divide from 2 to the square root of that number. If it can be divided evenly, then the number is not prime, otherwise it is prime.
2. Program Source Code:
main()
{
char i, j, k; /* i is a's opponent, j is b's opponent, k is c's opponent */
for(i = 'x'; i <= 'z'; i++)
for(j = 'x'; j <= 'z'; j++)
{
if(i != j)
for(k = 'x'; k <= 'z'; k++)
{
if(i != k && j != k)
{
if(i != 'x' && k != 'x' && k != 'z')
printf("order is a--%c\tb--%c\tc--%c\n", i, j, k);
}
}
}
}
==============================================================
[Program 23]
Problem: Print the following pattern (diamond)
*
*
1. Program Analysis: First, divide the pattern into two parts. The first four rows have one pattern, and the last three rows have another pattern. Use a double for loop, the first layer controls the rows, and the second layer controls the columns.
2. Program Source Code:
main()
{
int i, j, k;
for(i = 0; i <= 3; i++)
{
for(j = 0; j <= 2 - i; j++)
printf(" ");
for(k = 0; k <= 2 * i; k++)
printf("*");
printf("\n");
}
for(i = 0; i <= 2; i++)
{
for(j = 0; j <= i; j++)
printf(" ");
for(k = 0; k <= 4 - 2 * i; k++)
printf("*");
printf("\n");
}
}
==============================================================
[Program 24]
Problem: There is a fraction sequence: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13… Find the sum of the first 20 terms of this sequence.
1. Program Analysis: Please grasp the change rule of the numerator and denominator.
2. Program Source Code:
main()
{
int n, t, number = 20;
float a = 2, b = 1, s = 0;
for(n = 1; n <= number; n++)
{
s = s + a / b;
t = a;
a = a + b;
b = t; /* This part is the key to the program, please guess the role of t */
}
printf("sum is %9.6f\n", s);
}
==============================================================
[Program 25]
Problem: Find the sum of 1 + 2! + 3! + … + 20!
1. Program Analysis: This program simply changes addition into multiplication.
2. Program Source Code:
main()
{
float n, s = 0, t = 1;
for(n = 1; n <= 20; n++)
{
t *= n;
s += t;
}
printf("1 + 2! + 3!... + 20! = %e\n", s);
}
==============================================================
[Program 26]
Problem: Use recursion to find 5!.
1. Program Analysis: Recursion formula: fn = fn_1 * 4!
2. Program Source Code:
#include "stdio.h"
main()
{
int i;
int fact();
for(i = 0; i < 5; i++)
printf("%d! = %d\n", i, fact(i));
}
int fact(j)
int j;
{
int sum;
if(j == 0)
sum = 1;
else
sum = j * fact(j - 1);
return sum;
}
==============================================================
[Program 27]
Problem: Use recursion to print the 5 characters input in reverse order.
1. Program Analysis:
2. Program Source Code:
#include "stdio.h"
main()
{
int i = 5;
void palin(int n);
printf("Input: ");
palin(i);
printf("\n");
}
void palin(n)
int n;
{
char next;
if(n <= 1)
{
next = getchar();
printf("\nOutput: ");
putchar(next);
}
else
{
next = getchar();
palin(n - 1);
putchar(next);
}
}
==============================================================
[Program 28]
Problem: There are 5 people sitting together. When asked the age of the fifth person, he said he is 2 years older than the fourth person. When asked the fourth person’s age, he said he is 2 years older than the third person. When asked the third person, he said he is 2 years older than the second person. When asked the second person, he said he is 2 years older than the first person. Finally, when asked the first person, he said he is 10 years old. How old is the fifth person?
1. Program Analysis: Use recursion. Recursion is divided into two stages: backward deduction and forward deduction. To know the age of the fifth person, you need to know the age of the fourth person, and so on, until you reach the first person (10 years), and then deduce backward.
2. Program Source Code:
age(n)
int n;
{
int c;
if(n == 1) c = 10;
else c = age(n - 1) + 2;
return(c);
}
main()
{
printf("%d", age(5));
}
==============================================================
[Program 29]
Problem: Given a positive integer not exceeding 5 digits, find: 1. How many digits it has; 2. Print each digit in reverse order.
1. Program Analysis: Learn to decompose each digit, as explained below: (This is a simple algorithm provided by Zhao Xin from Class 002 of Teacher Training College)
2. Program Source Code:
main()
{
long a, b, c, d, e, x;
scanf("%ld", &x);
a = x / 10000; /* Decompose ten-thousands place */
b = x % 10000 / 1000; /* Decompose thousands place */
c = x % 1000 / 100; /* Decompose hundreds place */
d = x % 100 / 10; /* Decompose tens place */
e = x % 10; /* Decompose units place */
if (a != 0) printf("there are 5, %ld %ld %ld %ld %ld\n", e, d, c, b, a);
else if (b != 0) printf("there are 4, %ld %ld %ld %ld\n", e, d, c, b);
else if (c != 0) printf("there are 3, %ld %ld %ld\n", e, d, c);
else if (d != 0) printf("there are 2, %ld %ld\n", e, d);
else if (e != 0) printf("there are 1, %ld\n", e);
}
==============================================================
[Program 30]
Problem: Determine if a 5-digit number is a palindrome. For example, 12321 is a palindrome, as the units digit is the same as the ten-thousands digit, and the tens digit is the same as the thousands digit.
1. Program Analysis: Same as Example 29.
2. Program Source Code:
main()
{
long ge, shi, qian, wan, x;
scanf("%ld", &x);
wan = x / 10000;
qian = x % 10000 / 1000;
shi = x % 100 / 10;
ge = x % 10;
if (ge == wan && shi == qian) /* Units equal ten-thousands and tens equal thousands */
printf("this number is a palindrome\n");
else
printf("this number is not a palindrome\n");
}
==============================================================
[Program 31]
Problem: Please input the first letter of a day of the week to determine which day it is. If the first letter is the same, continue to check the second letter.
1. Program Analysis: Using case statements is better; if the first letter is the same, use case statements or if statements to check the second letter.
2. Program Source Code:
#include
void main()
{
char letter;
printf("please input the first letter of someday\n");
while ((letter = getch()) != 'Y') /* End when the letter pressed is Y */
{
switch (letter)
{
case 'S':
printf("please input second letter\n");
if((letter = getch()) == 'a')
printf("saturday\n");
else if ((letter = getch()) == 'u')
printf("sunday\n");
else
printf("data error\n");
break;
case 'F':
printf("friday\n");
break;
case 'M':
printf("monday\n");
break;
case 'T':
printf("please input second letter\n");
if((letter = getch()) == 'u')
printf("tuesday\n");
else if ((letter = getch()) == 'h')
printf("thursday\n");
else
printf("data error\n");
break;
case 'W':
printf("wednesday\n");
break;
default:
printf("data error\n");
}
}
}
==============================================================
[Program 32]
Problem: Press any key to change color, do you want to try it? Please hurry up!
1. Program Analysis:
2. Program Source Code:
#include
void main(void)
{
int color;
for (color = 0; color < 8; color++)
{
textbackground(color); /* Set the background color of the text */
cprintf("This is color %d\r\n", color);
cprintf("Press any key to continue\r\n");
getch(); /* Input character is invisible */
}
}
==============================================================
[Program 33]
Problem: Learn about gotoxy() and clrscr() functions.
1. Program Analysis:
2. Program Source Code:
#include
void main(void)
{
clrscr(); /* Clear screen function */
textbackground(2);
gotoxy(1, 5); /* Position function */
cprintf("Output at row 5 column 1\n");
textbackground(3);
gotoxy(20, 10);
cprintf("Output at row 10 column 20\n");
}
==============================================================
[Program 34]
Problem: Practice function calls.
1. Program Analysis:
2. Program Source Code:
#include
void hello_world(void)
{
printf("Hello, world!\n");
}
void three_hellos(void)
{
int counter;
for (counter = 1; counter <= 3; counter++)
hello_world(); /* Call this function */
}
void main(void)
{
three_hellos(); /* Call this function */
}
==============================================================
[Program 35]
Problem: Text color setting.
1. Program Analysis:
2. Program Source Code:
#include
void main(void)
{
int color;
for (color = 1; color < 16; color++)
{
textcolor(color); /* Set text color */
cprintf("This is color %d\r\n", color);
}
textcolor(128 + 15);
cprintf("This is blinking\r\n");
}
==============================================================
[Program 36]
Problem: Find prime numbers within 100.
1. Program Analysis:
2. Program Source Code:
#include
#include "math.h"
#define N 101
main()
{
int i, j, line, a[N];
for(i = 2; i < N; i++)
for(j = i + 1; j < N; j++)
if(a[j] != 0)
if(a[j] % a[i] == 0)
a[j] = 0;
printf("\n");
for(i = 2, line = 0; i < N; i++)
if(a[i] != 0)
{
printf("%5d", a[i]);
line++;
if(line == 10)
{
printf("\n");
line = 0;
}
}
}
==============================================================
[Program 37]
Problem: Sort 10 numbers.
1. Program Analysis: You can use the selection method, which selects the smallest number from the last 9 comparisons and swaps it with the first element, and so on, using the second element to compare with the last 8 elements, and perform swaps.
2. Program Source Code:
#define N 10
main()
{
int i, j, min, tem, a[N];
/* Input data */
printf("please input ten numbers:\n");
for(i = 0; i < N; i++)
{
printf("a[%d] = ", i);
scanf("%d", &a[i]);
}
printf("\n");
for(i = 0; i < N; i++)
printf("%5d", a[i]);
printf("\n");
/* Sort ten numbers */
for(i = 0; i < N; i++)
{
min = i;
for(j = i + 1; j < N; j++)
if(a[min] > a[j])
min = j;
tem = a[i];
a[i] = a[min];
a[min] = tem;
}
/* Output data */
printf("After sorted \n");
for(i = 0; i < N; i++)
printf("%5d", a[i]);
}
==============================================================
[Program 38]
Problem: Find the sum of the diagonal elements of a 3*3 matrix.
1. Program Analysis: Use a double for loop to control the input of a two-dimensional array, and then sum up a.
2. Program Source Code:
main()
{
float a[3][3], sum = 0;
int i, j;
printf("please input rectangle elements:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%f", &a[i][j]);
for(i = 0; i < 3; i++)
sum = sum + a[i][i];
printf("Diagonal sum is %6.2f", sum);
}
==============================================================
[Program 39]
Problem: There is an already sorted array. Now input a number and insert it into the array according to the original order.
1. Program Analysis: First, determine whether this number is larger than the last number, then consider the case of inserting into the middle number. After inserting, the numbers after this element will shift one position.
2. Program Source Code:
main()
{
int a[11] = {1, 4, 6, 9, 13, 16, 19, 28, 40, 100};
int temp1, temp2, number, end, i, j;
printf("original array is:\n");
for(i = 0; i < 10; i++)
printf("%5d", a[i]);
printf("\n");
printf("insert a new number:");
scanf("%d", &number);
end = a[9];
if(number > end)
a[10] = number;
else
{
for(i = 0; i < 10; i++)
{
if(a[i] > number)
{
temp1 = a[i];
a[i] = number;
for(j = i + 1; j <= 10; j++)
{
temp2 = a[j];
a[j] = temp1;
temp1 = temp2;
}
break;
}
}
}
for(i = 0; i <= 10; i++)
printf("%6d", a[i]);
}
==============================================================
[Program 40]
Problem: Output an array in reverse order.
1. Program Analysis: Swap the first with the last.
2. Program Source Code:
#define N 5
main()
{
int a[N] = {9, 6, 5, 4, 1}, i, temp;
printf("\n Original array:\n");
for(i = 0; i < N; i++)
printf("%4d", a[i]);
for(i = 0; i < N / 2; i++)
{
temp = a[i];
a[i] = a[N - i - 1];
a[N - i - 1] = temp;
}
printf("\n Sorted array:\n");
for(i = 0; i < N; i++)
printf("%4d", a[i]);
}
==============================================================
[Program 41]
Problem: Learn the usage of static to define static variables.
1. Program Analysis:
2. Program Source Code:
#include "stdio.h"
void varfunc()
{
int var = 0;
static int static_var = 0;
printf("Var = %d\n", var);
printf("Static var = %d\n", static_var);
printf("\n");
var++;
static_var++;
}
void main()
{
int i;
for(i = 0; i < 3; i++)
varfunc();
}
==============================================================
[Program 42]
Problem: Learn how to use auto to define variables.
1. Program Analysis:
2. Program Source Code:
#include "stdio.h"
void main()
{
int i, num;
num = 2;
for (i = 0; i < 3; i++)
{
printf("Num = %d\n", num);
num++;
{
auto int num = 1;
printf("Internal block num = %d\n", num);
num++;
}
}
}
==============================================================
[Program 43]
Problem: Learn another usage of static.
1. Program Analysis:
2. Program Source Code:
#include "stdio.h"
void main()
{
int i, num;
num = 2;
for(i = 0; i < 3; i++)
{
printf("Num = %d\n", num);
num++;
{
static int num = 1;
printf("Internal block num = %d\n", num);
num++;
}
}
}
==============================================================
[Program 44]
Problem: Learn the usage of external.
1. Program Analysis:
2. Program Source Code:
#include "stdio.h"
int a, b, c;
void add()
{
int a;
a = 3;
c = a + b;
}
void main()
{
a = b = 4;
add();
printf("The value of c is equal to %d\n", c);
}
==============================================================
[Program 45]
Problem: Learn how to use register to define variables.
1. Program Analysis:
2. Program Source Code:
void main()
{
register int i;
int tmp = 0;
for(i = 1; i <= 100; i++)
tmp += i;
printf("The sum is %d\n", tmp);
}
==============================================================
[Program 46]
Problem: Practice with the #define command (1)
1. Program Analysis:
2. Program Source Code:
#include "stdio.h"
#define TRUE 1
#define FALSE 0
#define SQ(x) (x)*(x)
void main()
{
int num;
int again = 1;
printf("Program will stop if input value is less than 50.\n");
while(again)
{
printf("Please input number ==>");
scanf("%d", &num);
printf("The square for this number is %d \n", SQ(num));
if(num >= 50)
again = TRUE;
else
again = FALSE;
}
}
==============================================================
[Program 47]
Problem: Practice with the #define command (2)
1. Program Analysis:
2. Program Source Code:
#include "stdio.h"
#define exchange(a,b) { \
int t;\
t = a;\
a = b;\
b = t;\
}
void main(void)
{
int x = 10;
int y = 20;
printf("x=%d; y=%d\n", x, y);
exchange(x, y);
printf("x=%d; y=%d\n", x, y);
}
==============================================================
[Program 48]
Problem: Practice with the #define command (3)
1. Program Analysis:
2. Program Source Code:
#define LAG >
#define SMA <
#define EQ ==
#include "stdio.h"
void main()
{
int i = 10;
int j = 20;
if(i LAG j)
printf("%d larger than %d \n", i, j);
else if(i EQ j)
printf("%d equal to %d \n", i, j);
else if(i SMA j)
printf("%d smaller than %d \n", i, j);
else
printf("No such value.\n");
}
==============================================================
[Program 49]
Problem: Comprehensive application of #if #ifdef and #ifndef.
1. Program Analysis:
2. Program Source Code:
#include "stdio.h"
#define MAX
#define MAXIMUM(x,y) (x>y)?x:y
#define MINIMUM(x,y) (x>y)?y:x
void main()
{
int a = 10, b = 20;
#ifdef MAX
printf("The larger one is %d\n", MAXIMUM(a, b));
#else
printf("The lower one is %d\n", MINIMUM(a, b));
#endif
#ifndef MIN
printf("The lower one is %d\n", MINIMUM(a, b));
#else
printf("The larger one is %d\n", MAXIMUM(a, b));
#endif
#undef MAX
#ifdef MAX
printf("The larger one is %d\n", MAXIMUM(a, b));
#else
printf("The lower one is %d\n", MINIMUM(a, b));
#endif
#define MIN
#ifndef MIN
printf("The lower one is %d\n", MINIMUM(a, b));
#else
printf("The larger one is %d\n", MAXIMUM(a, b));
#endif
}
==============================================================
[Program 50]
Problem: Application practice of #include
1. Program Analysis:
2. Program Source Code:
#define LAG >
#define SMA <
#define EQ ==
#include "test.h" /* A new file 50.c, includes test.h */
#include "stdio.h"
void main()
{
int i = 10;
int j = 20;
if(i LAG j)
printf("%d larger than %d \n", i, j);
else if(i EQ j)
printf("%d equal to %d \n", i, j);
else if(i SMA j)
printf("%d smaller than %d \n", i, j);
else
printf("No such value.\n");
}