Array arrangement of C language experiment course

It's really hard. Take notes.

[10 points] A. experiment 7-1-1 simplified insertion sorting
Title Description

This problem requires writing a program to insert a given integer into the original ordered integer sequence, so that the result sequence is still orderly.

input

Input the non negative integer N (< = 10) in the first line; The second line gives N integers in order from small to large; The third line gives an integer X.

output

Output a sequence of integers that are still ordered from small to large after inserting X in one line, with a space after each number

sample input
5
1 2 4 5 7
3

sample output
1 2 3 4 5 7

#include<stdio.h>
int main()
{
	int n=0;
	int t=0;
	int a[11]={0};
	//First store the numbers in array a 
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	//Then save the number to be inserted into the last element of array a 
	scanf("%d",&a[n]);
	//Select Sorting: compare a[i] with the elements behind array a one by one, adjust the smaller number to the left, move to the next bit with the increase of I, and continuously adjust the smaller number to the left
	for(int i=0;i<=n;i++)
	{
		for(int j=i+1;j<=n+1;j++)
		{
			if(a[i]>a[j])
			{
				t=a[i];
				a[i]=a[j];
				a[j]=t;
			}
		}
	}
	//Traverse the array and output 
	for(int k=0;k<=n;k++)
	{
		printf("%d ",a[k]);
	}
	return 0;
 } 

[20 points] B. experiment 7-1-5 exchange minimum and maximum values
Title Description

This problem requires writing a program to exchange the minimum value of the input series of integers with the first number, then exchange the maximum value with the last number, and finally output the exchanged sequence.

Note: the title ensures that the maximum and minimum values are unique.

input

Input gives a positive integer n (≤ 10) in the first line, and N integers in the second line, separated by spaces.

output

The exchanged sequence is output sequentially in one line, with each integer followed by a space.

sample input
5
8 2 5 1 4

sample output
1 2 5 4 8

#include<stdio.h>
int main()
{
	int n=0;
	int a[10]={0};
	int max=0;
	int min=0;
	int t=0;
	scanf("%d",&n);
	//Store all numbers in array a 
	for (int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	//Find the smallest element in array A: compare the element size, find the smaller one, and assign the subscript value to min. After the loop, a[min] is the smallest in array a 
	for(int i=1;i<n;i++)
	{
			if(a[i]<a[min])
			{
				min=i;
			}
	}
	//Swap the smallest element in array a with a[0] 
	t=a[min];
	a[min]=a[0];
	a[0]=t;
	//Repeat the above operation to find the maximum value 
	for(int i=1;i<n;i++)
	{
		if(a[i]>a[max])
		{
			max=i;
		}
	}
	//Swap the maximum value with the last position of the element array 
	t=a[max];
	a[max]=a[n-1];
	a[n-1]=t;
	//Traverse and print the elements of array a 
	for(int j=0;j<n;j++)
	{
		printf("%d ",a[j]);
	}
	return 0;
 } 

C. Experiment 7-1-6 find the most digits in a batch of integers
Title Description

Given a batch of integers, analyze each digit of each integer and find the digits with the most occurrences. For example, given three integers 1234, 2345 and 3456, the numbers with the most occurrences are 3 and 4, both of which occur three times.

input

Input: the positive integer n (1 ≤ n ≤ 1000) is given in the first line, and N non negative integers not exceeding the integer range are given in the second line, and the numbers are separated by spaces.

output

Output in the format "M: n1, n2..." in one line, where M is the maximum number of occurrences, and n1, n2,... Are the digits with the most occurrences, arranged from small to large. Numbers are separated by spaces, but there must be no extra spaces at the end.

sample input
3
1234 2345 3456

sample output
3: 3 4

#include<stdio.h>
int FindMax(int fqy[]);
int main()
{
	int num[1000]={0};
	int n=0;
	int digit=0;
	int Max=0;
	int fqy[10]={0};
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&num[i]);     //The array num is used to store several groups of input numbers 
	}
	for(int i=0;i<n;i++)
	{
		while(num[i]>0)
		{
			digit=num[i]%10;       //Implement bit by bit counting 
			fqy[digit]++;          //Clever handling: here fqy, the subscript in the array exactly corresponds to digit, and its value appears the number of times 
			num[i]=num[i]/10;
		}
	}
	Max=FindMax(fqy);
	printf("%d: ",Max);
	for(int j=0;j<10;j++)
	{
		if(fqy[j]==Max)
		{
			printf("%d ",j);       //Clever handling: if the value of fqy element of frequency array is exactly equal to the maximum value, print the subscript of fqy array 
		}
	}
	return 0;
 } 
int FindMax(int fqy[])          //Function to find the largest value in the fqy array, that is, the one with the highest frequency 
{
	int max=fqy[0];
	for(int i=0;i<10;i++)
	{
		if(fqy[i]>max)
		{
			max=fqy[i];
		}
	}
	return max;
}

D. Experiment 7-1-8 finds out the elements that are not common to two arrays
Title Description

Given two integer arrays, this problem requires finding elements that are not common to both.

input

Input gives two integer arrays in two lines respectively. Each line first gives a positive integer n (≤ 20), followed by N integers separated by spaces.

output

In a line, the elements that are not common to two groups of numbers are output in the order given by the numbers. The numbers are separated by spaces, but there must be no redundant spaces at the end of the line. The title guarantees that at least one such number exists. The same number is not output repeatedly.

sample input
10 3 -5 2 8 0 3 5 -15 9 100
11 6 4 8 2 6 -5 9 0 100 8 1

