1.序列中删除指定的数字
2.序列中整数去重
方法1:
方法2:
3.有序序列合并
#include<stdio.h>
int main()
{
int n = 0;
int arr[50] = { 0 };
int delete = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
scanf("%d", &delete);
int i = 0;
int j = 0;
for (i = 0, j = 0; i < n; i++)
{//i的作用:遍历数组
//j的作用:记录存放数据位置的下标
if (arr[i] != delete)
{
arr[j++] = arr[i];
}
//此时的j中存放的数据就是删除元素后剩下的元素个数
}
for (int i = 0; i < j; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
(与前面的元素进行比较)
#include<stdio.h>
int main()
{
int n = 0;
int arr[1000] = { 0 };
scanf("%d", &n);
int i = 0;
int j = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
//去重
j = 1;
for (i = 1; i < n; i++)//从第二个开始进行遍历
{
int flag = 0;
for (int k = 0; k < i; k++)
{
if (arr[k] == arr[i])
{
flag = 1;
}
}
if (flag != 1)
{
arr[j++] = arr[i];
}//此处的方法类似于上面那个题的方法
}
for (int i = 0; i < j; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
#include<stdio.h>
int main()
{
int n = 0;
int arr[1000] = { 0 };
scanf("%d", &n);
int i = 0;
int j = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
//去重
for (i = 0; i < n; i++)
{
//判断arr[i]是否在后面出现过
int j = 0;
for (j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
{
//去重,即后面的元素向前面进行覆盖
int k = 0;
for (k = j; k < n - 1; k++)
{
arr[k] = arr[k + 1];
}
n--;
j--;//防止覆盖上来的仍然与前面的arr[i]元素重复
}
}
}
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
#include<stdio.h>
#include<malloc.h>
int main()
{
int n = 0;
int m = 0;
int all = m+n;
int i = 0;
//输入n和m
scanf("%d %d",&n,&m);
int *arr1 = (int *)malloc(sizeof(int)*n);
int *arr2 = (int *)malloc(sizeof(int)*m);
int *arr3 = (int *)malloc(sizeof(int)*(m+n));
//输入两个有序数组
for(int i = 0;i<n;i++)
{
scanf("%d",&arr1[i]);
}
for(int i = 0;i<m;i++)
{
scanf("%d",&arr2[i]);
}
//合并数组
i = 0;
int j = 0;
int k = 0;
while(i<n&&j<m)
{
if(arr1[i]<arr2[j])
{
arr3[k++] = arr1[i++];
}
else
{
arr3[k++] = arr2[j++];
}
}
if(i==n)
{
for(;j<m;j++)
{
arr3[k++] = arr2[j];
}
}
else
{
for(;i<n;i++)
{
arr3[k++] = arr1[i];
}
}
for( i =0;i<k;i++)
{
printf("%d ",arr3[i]);
}
return 0;
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/m0_57304511/article/details/122901120
内容来源于网络,如有侵权,请联系作者删除!