对数组元素的一些简单操作
求两个有序数组的公共元素
#include<stdio.h>
#include<stdlib.h>
#define N 7
#define M 5
void intersection(int array1[], int array2[])
{
int i = 0,j = 0;
while (i < M&&j < N)
{
while (array1[i] < array2[j])
i++;
while (array1[i] > array2[j])
j++;
while (array1[i] == array2[j])
{
printf("%d ", array1[i]);
i++, j++;
}
}
printf("\n");
}
int main()
{
int array1[M] = {1,3,4,5,8};
int array2[N] = { 2, 3, 5, 6, 7, 8, 11};
intersection(array1, array2);
system("pause");
return 0;
}
求三个有序数组的公共元素
#include<stdio.h>
#include<stdlib.h>
#define N 7
void intersection(int array1[], int array2[],int b[])
{
int i = 0, j = 0,k = 0;
while (i < N&&j < N)
{
while (array1[i] < array2[j] && i<N)
i++;
while (array1[i] > array2[j] && j<N)
j++;
while (array1[i] == array2[j] && i<N&&j<N)
{
b[k++] = array1[i];
i++, j++;
}
}
}
int main()
{
int i;
int a1[N] = { 1, 2, 3, 4, 5, 6, 7 };
int a2[N] = { 1, 3, 5, 7, 8, 9, 11 };
int a3[N] = { 2, 4, 5, 6, 7, 8, 9 };
int b[N] = { 0 }, b2[N] = { 0 };//数组元素全部初始化为0
intersection(a1,a2,b);
intersection(a3,b,b2);
for (i = 0; b2[i]; i++)
{
printf("%d ", b2[i]);
}
printf("\n");
system("pause");
return 0;
}
求n个有序数组的公共元素
#include<stdio.h>
#include<stdlib.h>
#define N 7
#define M 5
void intersection(int array1[], int array2[], int b[])
{
int i = 0, j = 0, k = 0;
while (i < N&&j < N)
{
while (array1[i] < array2[j] && i<N)
i++;
while (array1[i] > array2[j] && j<N)
j++;
while (array1[i] == array2[j] && i<N&&j<N)
{
b[k++] = array1[i];
i++, j++;
}
}
}
int main()
{
int i;
int a[M][N] = { { 1, 3, 4, 7, 8, 9, 10 }, { 2, 3, 5, 6, 7, 8, 11 }, { 1, 2, 3, 4, 5, 7 ,8}, { 1, 3, 4, 6, 7, 8, 10 }, { 2, 5, 6,
7, 8, 15, 17 } };
int b[M - 1][N] = { { 0 }, { 0 }, { 0 }, { 0 } };
intersection(a[0], a[1], b[0]);
for (i = 2; i < M; i++)
{
intersection(a[i],b[i-2],b[i-1]);//循环时,要注意避免数组下标越界
}
for (i = 0; b[M-2][i]; i++)//公共元素存放在了最后一行中
{
printf("%d\n", b[M - 2][i]);
}
system("pause");
return 0;
}
求数组的最大值和次大值
//未使用冒泡排序
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[20];
int max1, max2;
int n;
while(scanf("%d", &n)!=EOF)
{
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
max1 = max2 = a[0];
for (int i = 1; i < n; i++)
{
if (max1 < a[i])
max1 = a[i];
}
for (int i = 1; i < n; i++)
{
if (max2 < a[i] && a[i] < max1)
max2 = a[i];
}
printf("MAX=%d\tSecond_MAX=%d\n", max1, max2);
}
return 0;
}
给定一个 n 个整型元素的数组 a,其中有一个元素出现次数超过 n / 2,求这个元素
#include<stdio.h>
#include<string.h>
int n;
int func(int a[])
{
int i, j, t;
//冒泡排序
for (i = 0; i < n; i++)
{
for (j = 0; j < i; j++)
{
if (a[i] < a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
return a[n/2];
}
int main()
{
int a[20];
int countmax;
while (~scanf("%d", &n))
{
memset(a, 0, sizeof(a));
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
countmax = func(a);
printf("%d\n", countmax);
}
return 0;
}
给定一个含有n个元素的整型数组,找出数组中的两个元素x和y使得abs(x - y)值最小
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define INF 1e9+7
void min_abs(int a[],int n)
{
int minn, x, y;
minn = INF;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (minn > abs(a[i] - a[j]))
{
minn = abs(a[i] - a[j]);
x = i, y = j;
}
}
}
printf("%d %d\n", a[x], a[y]);
}
int main()
{
int a[20];
int n;
while (~scanf("%d", &n))
{
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
min_abs(a, n);
}
return 0;
}
给定含有1001个元素的数组,其中存放了1-1000之内的整数,只有一个整数是重复的,请找出这个数
//思路分析: A1 + … + A1001 – (1 + …+ 1000)
#include <stdio.h>
#include <stdlib.h>
int repeat_num(int array[])
{
int i;
long sum = 0;
for (i = 0; i <= 1000; i++)
sum = sum + array[i] - i;
return sum;
}
int main()
{
int array[1001];
int num;
int i;
for (i = 0; i<500; i++)
array[i] = i + 1;
array[i++] = 520; //array[500]的值:520
for (; i<1001; i++)
array[i] = i;
num = repeat_num(array);
printf("%d\n", num);
system("pause");
return 0;
}