This article is a summary of the problems I encountered in the C language that I have studied for a period of time. The knowledge points involved may not be friendly to beginners, and the amount of thinking is a little big, but no matter how difficult it is, I believe that we It can always be overcome. I will try to explain the content in more detail. If you don’t understand anything, you can leave a message in the comment area and I will try my best to solve your problem.
1 The frog jumping problem–actually a Fibonacci sequence problem
The question is as follows: A frog starts jumping from the first step. It can only jump 1 step or 2 steps at a time. How many ways can the frog jump to the nth step?
Let me first use mathematical knowledge to answer the question: When a frog jumps to the nth step, it can only jump two steps from the n-2th step, or jump one step from the n-1th step to the nth step.
Solution 1: Use for loop (Basically, for loop is used more in the later period. The third lecture will explain why for loop is used more)
#include<stdio.h> int main() { int a = 1; int b = 1; int c = 0;//Initialize three variables here for subsequent exchanges, c represents the value of the nth item int i = 0; int n = 0; scanf("%d", & amp;n); if(1 == n||2 == n) { c==1; } for(i=3;i<=n;i + + ) { c = a + b; a = b; b = c; } printf("The value of the nth item is: %d",c); return 0; }We can see that the values running are correct
Solution 2: Recursion and iteration of functions
#include<stdio.h> int Fib(int n) { if(1 == n||2 == n) return 1; else return Fib(n-1) + Fib(n-2); } int main() { int n = 0; int ret = 0;//Used to receive the value of function Fib(n) scanf("%d", & amp;n); ret=Fib(n); printf("%d",ret); }Doesn’t it look very simple? The following picture is easy to understand.
Extension of the title:
1-1 Find the sum of the first n terms of the Fibonacci sequence
Below is the code
#include<stdio.h> int main() { int i = 0; int n = 0; int a = 1; int b = 1; int c = 0; int m = 2; int sum = 0; scanf("%d", & amp;n); for (i = 3; i <= n; i + + ) { if (4 == i) m = 0;//Add m=0 here to avoid adding 2 more in the next cycle c = a + b; sum = m + c + sum; a = b; b = c; } if (1 == n) { sum == 1; } if (2 == n) { sum = 2; } printf("sum=%d\ ", sum); return 0; }
2 Number Guessing Game
Before writing code, we must first understand the steps required
2-1 We first need a program to enter the game
2-2 We need to set random numbers
2-3 Playing once is definitely not enough, you definitely need to play a few more times (the implication is that you need to use loops)
2-4 When you are done playing and don’t want to play anymore, you must have a procedure to exit the game.
Below we use two methods to play the game
Method 1: Recursion and iteration of functions
#include<stdio.h> #include<stdlib.h> #include<time.h>//Time stamp header file void menu()//Note that viod here means that the function has no return value and must be set up. { printf("Please enter your number: 0 to enter the game 1 to exit the game\ "); } void game() { //The concept of timestamp is used here. The timestamp is the current time minus the time of 1970, so that the timestamp is different every moment, and the generated number is more random. int guess = 0;//Set the number to guess int random = rand() 1;//The function rand that releases random numbers is used here. The header file is required for rand. printf("Please guess an integer from 1 to 100:\ "); do { scanf("%d", & guess); if (guess > random)//Set up to remind you whether the number you guessed is too high or too low or if you guessed it correctly { printf("Guessed it\ "); } else if (guess < random) { printf("Guess it's too small\ "); } else printf("Congratulations on guessing correctly\ "); } while (guess != random);//Don’t forget to add a semicolon here, and then the game is over if you guess correctly } int main() { menu();//Set the menu function. Enter 0 to enter the game. Enter 1 to exit the game. int input = 0;//Set the variable to the number you want to input srand((unsigned int)time(NULL));//Set a seed for random number generation. In order to make the generated numbers more random, the srand function is placed in the main function. scanf("%d/n", & amp;input); switch(input) { case 0: printf("Start playing the game play/n"); game();//Set a function for playing games break; case 1: printf("I don’t want to play anymore, I want to quit the game/n"); break; default: printf("Input error: Please re-enter the number\ "); break; } return 0; }
Advanced version of method two: do not call external functions and only use the main function —> Here you limit the number of times you guess the number, the number of times you guess the number, the record of the number you guess, and you will be told the answer if you guess the number incorrectly!
#include<stdio.h> #include<stdlib.h> #include<time.h> #define MAX_STAGE 10//Define macro to set your maximum number of times to read numbers int main() { int random = 0; int guess = 0; int i = 0; int stage = 0;//Number of times entered srand((unsigned int)time(NULL)); int num[MAX_STAGE] = { 0 }; random = rand() 1; printf("Please enter your numbers: 0 to enter the game 1 to exit the game\ "); int input = 0; while(1) { scanf("%d", & amp;input); while((stage + + )<MAX_STAGE) { switch(input) { case 0: printf("Enter the game"); do { a: printf("Remaining chances of guessing the number: %d\ ", MAX_STAGE - stage); printf("Please enter the number you want to guess:"); scanf("%d", & guess); num[stage + + ] = guess;//Save the guessed number in the array if (guess > random) printf("Guessed it\ "); else if (guess < random) printf("guessed too small"); else { printf("Congratulations on guessing correctly"); } } while (random != guess & amp; & amp; MAX_STAGE - stage>0);//Add a restriction and the game will end if the number of guesses exceeds 10 times. break; case 1: printf("Exit the game\ "); goto b; default: printf("Incorrect input, please re-enter:"); goto a; } } if (random != guess) { printf("Sorry to tell you the correct answer: %d", random); } printf("The following is a record of the numbers you entered"); for (i = 0; i < MAX_STAGE; i + + ) { printf(" - : M % + 4d\ ", i + 1,num[i],num[i]-random);//How many times to read the difference between the number read and the correct number } b: return 0; }
You can copy the above code yourself and verify it in your compiler
Summary below:
1 When you set a variable, you should know clearly what it is used for, and then divide each step:
For example: Guess the number game: First I have to enter the game
Secondly, I want to set a random number
Then guess the number
Then I will tell you whether you guessed higher or lower
The game ends if you guess correctly, continue guessing if you guess wrong
2 Required knowledge points: Recursion and iteration of functions
for and do…while loops
Accept numbers in array
Finally, thank you everyone for watching. See you in the next lecture!