Question 1: Store the numbers in the array in reverse order
This question requires writing a program to store the given n integers into an array, store the n numbers in the array in reverse order, and then output the elements in the array in order.
Algorithm description: 1. Input elements into array a;
2. Define a new array new and store the elements in array a in reverse order;
3. Output the array b in positive order, paying attention to the format issue without spaces at the end .
Code:
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i,arr[n];
for(i = 0;i < n;i++)
{
scanf("%d",&arr[i]);
}
int b[n]; //定义一个数组new用来存入数组a的逆顺序
for(int i = 0;i < n;i++)
{
b[i] = arr[n-1-i]; //倒序存入
}
for(int k = 0;k < n - 1;k++) //数组中最后一个元素不一起输入,保持结尾无空格
{
printf("%d ",b[k]);
}
printf("%d",b[n-1]); //输出最后一个元素
return 0;
}
operation result:
![](https://img-blog.csdnimg.cn/b9039f73ff8f4809834a062bb5c1dade.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAd2VpeGluXzYwNTc2NDgy,size_11,color_FFFFFF,t_70,g_se,x_16)
Question 2: Find the maximum value and its subscript in the array
This question requires writing a program to find the maximum value among the given n numbers and its corresponding minimum subscript (the subscript starts from 0). Input format: The input gives a positive integer n (1<n≤10) in the first line. In the second line, enter n integers, separated by spaces. Output format: Output the maximum value and the minimum subscript of the maximum value in one line, separated by a space.
Algorithm description: 1. Store elements in the array;
2. Define a variable to save the first element in the array;
3. Traverse the array. If it is larger than the variable holding the first element, assign it to that variable.
Until the variable value becomes the maximum;
4. Define a variable to record the subscript of the maximum value.
Code:
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
int arr[n],i;
for(i = 0;i < n;i++)
{
scanf("%d",&arr[i]);
}
int max = arr[0]; //定义一个变量保存数组第一个元素
for(int j = 1;j < n;j++)
{
if(max < arr[j]) //如果小于就一直给m,直到找到最大值停
{
max = arr[j];
}
}
for(int num = 0;num < n;num++) //记录最大值下标操作
{
if(arr[num] == max)
{
printf("最大值为:%d\n其下标为:%d",max,num);
break;
}
}
return 0;
}
operation result:
Question 3: Find elements that are not common to two arrays
This question requires that two integer arrays are given, and this question requires finding elements that are not common to both.
Algorithm description: 1. Define two arrays and enter values;
2. Use the elements in array a to compare the elements in array b, and put the non-shared values into c (a flag variable flag can be used here to mark whether there are shared values);
3. Use the elements in array b to compare the elements in array a, and repeat the "2" operation;
4. Finally, output the first element in array c, and then determine whether the element is the same as the previously entered element.
repeat.
5. Finally, print out the elements in array c, which are non-shared values. Pay attention to the format issue here.
Code:
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
int arr1[n],i; //定义第一个数组
for(i = 0;i < n;i++)
{
scanf("%d",&arr1[i]);
}
int m;
scanf("%d",&m);
int arr2[m],j; //定义第二个数组
for(j = 0;j < m;j++)
{
scanf("%d",&arr2[j]);
}
int k,rem[20] = {}; //数组rem是用来记录非公有值的
for(i = 0;i < n;i++)
{
int flag = 0; //定义标志变量用来标志是否有非共有值
for(j = 0;j < m;j++)
{
if(arr1[i] == arr2[j]) //用数组1中元素对比数组2中元素,非共有值放入数组rem中
{
flag = 1;
}
}
if(flag == 0)
{
rem[k] = arr1[i];
k++;
}
}
for(i = 0;i < m;i++) //用数组2中元素对比数组1中元素,非共有值放入数组rem中
{
int flag = 0;
for(j = 0;j < n;j++)
{
if(arr2[i] == arr1[j])
{
flag = 1;
}
}
if(flag == 0)
{
rem[k] = arr2[i];
k++;
}
}
printf("非共有元素:%d",rem[0]); //输出rem中第一个元素
for(i = 1;i < k;i++)
{
for(j = 0;j < i;j++)
{
if(rem[i] == rem[j])
{
break;
}
}
if(j >= i)
{
printf(" %d",rem[i]); //因为提前输出第一个元素,所以后面要打一个空格再打元素,
//结尾无空格的格式问题
}
}
return 0;
}
operation result:
Question 4: Find the number that appears most often
This question requires counting the most frequent integers in an integer sequence and their number of occurrences.
Input format: Enter the number N of integers in the sequence (0<N≤1000) and N integers in one line. Separate numbers with spaces.
Output format: Output the integer with the most occurrences and the number of occurrences in one line, with spaces separating the numbers. The question ensures that such a number is unique .
Algorithm description: 1. Define two arrays, one used to store elements and the other used as a counter. Note that this array needs to be initialized;
2. Find duplicates in this array, and then assign +1 to the counter array;
3. Keep traversing to find the most repeated one. After finding it, assign the counter array element to the variable max (that is, the number of occurrences). The counter array subscript is the subscript with the most occurrences.
Code:
#include <stdio.h>
int main()
{
int n,arr[1000],cnt[1000] = {0}; //cnt为计数器数组,需要初始化
int i,j,k,max = 0;
scanf("%d",&n);
for(i = 0;i < n;i++)
{
scanf("%d",&arr[i]);
}
for(i = 0;i < n;i++)
{
for(j = 0;j < n;j++)
{
if(arr[i] == arr[j]) //找到一样的
{
cnt[i] = cnt[i] + 1; //cnt[i]++
}
}
}
for(i = 0;i < n;i++)
{
if(max < cnt[i]) //一直遍历直到找重复最多的
{
max = cnt[i]; //找到后赋值给max
k = i; //i就是重复最多的数的下标
}
}
printf("出现次数最多的数字为:%d\n其出现次数为:%d",arr[k],max);
return 0;
}
operation result:
Question 5: Insert numbers into the array and sort them
This question requires giving an array of n elements, arranged from small to large, and then inputting a num, inserting it into the array, so that the new array is still in order from small to large (completed with an array).
Algorithm description: 1. Define an array and store the elements, and record the penultimate element separately;
2. Enter the number to be inserted. Note that special circumstances can be considered. If the inserted number is greater than the original second to last number, you can directly insert and assign the value to the last element;
3. Consider the general situation again, traverse the array, if the original number is greater than the number to be inserted, save the original number first, and then put the inserted number into the position of the original number;
4. After insertion, the original number reaches the next digit. You need to save the position of the last digit, put the original number into the next digit, and then assign the position to the value of the original number;
5. Finally traverse the output array elements.
Code:
#include <stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
int arr[n],t1,t2,num,end;
for(i = 0;i < n;i++)
{
scanf("%d",&arr[i]);
}
scanf("%d",&num); //输入要插入的数字
end = arr[n-1]; //保存倒数第二个元素
if(num > end) //考虑特殊情况,如果插入的数大于倒数第二个
{
arr[n] = num; //直接插入赋值
}
else
{
for(i = 0;i < n;i++)
{
if(arr[i] > num) //如果原本的数大于插入的数
{
t1 = arr[i]; //先保存原来的数
arr[i] = num; //把插入的数放到原来数的位置
for(j = i + 1;j <= n;j++) //处理原来的数
{
t2 = arr[j]; //原来的数到了后一位保存住
arr[j] = t1; //把原来的数赋值给现在要存的这个位置上
t1 = t2;
}
break;
}
}
}
for(i = 0;i <= n;i++) //注意这里i<=n,因为多插入了一个数字
{
printf("%d ",arr[i]);
}
return 0;
}
operation result:
Question 6: Array circular right shift problem
There are N (>0) integers in an array A. On the premise that other arrays are not allowed, each integer is cyclically shifted to the right by M (≥0) positions, that is, the data in A is changed from ( A 0 A 1⋯*AN −1) is transformed into ( AN − M ⋯ AN −1 A 0 A 1⋯ AN − M −1) (the last M numbers are circularly moved to the first M* positions). If you need to consider the number of times the program moves data as little as possible, how should you design a moving method?
Input format:
Each input contains a test case. The first line inputs N (1≤ N ≤100) and M (≥0); the second line inputs N integers, separated by spaces.
Output format:
Output the integer sequence rotated right by M bits in one line , separated by spaces, and there must be no extra spaces at the end of the sequence .
Input example:
6 2 1 2 3 4 5 6
No blank line at the end
Output sample:
5 6 1 2 3 4
No blank line at the end
Algorithm description: 1. Define the array and store the elements, enter the value to be moved;
2. Pay attention to processing the moved value first, so that it is the remainder of the total number of elements in the array;
3. Give priority to special cases: if the moved value is 0, print directly;
4. Move k items, put the last k values in front first, and do special processing to facilitate the printing of the remaining elements;
5. Then pay attention to the format problem, print out the numbers except the last one + spaces, and finally print the last number.
Code:
#include <stdio.h>
int main()
{
int n,move;
scanf("%d %d",&n,&move);
int arr[n],i;
for(i = 0;i < n;i++)
{
scanf("%d",&arr[i]);
}
move %= n; //取余算出到底移动多少,进行简化
if(move == 0) //特殊情况特殊处理
{
for(i = 0;i < n - 1;i++)
{
printf("%d",arr[i]); //注意格式问题
}
printf("%d",arr[n-1]);
return 0;
}
for(i = n - move;i < n;i++) //移动move,把最后move个数放前面
{
printf("%d ",arr[i]);
}
for(i = 0;i < n - move - 1;i++) //把剩下除了最后一个数,打印出来
{
printf("%d ",arr[i]);
}
printf("%d",arr[n-1-move]); //最后一个没空格
return 0;
}