C language: Realize ordered sequence judgment

topic:

Enter a sequence of integers to determine whether it is an ordered sequence . Ordered means that the integers in the sequence are sorted from small to large or from large to small ( the same elements are also considered to be ordered ).

               

Enter a description:

The first line enters an integer N  (3≤N≤50).

Enter N integers in the second line , and separate N integers with spaces .

                            

Output description:

The output is one line , sorted if the sequence is ordered , otherwise unsorted .

           

Example 1:

enter:

5
1 6 9 22 30

output:

sorted

Example 2:

enter:

5
3 4 7 2 10

output:

unsorted

Example 3:

enter:

5
1 1 1 1 1

output:

sorted

                    

 =========================================================================

                       

Idea 1: Input data first and then judge

general idea:

(one).

Input array length N ; input N integers and store them in an array

              

(two).

Determine whether it is ordered : ascending or descending ,

In order, all are greater than sign or all are less than sign .

             

Use two variables to represent ascending and descending order :

int flag1 = 0;        -- The table is in ascending order , which satisfies the ascending order relationship, that is, flag1 = 1

int flag2 = 0;        -- table in descending order , satisfying the descending relationship, that is, flag2 = 1

If equal , flag1 and flag2 are both 0

              

Compare two adjacent numbers , and compare n-1 pairs of N numbers :

Use a for loop to loop through the comparison ,

Use the if conditional judgment statement in the for loop ,

Judging whether the previous number is less than the next number , or whether the previous number is greater than the next number ,

If it is less than, flag1 = 1 , that is, the current two numbers are less than signs , and may be in ascending order ;

If it is greater than then flag2 = 1 , that is,  the current two numbers are greater than , possibly in descending order .

                   

(three). 

After judging the ascending or descending order , look at the situation of flag1 and flag2 to judge whether it is in order :

               

If flag1 + flag2 == 2 , it means there are both greater than and less than signs , print unsorted  ;

Other cases:

If flag1 + flag2 == 1 , it means order ;

If flag1 + flag2 == 0 , it means that they are all equal and in order , so print sorted .

                


                 

first step:

(1). Input array length N

              

(2). Input N integers and store them in an array

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入数组长度N:
	int N = 0; //数组长度
	//输入:
	scanf("%d", &N);

	//输入N个整数,用一个数组进行存储:
	int arr[50] = { 0 };
	//输入N个整数:
	int i = 0;
	for (i = 0; i < N; i++)
	{
		scanf("%d", &arr[i]);
	}


	return 0;
}

Realize the picture:

                 


                 

Step two:

Determine whether it is ordered : ascending or descending ,

In order, all are greater than sign or all are less than sign .

             

(1).

Use two variables to represent ascending and descending order :

int flag1 = 0;        -- The table is in ascending order , which satisfies the ascending order relationship, that is, flag1 = 1

int flag2 = 0;        -- table in descending order , satisfying the descending relationship, that is, flag2 = 1

If equal , flag1 and flag2 are both 0

              

(2).

Compare two adjacent numbers , and compare n-1 pairs of N numbers :

Use a for loop to loop through the comparison ,

Use the if conditional judgment statement in the for loop ,

Judging whether the previous number is less than the next number , or whether the previous number is greater than the next number ,

If it is less than, flag1 = 1 , that is, the current two numbers are less than signs , and may be in ascending order ;

If it is greater than then flag2 = 1 , that is,  the current two numbers are greater than , possibly in descending order .

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入数组长度N:
	int N = 0; //数组长度
	//输入:
	scanf("%d", &N);

	//输入N个整数,用一个数组进行存储:
	int arr[50] = { 0 };
	//输入N个整数:
	int i = 0;
	for (i = 0; i < N; i++)
	{
		scanf("%d", &arr[i]);
	}

	//判断是否有序:升序 或 降序
	//有序的情况下,都是 大于号 或 都是 小于号

	int flag1 = 0; //表示升序,满足升序关系 --> flag1=1
	int flag2 = 0; //表示降序,满足降序关系 --> flag2=1
	//判断过程中,
	//如果 flag1一直为1,flag2一直为0,说明是升序,
	//如果 flag2一直为1,flag1一直为0,说明是降序
	//等于的话,flag1和flag2都是0

	//相邻两个数进行比较,N个数,比较 N-1 对:
	for (i = 0; i < N-1; i++)
	{
		if (arr[i] < arr[i + 1])
		//前一个数 小于 后一个数
		{
			flag1 = 1;
			//满足升序条件
		}
		else if (arr[i] > arr[i + 1])
		//前一个数 大于 后一个数
		{
			flag2 = 1;
			//满足降序条件
		}
	}

	return 0;
}

