合并两个降序数组,使合并后的数组仍然保持降序排列
求解 怎么写[ 本帖最后由 CC小学徒 于 2014-11-23 21:48 编辑 ]
#include <stdio.h> int main(void) { #define N 6 int a[N] = {1, 2, 2, 3, 5, 6}; int b[N] = {1, 1, 3, 4, 6, 10}; int c[N*2] = {0}; for (int res = 0, i = 0, j = 0; res != N*2; ++res) { if (a[i] < b[j]) { c[res] = a[i]; ++i; if (i == N) { for (++res; j != N; ++j, ++res) { c[res] = b[j]; } break; } } else { c[res] = b[j]; ++j; if (j == N) { for (++res; i != N; ++i, ++res) { c[res] = a[i]; } break; } } } for (int res = 0; res != N*2; ++res) { printf("%d ",c[res]); } printf("\n"); #undef N return 0; }
#include <stdio.h> #define N 6 void main() { int iStu[N],i,k,iTemp; for(i=0;i<N;i++) scanf("%d",&iStu[i]); for(k=1;k<=N-1;k++) { for(i=N-1;i>=k;i--) { if(iStu[i]>iStu[i-1]) { iTemp=iStu[i]; iStu[i]=iStu[i-1]; iStu[i-1]=iTemp; } } } printf("\nThe sorted array is:"); for(i=0;i<N;i++) printf("%6d",iStu[i]); getch(); }
#include <stdio.h> #define N 6 void main() { int iStu[N],i,k,iTemp; for(i=0;i<N;i++) scanf("%d",&iStu[i]); for(k=1;k<=N-1;k++) { for(i=N-1;i>=k;i--) { if(iStu[i]>iStu[i-1]) { iTemp=iStu[i]; iStu[i]=iStu[i-1]; iStu[i-1]=iTemp; } } } printf("\nThe sorted array is:"); for(i=0;i<N;i++) printf("%6d",iStu[i]); getch(); }