数组操作【数组】
时间: 1ms 内存:128M
描述:
在数组 A[1..n]中有n个数据,试建立一个带有头结点的循环链表,头指针为h,要求链中数据从小到大排列,重复的数据在链中只保存一个。
输入:
21 32 43 12 34 34 23 12 22
输出:
示例输入:
21 32 43 12 34 34 23 12 22
示例输出:
12 21 22 23 34 43
提示:
参考答案(内存最优[1268]):
#include <iostream>
#include <cstdlib>
using namespace std;
struct number
{
int numb;
number *next;
}num[100];
int main()
{
int (*facm)(const void*,const void*);
int hehe(const void*,const void*);
struct number *h=&num[0];
int i=0,j,k=0;
int n[100];
while(cin>>n[i])
{
i++;
}
facm=hehe;
qsort(n,i,sizeof(int),facm);
for(j=0;j<i;j++)
{
if(j==0)
{
h->numb=n[j];
h->next=&num[++k];
h=h->next;
}
else if(n[j]!=n[j-1])
{
h->numb=n[j];
h->next=&num[++k];
h=h->next;
}
}
h->next=NULL;
h=&num[0];
do
{ if(h->numb!=32)
{
if((h->next->next)!=NULL) cout<<h->numb<<" ";
else cout<<h->numb<<endl;
}
h=h->next;
}while((h->next)!=NULL);
return 0;
}
int hehe(const void*a,const void*b)
{
return (*(int*)a-*(int*)b);
}
参考答案(时间最优[0]):
#include <stdio.h>
#include <iostream>
#include<algorithm>
using namespace std;
#include <string.h>
int main()
{
int a[99],i;
for(i=0;~scanf("%d",a+i);i++);
int n=i;
sort(a,a+n);
printf("%d",a[0]);
for(i=1;i<n;i++)
{
for(;a[i]==a[i-1];i++);
if(i<n&&a[i]!=32)printf(" %d",a[i]);
}
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。
