题目标题:
将两个整型数组按照升序合并,并且过滤掉重复数组元素。
输出时相邻两数之间没有空格。
#include <stdio.h> #include <stdlib.h> #include <string.h> int compare_func(int *a,int *b) { return *a > *b; } int main() { int* pArrA = 0; int* pArrB = 0; int* pArrOut = 0; int cntA = 0; int cntB = 0; scanf("%d", &cntA); pArrA = malloc(sizeof(int) * cntA); for (int i = 0; i < cntA; i++) { scanf("%d", pArrA + i); } scanf("%d", &cntB); pArrB = malloc(sizeof(int) * cntB); for (int i = 0; i < cntB; i++) { scanf("%d", pArrB + i); } int cntOut = cntA+cntB; pArrOut = malloc(sizeof(int)*cntOut); memcpy(pArrOut, pArrA, sizeof(int)*cntA); memcpy((char *)pArrOut + sizeof(int)*cntA, pArrB, sizeof(int)*cntB); qsort(pArrOut, cntOut, sizeof(int), compare_func); int val = *(pArrOut); printf("%d", val); for(int i=1;i<cntOut;i++){ if(*(pArrOut + i) != val){ printf("%d", *(pArrOut + i)); val = *(pArrOut + i); } } return 0; }
#include <stdio.h> #include<stdlib.h> int comp(const void*a,const void*b) //用来做比较的函数。 { return *(int*)a-*(int*)b; } int main() { int m,n,a[1000] = {0},i = 0; scanf("%d",&m); for(i = 0; i<m; i++)scanf("%d",&a[i]); scanf("%d",&n); for(; i<m+n; i++)scanf("%d",&a[i]); qsort(a,n+m,sizeof(int),comp); //调用qsort排序 for(int i = 0, temp = 0; i<m+n; i++){ if(a[i] != temp)printf("%d",a[i]),temp = a[i]; } return 0; }
#include <stdio.h> #define N 1000 int main() { int arr[N],m,i=0,j,temp,k=2; while(k>0) { scanf("%d",&m); while(m>0) { scanf("%d",&temp); if(i==0) arr[i++]=temp; else { for(j=0;j<i;j++) { if(temp==arr[j]) break; } if(j==i) arr[i++]=temp; } m--; } k--; } m=i; //sort for(i=0;i<m-1;i++) { for(j=0;j<m-1-i;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } for(i=0;i<m;i++) { printf("%d",arr[i]); } return 0; }我直接用一个数组完成,大力出奇迹。。。
#include "stdio.h" #include "string.h" /*快速排序之确定枢纽节点*/ int partition(int A[], int low, int high) { int pivot = A[low]; while(low < high) { while(low < high && A[high] >= pivot) high--; A[low] = A[high]; while(low < high && A[low] < pivot) low++; A[high] = A[low]; } A[low] = pivot; return low; } //快速排序递归函数 void QuickSort(int A[], int low, int high) { if(low<high) { int pivotpos = partition(A,low,high);//划分 QuickSort(A, low, pivotpos-1);//划分左表 QuickSort(A, pivotpos+1, high);//划分右表 } } int main() { int N = 0; int M = 0; while(scanf("%d", &N) != EOF) { int ZeroFlag = 0; int a[1000] = {0}; for(int i=0; i<N; i++) { scanf("%d", &a[i]); } scanf("%d", &M); for(int i=0; i<M; i++) { scanf("%d", &a[i+N]); } #if 0 int temp = 0; /*冒泡排序*/ int flag, ExcPos; for(int i= 1; i<(N+M); i++) { flag = 0; for(int j=N+M-1; j>=i; j--) { if(a[j-1] > a[j]) { temp = a[j-1]; a[j-1] = a[j]; a[j] = temp; flag = 1;//本次有过交换,置1。 ExcPos = j;//记录交换的位置,j前面的数据已经有序 } } if(!flag)//数组已经有序 break; i = ExcPos; i--; } #elif 0 /*插入排序*/ int temp = 0; for(int i=1; i<N+M; i++) { int flag = 0; if(a[i-1] > a[i]) { temp = a[i]; int j; for(j=i; j>=1 && a[j-1] > temp; j--) { a[j] = a[j-1]; } a[j] = temp; } } #elif 1 /*快速排序*/ QuickSort(a, 0, N+M-1); #endif for(int i=0; i<(N+M); i++) { if(a[i-1] != a[i]) printf("%d", a[i]); } printf("\n"); } return 0; }