sample output
3 5 -15 6 4 1

#include<stdio.h>
int main()
{
	int n=0;
	int m=0;
	int flag=0;
	int a[20]={0};
	int b[20]={0};
	int c[20]={0};
	int k=0;
	//First store the numbers in the a and b arrays 
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	scanf("%d",&m);
	for(int j=0;j<m;j++)
	{
		scanf("%d",&b[j]);
	}
	//Compare the elements of array a with array b 
	for(int i=0;i<n;i++)
	{
		flag=1;
		for(int j=0;j<m;j++)
		{
			if(a[i]==b[j])
			{
				flag=-1;         //If an element of array a is the same as that of array b, the flag will be put down 
			}
		}
		if(flag==1)             //If the flag is not placed, that is, an element of array a is different from all elements of array b 
		{
			c[k]=a[i];              //Save this element in array c 
			for(int m=0;m<k;m++)    //Loop to compare the newly stored elements of array c with the previously stored elements 
			{
				if(c[k]==c[m])
				{
					flag=-1;             //If the elements are the same, the flag is put down 
				}
			}
			k++;                       
			if(k!=1&&flag==1)
			{
				printf(" ");            //Control space output: K= 1 ensure that there are no spaces in front of the first output and no spaces in the output of the last element 
			}
			if(flag==1)
			{
				printf("%d",a[i]);      //If the flag is not put down, it will be output 
			}
		}
	}
	//The following is to compare the elements of array b with array a and repeat the above operation 
	for(int i=0;i<m;i++)
	{
		flag=1;
		for(int j=0;j<n;j++)
		{
			if(a[j]==b[i])
			{
				flag=-1;
			}
		}
		if(flag==1)
		{
			c[k]=b[i];
			for(int m=0;m<k;m++)
			{
				if(c[k]==c[m])
				{
					flag=-1;
				}
			}
			k++;
			if(k!=1&&flag==1)
			{
				printf(" ");
			}
			if(flag==1)
			{
				printf("%d",b[i]);
			}
			
		}
	}
	return 0;
}

E. Experiment 7-1-9 find the number with the largest number of occurrences in the integer sequence
Title Description

This question requires statistics on the integer with the most occurrences in an integer sequence and its occurrences.

input

Enter the number of integers n (0 < n ≤ 1000) and N integers in the sequence given in one line. Numbers are separated by spaces.

output

In one line, the integer with the most occurrences and its occurrences are output, and the numbers are separated by spaces. The title guarantees that such numbers are unique.

sample input
10 3 2 -1 5 3 4 3 0 3 2

sample output
3 4

#include <stdio.h>
int main() 
{
    int n=0;
	int a[1000];
	int b[1000];
	int max=0;
	int k = 0;
    scanf("%d", &n);
    for (int i=0; i<n; i++) 
	{
        scanf("%d", &a[i]);        //The read data is stored in array a
    }
    for (int i=0; i<n; i++) 
	{ 
        b[i] = 0;                  //Initialize array b, which is used to calculate the number of repetitions of each element in array a
    }
    for (int i=0; i<n; i++) 
	{
        for (int j=0; j<n; j++) 
		{
            if(a[i]==a[j])
            {
            	b[i]++;	/*Judge whether the elements in array a are repeated, record the number of times the elements in array a are repeated and put them into array b. Note: here, element 3 of array A is repeated four times in array b, occupying the positions of the four elements of array b*/
			}              
        }
    }
    max = b[0];   //The predecessor specifies that the maximum number of repetitions is the first element of array b
    for (int i=1; i<n; i++) 
	{
        if(b[i]>max)            //Further comparison, find out the one with the largest number of real repetitions, assign it to max, and assign the subscript to k
		{
            max = b[i]; 
            k = i;              //k here corresponds to i repeated at the beginning of array a one-to-one
        }
    }
    printf("%d %d", a[k], max);     //So a[k] here is the element with the most repetitions, and max is the number of repetitions
}

F. Experimental group 7-1-10: minimum number of decimals
Title Description

Give a number of numbers 0-9. You can arrange these numbers in any order, but you must use them all. The goal is to make the final number as small as possible (note that 0 cannot be the first). For example, given two zeros, two 1s, three 5S and one 8, the minimum number we get is 10015558.

Given the number, please write a program to output the smallest number that can be composed.

input

The input gives 10 non negative integers in one line, and the order indicates that we have the number of numbers 0, 1,... 9. Integers are separated by a space. The total number of 10 numbers shall not exceed 50, and there shall be at least 1 non-zero number.

output

Output the smallest number that can be composed in one line.

sample input
2 2 0 0 0 3 0 0 1 0

sample output
10015558

#include<stdio.h>
int main()
{
	int n=10;
	int num=0;
	int m=0;
	int a[10]={0};
	for(int i=0;i<n;i++)
	{
		scanf("%d",&num);
		a[i]=num;
	}
	for(int i=1;i<n;i++)
	{
		if(a[i]!=0)
		{
			m=i;
			break;
		}
	}
	printf("%d",m);
	a[m]-=1;
	for(int j=0;j<n;j++)
	{
		if(a[j]!=0)
		{
			for(int k=0;k<a[j];k++)
			{
				printf("%d",j);
			}
		}
	}
	return 0;
}

I can only say that I'm really too delicious. I really have to spend more time on C language.

Keywords: C Algorithm

Added by Janco on Sat, 11 Dec 2021 07:23:16 +0200