当前位置:网站首页>C language question brushing series (III)

C language question brushing series (III)

2022-06-10 03:33:00 Small process

This series is just a summary of my own study , Please give me some praise !

Catalog

1. Calculation n The factorial

2. Calculation 1!+2!+3!+...+10!

3. Find a specific number in an ordered array .

4. Write code , Demonstrate the movement of multiple characters from both ends , Converging in the middle .

5. Write code to achieve , Simulate user login scenarios , And can only log in three times .( Only three passwords are allowed , If the password is correct, the login is successful , If all three inputs are wrong , Then exit the program .)


1. Calculation n The factorial

Ideas :

To count n The factorial , First we have to have a number n,n The factorial of is from 1*2*3...*n Of , So you also need to have a loop variable i To multiply all the time .

int main()
{
	int n = 0;
	scanf("%d", &n);
	int i = 0;
	int ret = 1;
	for (i = 1; i <= n; i++)
	{
		ret = ret * i;
	}
	printf("%d\n", ret);
	return 0;
}

2. Calculation 1!+2!+3!+...+10!

Ideas :

First , We are based on the code in the previous figure , We can draw n The factorial , That request 1!+2!+3!+...+10! Just add the factorials of each number together , Use one sum Save the variable .

int main()
{
	int n = 0;
	scanf("%d", &n);
	int i = 0;
	int ret = 1;
	int sum = 0;
	for (i = 1; i <= n; i++)
	{
		ret = ret * i;
		sum = sum + ret;
	}

	printf("%d\n", sum);
	return 0;
}

3. Find a specific number in an ordered array .

Ideas :

( Two points search ) Here I will directly use pictures to explain the process of binary search

The words are ugly , I hope you can bear more .

This is also the code for binary search  

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int k = 12;
	int sz = sizeof(arr) / sizeof(arr[0]);
	// Here is a way to find the subscript at the end of an array 
	int left = 0;
	int right = sz - 1;
	//int mid = (left + right) / 2;
	/*int mid = left + (right - left) / 2;*/
	// It is better to write here in the following way .
	while (left<=right)
	{
		//int mid = (left + right) / 2;
		int mid = left + (right - left) / 2;
		// It is better to write here in the following way .
		if (arr[mid] < k)
		{
			left = mid + 1;
		}
		else if (arr[mid] > k)
		{
			right = mid - 1;
		}
		else
		{
			printf(" eureka , The subscript is :%d\n", mid);
			break;
		}
	}
	if (left > right)
	{
		printf(" Can not find ");
	}
	return 0;
}

There are a few things to pay attention to ,

int mid = (left + right) / 2;
int mid = left + (right - left) / 2;
It is better to write here in the following way ., Because the size of the shape type has a range , In case your two numbers add up out of range , It will lead to mistakes .

int sz = sizeof(arr) / sizeof(arr[0]);

Here is a way to find the subscript at the end of an array

4. Write code , Demonstrate the movement of multiple characters from both ends , Converging in the middle .

Ideas :

welcome to anhui!!!!!!
######################
w####################!
we##################!!
wel################!!!
...
welcome to anhui!!!!!!

First , It doesn't matter , We need to have two arrays , Then, you can overlay the elements of the above array on the lower array , Another cycle .

#include <string.h>
#include <windows.h>
#include <stdlib.h>
//
int main()
{
	char arr1[] = "welcome to anhui!!!!!!";
	char arr2[] = "######################";
	int left = 0;
	int right = strlen(arr1) - 1;
	while (left<=right)
	{
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		printf("%s\n", arr2);
		left++;
		right--;
		Sleep(1000);
		system("cls");
	}
	printf("%s\n", arr2);
	return 0;
}

Pay attention here , Two library functions ,Sleep Is contained in stdlib In this header file , In milliseconds .system yes windows Self contained , and clc Is the command to clear the screen .

5. Write code to achieve , Simulate user login scenarios , And can only log in three times .( Only three passwords are allowed , If the password is correct, the login is successful , If all three inputs are wrong , Then exit the program .)

Ideas :

You can only log in three times , Then use a variable to represent three times , Then compare whether the input string and password are equal .

It is important to note that the comparison of two strings cannot be used ==
You should use strcmp

int main()
{
	int n = 0;
	char password[20] = { 0 };
	// Suppose the password is abcdef
	for (n = 1; n <= 3; n++)
	{
		printf(" Please input a password :");
		scanf("%s", password);
		if (strcmp(password, "abcdef") == 0)
		{
			printf(" The password is correct \n");
			break;
		}
		else
		{
			printf(" Reenter the password \n");
		}
	}
	if (4 == n)
	{
		printf(" Input error , Exit procedure \n");
	}
	return 0;
}

原网站

版权声明
本文为[Small process]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100324309122.html