当前位置:网站首页>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 .
边栏推荐
- 如何选择正确的服务器备份方法
- Non photorealistic rendering (NPR) paper understanding and reproduction (unity) - stylized highlights for cartoon rendering and animation
- ROS通信机制进阶
- Thesis writing (harvest)
- 多线程CAS、synchronized锁原理 、JUC以及死锁
- 遥感影像识别-制作数据集
- selenium知识点
- Detailed explanation of thread safety problems
- 力扣题解 动态规划(2)
- Related knowledge of multithreading
猜你喜欢
随机推荐
IP核小结
学习软件测试时需要配备的运行环境需求搭建
遥感影像识别-多类识别下的错分问题
5g network identity - detailed explanation of 5g Guti
ROS运行管理之launch文件
Man moon myth reading notes
Launch file of ROS operation management
Install Wireshark correctly
TF坐标变换
How to choose the correct server backup method
Strategies for common locks in multithreading
C thread lock
Three ways to get RPM packages using yum
通信机制比较
UnityShader-深度纹理(理解以及遇到的问题)
Dynamic planning for solving problems (5)
文件内容的读写——数据流
socket 长链接
IP核之PLL
切线空间以及TBN矩阵









