zoukankan      html  css  js  c++  java
  • 编程题之合并两个有序的数组

    给定两个有序的数组,试将其合并后的数组打印出来。。。

    #include<iostream>
    #include<vector>
    #include<algorithm>

    using namespace std;

    void arr_merge(int arr1[], int arr2[], int n1, int n2)
    {
     int arr[200];
     int i=0;
     int j=0;
     int k=0;
     while((i<n1)&&(j<n2))
     {
      if(arr1[i]<=arr2[j])
      {
       arr[k]=arr1[i];
       i++;
       k++;
      }
      else
      {
       arr[k]=arr2[j];
       j++;
       k++;
      }
     }
     while(i<n1)
     {
      arr[k++]=arr1[i++];
     }
     while(j<n2)
     {
      arr[k++]=arr2[j++];
     }
     cout<<"The result " << endl;
     for(int p=0;p<k;p++)
      cout<<arr[p]<<" ";
     cout<<endl;

    }

    int main()
    {
     int a1[100];
     int a2[100];
     int n1;
     int n2;
     cout<<"input the arr1 num : " << endl;
     cin>>n1;
     for(int i=0;i<n1;i++)
     {
      cin>>a1[i];
     }
     cout<<"input the arr2 num: " << endl;
     cin>>n2;
     for(int i=0;i<n2;i++)
     {
      cin>>a2[i];
     }
     sort(a1,a1+n1);
     sort(a2,a2+n2);
     arr_merge(a1, a2, n1, n2);
     system("pause");
     return 0;
    }

    Fight fight fight ! 你有你的奇迹 ! Fight fight fight ! Just to be yourself !
  • 相关阅读:
    mysql日志
    验证栈序列
    限流方案分析
    集合
    数据结构-树
    链表的中间节点
    PHP实现链表
    php扩展安装方式
    2017 Multi-University Training Contest
    用LCT解一类动态图的问题
  • 原文地址:https://www.cnblogs.com/sjlove/p/3162264.html
Copyright © 2011-2022 走看看