Sharing of writing ideas

1. Print leap year

1. Determine the number of variables, only one variable is year;
2. Variable range: output the leap year between 1000 and 2000, then 1000<=year<=2000;
3. Judging that the year is a leap year needs to meet any one of two combinations of conditions: 1. The year is divisible by 4 and the year is not divisible by 100. 2. The year is divisible by 400.
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	int year = 0;
	for (year = 1000; year <= 2000; year++)
   {
		if (year % 4 == 0 && year % 100 != 0)
			printf("%d ", year);
		else if (year % 400 == 0)
			printf("%d ", year);
   }

	return 0;
}

2. Find the maximum value among ten numbers

1. First, you need to define an integer array space, because you need to input ten numbers here, so the array space is 10.
2. Then define a maximum value Max, the initial default value is 0, which is used for comparison of subsequent values.
3. Then use the for loop to continuously receive the input of 10 numbers.
4. Then use a for loop to loop through the array to find the maximum value Max.
5. After the loop ends, output the final result, which is the maximum value among the 10 numbers we need.
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	int arr[10];
	int Max = 0;
	for (int  i = 0; i < 10; i++)
	{
		scanf("%d", &arr[i]);
	}
	for (int i = 0; i < 10; i++)
	{
		if (arr[i] >= Max)
		{
			Max = arr[i];
		}
	}
	printf("十个数中最大的是:%d", Max);
	return 0;
}

3. Binary search

The goals of the binary search algorithm can be divided into three categories:

1. Find the target value, if it exists, return the subscript; if it does not exist, return -1

2. Find the left boundary of the target value, if all elements of the sequence are greater than the target value, return -1

3. Find the right boundary of the target value, if all elements of the sequence are less than the target value, return the sequence length

 Implementation ideas

1. The initialization value of right is the length of the sequence -1, then the initial judgment interval is [0, len-1], the left-closed right-closed interval, and the subsequent judgment interval is [left, right]. So the end condition of the while loop is left>right, and the judgment condition is left<=right.

2. Every time the interval splitting operation is performed, [left, right] is split in half, and the median element arr[mid] has been searched, so that the next search interval is still a left-closed right-closed interval, then the left half The interval is [left, mid-1], and the right half interval is [mid+1, right]. So right=mid-1.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int sz = sizeof(arr) / sizeof(arr[1]);
	int k = 0;
	scanf("%d", &k);
	int left = 0;
	int right = sz-1;
	int flag = 0;
	while (left <= right)
	{
		int mid = (left + right) / 2;
		if (arr[mid] < k)
			mid = left + 1;
		else if (arr[mid] > k)
			mid = right - 1;
		else
		{
			printf("找到了,下标是:%d\n", mid);
			flag = 1;
			break;
		}
	}
	if (flag == 0)
		printf("找不到了");
	return 0;
}

4. Exchange the content in array A with the content in array B. (as big as the array)

No matter which method is used (create a temporary variable or not), if the exchange value is written in the form of a function, the address must be passed.
#define _CRT_SECURE_NO_WARNINGS 
#include<stdio.h>
void temp(int* x, int* y)
{
	*x = *x ^ *y;
	*y = *x ^ *y;
	*x = *x ^ *y;
}
int main()
{
	int arr1[] = { 1, 2, 3, 4, 5 };
	int arr2[] = { 6, 7, 8, 9, 10 };
	int sz = sizeof(arr1) / sizeof(arr1[0]);
	for (int i = 0; i < sz; i++)
	{
		temp(&arr1[i], &arr2[i]);
	}
	printf("交换后数组arr1的值:");
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr1[i]);
	}
	printf("\n");
	printf("交换后数组arr2的值:");
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr2[i]);
	}
}

 That's all for today's sharing, thank you soldering irons for reading.

Guess you like

Origin blog.csdn.net/2301_79035870/article/details/131796938