Binary search method | Greatest common divisor | Fighting in the arena | Printing x graphics | Printing from both sides to the middle and other questions | Detailed explanation | C language

1. Find the greatest common divisor of two numbers

method:

Euclidean division: Repeatedly treating the divisor as the dividend and dividing the remainder until the remainder reaches 0. The divisor at this time is the greatest common divisor of the original two numbers.

For example:

32 / 26 = 1...6

26 /   6 = 4...2

6   /   2 = 3...0

According to the above method, 2 is the greatest common divisor of 32 and 26 .

code show as below:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>//求两数最大公约数
//辗转相除法:反复的将除数当作被除数去除余数,直到余数为0截至,此时的除数就是原始两数的最大公约数。
int main()
{
	int x = 0;
	int y = 0;
	


	while (scanf("%d%d", &x, &y)==2)//多组输入
	{

		
		int max = 0;
		if (y > x)
		{
			max = y;
			y = x;
			x = max;//保证x永远是较大数
		}
		int i = x % y;//i是x与y的余数

		
		while (i!=0)//不断的将除数当作被除数去除余数,直到余数为0截至,此时除数就是原始两数的最大公约数
		{

				x = y;
				y = i;
				i = x % y;//i是x与y的余数
			
		}
		printf("%d是x与y的最大公约数\n", y);
	}


	return 0;
}

 2. Binary search method (half search method)

The binary search method is a half search, and half of the original data can be removed after each search.

Compared with the brute force enumeration method, the binary search method has the advantages of high search efficiency and fast speed (especially for a relatively large set of data).

But note that the binary search method is only suitable for finding elements in a set of ordered numbers. For a set of unordered numbers, you need to sort them first and then use the binary search method.

Example:

Suppose, find 7.

46c479bb7dff44b88f047ee74bfbcd5e.png

First determine the leftmost element and the subscript of the rightmost element. as follows:

int sz=sizeof(arr)/sizeof(arr[0]) //计算数组元素个数
int left=0;  //最左边元素下标
int right=sz-1;  //最右边元素下标

Then, find the subscript of the middle element. as follows:

int mid=left+(right-left)/2;  //中间元素下标

The element corresponding to the subscript is larger than the element you are looking for (in this example, we are looking for 7, so let’s compare it with 7) .

if(arr[mid]<7)
{
  left=mid+1;//更新左数下标
}
else if(arr[mid]>7)
{
  right=mid-1;//更新右数下标
}

Combine the above steps. as follows.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
    int arr[10] = { 0 };
    int i = 0;
    int sz = sizeof(arr) / sizeof(arr[0]);//数组中的元素的个数
    for (i = 0; i < sz; i++)//输入十个数
    {
        scanf("%d", &arr[i]);
    }
    //假设输入的十个数是正序的
    //进行二分查找
    int flag = 0;//用来判断是否找到了所要找的数
    int x = 0;//要查找的数
    printf("请输入你要查找的数:");   scanf("%d", &x);
    int left = 0;//刚开始最左边的数的下标
    int right = sz - 1;//刚开始最右边的数的下标
    while (left <= right)
    {
        int mid = left + (right - left) / 2;//计算中间的数的下标//可以排查掉一半
        if (x > arr[mid])//如果要查找的数大于中间的数,更新左数下标
        {
            left = mid + 1;//排查掉一半

        }
        else if (x < arr[mid])//如果要查找的数小于中间的数,更新右数下标
        {
            right = mid - 1;//排查掉一半

        }
        else if (x==arr[mid])//此时x==arr【mid】就说明left==right已经定位的只剩最后一个数了//如果x==arr【mid】就说明找到了 //否则就找不到
        { flag = 1;
            printf("找到了,这个数的下标是 :%d\n", mid);
            break;//找到了就退出
        }

    }
    if (flag == 0)
    {
        printf("找不到\n");
    }


    return 0;
}

3. Find the maximum-minimum number using the ring method

Find the maximum and minimum numbers by playing in the ring.

1. First assume that the maximum number is arr[0] and the minimum number is also arr[0].

as follows:

int max=arr[0];
int min=arr[0];  //假定最大值,最小值都是arr[0]

2.For loop, compare the elements in the array one by one.

as follows:

int sz=sizeof(arr)/sizeof(arr[0]);  //数组中元素的个数
for(int i=0;i<sz;i++)  //sz个元素,比sz-1次
{
  if(arr[i]>max)
  {
  max=arr[i];  //更新最大值
  }
  if(arr[i]<min)
  {
  min=arr[i];  //更新最小值
  }
}  

Combined with the above.

as follows:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	int arr[10] = { 0 };
	int sz = sizeof(arr) / sizeof(arr[0]);//数组中元素的个数


	for (int i = 0; i < 10; i++)
	{
		scanf("%d", &arr[i]);  //输入数组中元素的值
	}


	int min = arr[0];
	int max = arr[0];
	for (int i = 0; i < sz; i++)  //sz个数,比sz-1次
	{
		if (arr[i] > max)
		{
			max = arr[i];  //更新最大值
		}
		if (arr[i] < min)
		{
			min = arr[i];  //更新最小值
		}
	}

	printf("max=%d\n", max);
	printf("min=%d\n", min);



}

4. Print x graphics

Think about programming issues from a computer perspective.

as follows:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
//多组输入,一个整数(2~20),表示输出的行数,也表示组成“X”的反斜线和正斜线的长度。
//针对每行输入,输出用“*”组成的X形图案。
int main()
{
	int input = 0;  //输出的行数


	while (scanf("%d", &input) == 1)  //多组输入
	{
		for (int i = 1; i <= input; i++)  //外层循环,控制行
		{
			for (int j = 1; j <=input; j++)  //里层循环,控制列
			{
				if (i == j||input==i+j-1)  //打印'*'的两个条件:1.行等于列   2.输入的数等于行+列-1
					                       //满足一条即打印
				{
					printf("*");
				}
				else  //以上两个条件都不满足:打印空白字符
				{
					printf(" ");
				}
			}
			printf("\n");
		}
	}



}

5. Print from both sides to the middle

Note: It is necessary to ensure that the number of elements in the two arrays is the same.

as follows:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<Windows.h>
int main()
{
	char arr1[] = { "Welcome To CSDN" };
	char arr2[] = { "***************" };  //注意:两数组元素个数相同
	
	int sz = strlen(arr1);  //计算字符串数组的长度  //注意计算的是'\0'前面的字符
	
	int right = sz-1;  //最右边字符对应的下标 
	int left = 0;      //最左边字符对应的下标
	  

	while(left<=right)  //左下标小于等与右下标
	{
		arr2[right] = arr1[right];  //替换
		arr2[left] = arr1[left];  //替换
		 

		
		printf("%s\n", arr2);
		Sleep(1000);  //打印之后睡眠1000毫秒  //引头文件<Windows.h>
		system("cls");  //打印之后清理屏幕    //引头文件<stdlib.h>
		right--;  //右边--
		left++;  //左边++   //向中间聚拢
	}
	printf("%s\n", arr2);


	return 0;
}

Thank you for reading!

 

 

 

Guess you like

Origin blog.csdn.net/nmbg11/article/details/134767983