当前位置:网站首页>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 .
边栏推荐
- Force deduction problem solving monotonous stack
- Learning the operation environment needs to be equipped during software testing
- Unity 菜单界面的简单介绍
- Solve binary tree (6)
- ROS distributed communication
- 多坐标变换
- Multi threaded CAS, synchronized lock principle, JUC and deadlock
- PLL of IP core
- 文件内容的读写——数据流
- Solve binary tree (5)
猜你喜欢

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

Pzk's first understanding of pointer in learning C language

Unable to start program, access denied?

力扣每日一题leetcode 513. 找树左下角的值

IP核之RAM

数据库在终端的基础操作

Simple understanding of network principle

数据库在终端的增删改查

What is the difference between single line and three line when renting servers in Hong Kong?

遥感影像识别-成像合成
随机推荐
机器人导航实现
wireshark数据包修改--IP地址修改(一)
Knowledge supplement of multithreading
Navigation related messages
Solving binary tree with force deduction (8)
Unity 窗口界面的简单介绍
Install Wireshark correctly
ROM of IP core
Remote sensing image recognition imaging synthesis
C thread lock
通信机制比较
SQL novice
哈希表的原理及哈希冲突的解决方法
What is the difference between single line and three line when renting servers in Hong Kong?
IP核小结
Unityshader depth texture (understanding and problems encountered)
Progress in remote sensing image recognition 2022/5/5
遥感影像识别-训练策略
Dynamic programming for solving problems (2)
网络原理的简单认识