分析:

方法1:先使用数组a记录,然后使用双重循环逐个比较,把不重复的数值记录到b中。时间复杂度O(n^2)(n=10000)超时;

方法2:先使用数组a记录,然后对数组进行排序。再扫描一遍a,将不重复的数值记录在b数组.

 STL里面有个sort函数,sort 可以对给定区间所有元素进行排序,默认的排序方式是升序。
但使用这个函数,需要包含头文件<algorithm>和using namespace std;
 使用方法举例:
int a[1000],要对从a[0]到a[49]的元素进行排序,只要写sort(a,a+50)就行了。

 (    sort函数可以传两个参数或三个参数。第一个参数是要排序的区间首地址,第二个参数是区间的尾地址的下一地址。)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[21000],b[21000];
int main(){
    int x,y,n,maxn=-1,num=1,t=0;
    scanf("%d",&n);
    for(int i=0;i<=n-1;i++)    scanf("%d",&a[i]);
    sort(a,a+n);
    b[0]=a[0];
    for(int i=1;i<n;i++)    
        if (a[i]==a[i-1]);
        else {
            t++;b[t]=a[i];
        }
    for(int i=0;i<=t-1;i++) printf("%d ",b[i]);
    printf("%d\n",b[t]);            
    return 0;
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-03-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
相关资源
相似解决方案