Realize the picture:

                 


                 

third step:

After judging the ascending or descending order , look at the situation of flag1 and flag2 to judge whether it is in order :

               

If flag1 + flag2 == 2 , it means there are both greater than and less than signs , print unsorted  ;

Other cases:

If flag1 + flag2 == 1 , it means order ;

If flag1 + flag2 == 0 , it means that they are all equal and in order , so print sorted .

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入数组长度N:
	int N = 0; //数组长度
	//输入:
	scanf("%d", &N);

	//输入N个整数,用一个数组进行存储:
	int arr[50] = { 0 };
	//输入N个整数:
	int i = 0;
	for (i = 0; i < N; i++)
	{
		scanf("%d", &arr[i]);
	}

	//判断是否有序:升序 或 降序
	//有序的情况下,都是 大于号 或 都是 小于号

	int flag1 = 0; //表示升序,满足升序关系 --> flag1=1
	int flag2 = 0; //表示降序,满足降序关系 --> flag2=1
	//判断过程中,
	//如果 flag1一直为1,flag2一直为0,说明是升序,
	//如果 flag2一直为1,flag1一直为0,说明是降序
	//等于的话,flag1和flag2都是0

	//相邻两个数进行比较,N个数,比较 N-1 对:
	for (i = 0; i < N-1; i++)
	{
		if (arr[i] < arr[i + 1])
		//前一个数 小于 后一个数
		{
			flag1 = 1;
			//满足升序条件
		}
		else if (arr[i] > arr[i + 1])
		//前一个数 大于 后一个数
		{
			flag2 = 1;
			//满足降序条件
		}
	}

	//判断完 升序 或 降序 后,看 flag1 和 flag2 的情况来判断是否有序:
	if (flag1 + flag2 == 2)//说明既有 大于 又有 小于
	{
		printf("unsorted\n");
	}
	else
	// ==1 的话,说明有序;==0 的话,说明都相等,也是有序
	{
		printf("sorted\n");
	}

	return 0;
}

Realize the picture:

                    

Idea 1: Final code and implementation effect

Final code:

#include <stdio.h>
int main()
{
	//输入数组长度N:
	int N = 0; //数组长度
	//输入:
	scanf("%d", &N);

	//输入N个整数,用一个数组进行存储:
	int arr[50] = { 0 };
	//输入N个整数:
	int i = 0;
	for (i = 0; i < N; i++)
	{
		scanf("%d", &arr[i]);
	}

	//判断是否有序:升序 或 降序
	//有序的情况下,都是 大于号 或 都是 小于号

	int flag1 = 0; //表示升序,满足升序关系 --> flag1=1
	int flag2 = 0; //表示降序,满足降序关系 --> flag2=1
	//判断过程中,
	//如果 flag1一直为1,flag2一直为0,说明是升序,
	//如果 flag2一直为1,flag1一直为0,说明是降序
	//等于的话,flag1和flag2都是0

	//相邻两个数进行比较,N个数,比较 N-1 对:
	for (i = 0; i < N-1; i++)
	{
		if (arr[i] < arr[i + 1])
		//前一个数 小于 后一个数
		{
			flag1 = 1;
			//满足升序条件
		}
		else if (arr[i] > arr[i + 1])
		//前一个数 大于 后一个数
		{
			flag2 = 1;
			//满足降序条件
		}
	}

	//判断完 升序 或 降序 后,看 flag1 和 flag2 的情况来判断是否有序:
	if (flag1 + flag2 == 2)//说明既有 大于 又有 小于
	{
		printf("unsorted\n");
	}
	else
	// ==1 的话,说明有序;==0 的话,说明都相等,也是有序
	{
		printf("sorted\n");
	}

	return 0;
}

