Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use binary search to realize guessing game in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how C language uses binary search to realize guessing games. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

(1) binary search 1.1 what is binary search

Half search, also known as binary search, in some cases, compared with sequential search, the use of half search algorithm is more efficient. However, the premise of the use of this algorithm is that the data in the static look-up table must be ordered.

For example, before the look-up table uses the half-and-half search algorithm to find the data, it is necessary to sort the data in the table according to the keywords checked: {5pence 13, 19, 21, 21, 37, 5, 7, 5, 7, 5, 7, 5, 7, 5, 5, 7, 5, 7, 5, 8, 8, 9, 8, 8, 9, 8, 8, 9, 8, 8, 9, 8, 8, 9, 8, 9, 8, 8, 9, 8, 8, 9, 8, 8, 9, 8, 8, 9, 8, 9, 8, 9, 8, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 9, 4, 4, 5, 5, 5, 5, 7, 5, 7, 5,

1.2 the principle of binary search

Take the ascending number as an example, compare the size of an element with the element in the middle position in the sequence, if it is larger than the element in the middle position, continue to do a binary search in the second half of the sequence; if it is smaller than the element in the middle position, compare it in the first half of the sequence; if equal, find the position of the element. The length of the sequence for each comparison will be half that of the previous sequence until the location of the equivalent element is found or the element you are looking for is not found.

Moving picture demonstration: (compared with sequential search)

? The prerequisite for binary search is an ordered sequence, but not for ordinary search.

Find the subscript that returns the element, otherwise return-1.

The time complexity of ordinary search is O (N), and that of binary search is O (logN). 2 ^ m = N (m is the number of half searches), then the time complexity of m=log (N), binary search is O (logN).

1.4 Code implementation

1.4.1 initialization data

1.4.2 Core function

(II) initialization of the menu of the guessing game 2.1

2.2 Core function

2.3 main function

2.4 General Code # include # include # include void menu () {printf ("* *\ n"); printf ("* 1.play *\ n") The printf ("* 0.exit *\ n"); printf ("* *\ n");} / / RAND_MAX--rand function returns the maximum value of a random number. Void game () {int random_num = rand ()% 100 + 1; int input = 0; while (1) {printf ("enter guessed number >:"); scanf ("% d", & input) If (input > random_num) {printf ("guess big\ n");} else if (input)

< random_num) { printf("猜小了\n"); } else { printf("恭喜你,猜对了\n"); break; } }} int main(){ int input = 0; srand((unsigned)time(NULL)); do { menu(); printf("请选择>

: "); scanf ("% d ", & input); switch (input) {case 1: game (); break; case 0: break Default: printf ("wrong choice, please re-enter!\ n"); break;}} while (input); return 0;} Thank you for reading! On "C language how to use binary search to achieve guessing game" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report