当前位置:网站首页>Programming learning records - Lesson 5 [branch and loop statements]
Programming learning records - Lesson 5 [branch and loop statements]
2022-07-27 06:22:00 【Autumn mountain chariot God AE】
do while sentence
Execute statement first , Post judgment , It means executing at least once .
Two points search
Find an element in a sorted array , Because it's sorted , You can find it through two distributions , By judging the size of the middle elements of the array , Narrow the search , So back and forth , Until the corresponding element is found .
#include <stdio.h>
int main()
{
int i = 0;int n = 0;int arr[100] = { 0 };
printf(" Please enter the number of elements ( No more than 100):\n");
scanf("%d", &n);
printf(" Please input elements from small to large :\n");
for (i = 0;i < n;i++) { scanf("%d", &arr[i]); }
int target = 0;
printf(" Please enter the element you want to find :\n");
scanf("%d", &target);
int left = 0;
int right = n - 1;
while (left <= right) // Judge the condition , The left subscript cannot be larger than the right subscript .
{
int m = (left + right) / 2;// Can also be used left+(right-left)/2, Used when the number is large
if(arr[m] < target) { left = m + 1; }// Move left subscript right , Narrow the scope of
else if (arr[m] > target) { right = m - 1; }// Right subscript moves left , Narrow the scope of
else { printf(" Find success , The subscript is %d, Row of the first %d position .", m, m + 1);goto end; }
}
printf(" The element was not found ");
end:
return 0;
}Guess the number game
Generation of random numbers , You need two functions ,rand() And srand(), You need to srand Pass a random number , Initialize random functions , So that rand() The function continuously outputs random numbers , Here to srand Pass the timestamp as a random number ,
srand((unsigned int)time(NULL)); To express to srand() Pass a time stamp after forced time conversion , There is no need to store pointer addresses , So to time() Store a null pointer NULL;
Guess the number game
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
printf("***** Guess the number game *****\n");
printf("********************\n");
printf("*******1- Start *******\n");
printf("*******0- sign out *******\n");
printf("********************\n");
}
void game()
{
int s = rand()%100+1;// Operation of taking mold , Make the output random number controlled at 1-100;
int guess = 0;
while (1)// Put in a value that is always true , Control the continuation and jump out of the loop through the statements in the loop .
{
printf(" Please enter a number (1-100 Integers ):\n");
scanf("%d", &guess);
if (guess < s) { printf(" Guess a little \n");continue; }
else if (guess > s) { printf(" Guess the \n");continue; }
else { printf(" congratulations , Guessed it .\n"); break; }
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do// Put the menu and input and judgment into the cycle , So as to realize multiple operations .
{
menu();
printf(" Please enter a command \n");
scanf("%d", &input);
switch (input)
{
case 1: game();break;
case 0: printf(" Quit successfully \n");break;
default: printf(" Invalid order \n");break;
}
} while (input);
return 0;
}Simulate user login
// Simulate user login
//int main()
//{
// char password[] = "liangzai";
// char input[100] = " ";
// int i = 0;
// for (i = 0;i < 3;i++)
// {
// printf(" Please input a password :\n");
// scanf("%s", &input);
// if (strcmp(password, input) == 0) { printf(" Landing successful , Welcome, handsome boy ");break; }
// }
// if (i == 2) printf(" Diao Mao , Login without password ?");
// return 0;
//}
The point to note here is char The definition of type , If defining a single character , It only needs char ch='a', That's a good definition , If you define a string , You need to define an array ,char ch[100]="abcedfg"; You can consider the size of the array by yourself , But make sure to prevent array overflow ,strcmp Function is a function that compares strings , The returned value is an integer , When the values are the same , return 0.strcmp(password, input) == 0 The string representing two character type variables is exactly the same . If not, a positive integer is returned .
边栏推荐
- 租用香港服务器时单线与三线有什么区别?
- Index and transaction of database (emphasis)
- Pzk learns string function of C language (1)
- [dynamic planning - steel bar cutting]
- shell script if嵌套for循环脚本
- Li Kou 236. the nearest common ancestor of binary tree
- 如何选择正确的服务器备份方法
- Dynamic planning for solving problems (3)
- 遥感影像识别-制作数据集
- Unity 菜单界面的简单介绍
猜你喜欢

Osg环境搭建(Win10+Vs2019)

IP核之RAM

Pzk's first understanding of pointer in learning C language

Unityshader depth texture (understanding and problems encountered)

ROM of IP core

租用香港服务器时单线与三线有什么区别?

遥感影像识别-制作数据集

Ulcl function --5gc

Related knowledge of multithreading

What is the difference between single line and three line when renting servers in Hong Kong?
随机推荐
Wireshark packet modification -- adding or modifying message fields (2)
租用香港服务器时单线与三线有什么区别?
Random points in non overlapping rectangle (force deduction daily question)
UnityShader-LowPoly
ROS topic name setting
Index and transaction of database (emphasis)
ROS workspace coverage
Dynamic planning for solving problems (3)
Communication mechanism cases
Unityshader Gaussian blur
Learning the operation environment needs to be equipped during software testing
Sexy prime number (acwing daily question)
Reading and writing of file content - data flow
Three ways to get RPM packages using yum
wireshark功能介绍
Install Wireshark correctly
Socket long link
ROM of IP core
Unity practical tips (updating)
Interpretation of unity desktop version 7.6