Realize the effect:

                    

 =========================================================================

                       

Idea 2: Judging while inputting

(After entering two numbers, you can judge the size once)

general idea:

On the basis of idea one :

             

Input the (2) of the first step of idea 1 into N numbers

Put it in the for loop of the second step  (2) ,

          

If two numbers are entered , the comparison starts

                


              

first step:

(1).

Input the (2) of the first step of idea 1 into N numbers

Put it in the for loop of the second step  (2) ,

          

(2).

If two numbers are entered , the comparison starts

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入数组长度N:
	int N = 0; //数组长度
	//输入:
	scanf("%d", &N);

	//输入N个整数,用一个数组进行存储:
	int arr[50] = { 0 };

	//判断是否有序:升序 或 降序
	//有序的情况下,都是 大于号 或 都是 小于号

	int flag1 = 0; //表示升序,满足升序关系 --> flag1=1
	int flag2 = 0; //表示降序,满足降序关系 --> flag2=1
	//判断过程中,
	//如果 flag1一直为1,flag2一直为0,说明是升序,
	//如果 flag2一直为1,flag1一直为0,说明是降序
	//等于的话,flag1和flag2都是0

	int i = 0;
	for (i = 0; i < N; i++)
	{
		//输入:
		scanf("%d", &arr[i]);

		//输入了两个数后就开始比较
		if (i >= 1)
		// i=1 说明输入了 arr[0] 和 arr[1] ,已经输入了两个数
		{
			if (arr[i] < arr[i - 1])
				//因为进来后 i 以及是下标1了,还要判断下标0,arr[0]
				//一个数 小于 前一个数
			{
				flag1 = 1;
				//满足升序条件
			}
			else if (arr[i] > arr[i + 1])
				//一个数 大于 后一个数
			{
				flag2 = 1;
				//满足降序条件
			}
		}

	}

	//判断完 升序 或 降序 后,看 flag1 和 flag2 的情况来判断是否有序:
	if (flag1 + flag2 == 2)//说明既有 大于 又有 小于
	{
		printf("unsorted\n");
	}
	else
		// ==1 的话,说明有序;==0 的话,说明都相等,也是有序
	{
		printf("sorted\n");
	}

	return 0;
}

Realize the picture:

                    

Idea 2: Final code and implementation effect

Final code:

#include <stdio.h>
int main()
{
	//输入数组长度N:
	int N = 0; //数组长度
	//输入:
	scanf("%d", &N);

	//输入N个整数,用一个数组进行存储:
	int arr[50] = { 0 };

	//判断是否有序:升序 或 降序
	//有序的情况下,都是 大于号 或 都是 小于号

	int flag1 = 0; //表示升序,满足升序关系 --> flag1=1
	int flag2 = 0; //表示降序,满足降序关系 --> flag2=1
	//判断过程中,
	//如果 flag1一直为1,flag2一直为0,说明是升序,
	//如果 flag2一直为1,flag1一直为0,说明是降序
	//等于的话,flag1和flag2都是0

	int i = 0;
	for (i = 0; i < N; i++)
	{
		//输入:
		scanf("%d", &arr[i]);

		//输入了两个数后就开始比较
		if (i >= 1)
		// i=1 说明输入了 arr[0] 和 arr[1] ,已经输入了两个数
		{
			if (arr[i] < arr[i - 1])
				//因为进来后 i 以及是下标1了,还要判断下标0,arr[0]
				//一个数 小于 前一个数
			{
				flag1 = 1;
				//满足升序条件
			}
			else if (arr[i] > arr[i + 1])
				//一个数 大于 后一个数
			{
				flag2 = 1;
				//满足降序条件
			}
		}

	}

	//判断完 升序 或 降序 后,看 flag1 和 flag2 的情况来判断是否有序:
	if (flag1 + flag2 == 2)//说明既有 大于 又有 小于
	{
		printf("unsorted\n");
	}
	else
		// ==1 的话,说明有序;==0 的话,说明都相等,也是有序
	{
		printf("sorted\n");
	}

	return 0;
}

Realize the effect:

Guess you like

Origin blog.csdn.net/weixin_63176266/article/details/131307744