In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to understand the data structure of C language, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to understand the data structure of C language. let's take a look.
1 guessing number game-problem description
This game is no stranger, guess the price is once a very popular variety show. Many teachers also use this case as a classroom case. Here, I want to focus on the "thinking level", that is, why the code is written in this way, it realizes the function of guessing numbers.
Let's start with a live-action game of guessing numbers:
A: a number is silently in mind (agree on a range, suppose between [1-100]), and begin to guess
B guess: 50
A: it's big
B guess: 25
A: it's small
B guess: 150
A: you broke the rules
B guess: 30
A: that's right! The correct answer is 30! Do you want to keep guessing?
B: yes
...
2 problem analysis
From the front live version to the adult version:
Real-world program simulation approach A the computer silently says a number, the value of a variable, a random number / the value of a variable that is manually defined in the program / other (in short, you solve: a way to generate a number) B guess B tapping the keyboard scanf scan keyboard A said big, small, right computer judgment and output judgment results if judgment An and B continue to guess repeated events cycle between An and B.
As the saying goes, "the great things in the world must be done easily, and the difficult things in the world must be done in detail."
With an analysis of real problems, you can also simplify a problem layer by layer, and then enrich its functionality layer by layer:
1) guess once
2) until guessed
3) limit the number of guesses and show the current number of guesses
4) deal with special cases: if you guess in advance; if your guess is out of range
5) guess more numbers
6) the more you can time the game and guess correctly in one minute, you can also set levels.
...
In this way, you will have your own plan for a more comprehensive problem, and then you can start to try to break it one by one.
3 problem solving 3.1 guess once
Using IPO's thinking, continue to analyze the situation and see if the story flow is like this:
/ / I: enter
1) computer: give a number
2) user: enter a number on the keyboard (guess)
3) computer: get this number
/ / process and output
4) computer: judge the relationship between this number and its own number.
5) computer: according to different relationships, tell you to guess big, small, right
6) computer: tell you the correct answer
According to the input and output, determine the variables to be defined: the number produced by the computer, the number used for guessing
What the programmer has to do is to describe the above story flow in a programming language.
And the flow of the story is: algorithm
What the programming language describes is: the program
Then, we can get the following program:
# include # include # include int main () {int guess,magic; / / magic computer think srand (time (NULL)); / / use time as a seed to generate a random number magic = rand () 0x1; / / random number falls to a range of 1-100printf ("guess a number:\ n"); / / user guesses a number scanf ("% d", & guess) If (guess > magic) / / computer judges and tells you the guesses printf ("too big!\ n"); else if (guessmagic) printf ("too big!\ n"); else if (guessmagic) printf ("too big!\ n") Else if (guess
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.