《編程思維與實(shí)踐》1028.排序去重
題目

思路
法一(最方便):直接將所有數(shù)據(jù)進(jìn)行排序,相同數(shù)據(jù)則會(huì)排在相鄰位置,控制輸出保證不輸出相同數(shù)據(jù)即可.
法二(桶排序):桶排序就是將數(shù)組分到有限數(shù)量的桶里再對(duì)每個(gè)桶進(jìn)行個(gè)別排序.在這題中注意到數(shù)據(jù)最大只有1000,只需要開大小為1001的數(shù)組,之后將出現(xiàn)的數(shù)字?jǐn)?shù)組存為1,不出現(xiàn)存0,輸出時(shí)順序或逆序輸出不為0的數(shù)組對(duì)應(yīng)的數(shù)字即可.
注意的點(diǎn):
一組數(shù)據(jù)的輸入可以直接用EOF來判斷輸入是否結(jié)束,如果是多組數(shù)據(jù)則需要分割字符串.
代碼
法一:
int?cmp1(const?void?*a,const?void?*b)
{
????int?*m=(int*)a;
????int?*n=(int*)b;
????return?*m-*n;???????????????//升序?
}
int?cmp2(const?void?*a,const?void?*b)
{
????int?*m=(int*)a;
????int?*n=(int*)b;
????return?*n-*m;???????????????????????//降序?
}
int?main()
{
????char?judge;
????judge=getchar();
????int?number[100];
????int?i=0;
????while(scanf("%d",&number[i])!=EOF)????//EOF判斷一組數(shù)據(jù)輸入結(jié)束
????{
????????i++;
????}
????if(judge=='A')
????{
????????qsort(number,i,sizeof(int),cmp1);??
????}
????if(judge=='D')
????{
????????qsort(number,i,sizeof(int),cmp2);
????}
????for(int?j=0;j<i;j++)
????{
????????printf("%d?",number[j]);
????????while(number[j]==number[j+1])??//相同只輸出一個(gè)
????????{?
????????????j++;
????????}
????}
????return?0;?
}
法二:
int?main()
{
????char?judge;
????judge=getchar();
????int?tab[1001]={0};
????int?number;
????while(scanf("%d",&number)!=EOF)????//EOF判斷一組數(shù)據(jù)輸入結(jié)束
????{
????????if(tab[number]==0)
????????{
????????????tab[number]=1;
????????}
????}
????if(judge=='A')
????{
????????for(int?j=0;j<1001;j++)
????????{
????????????if(tab[j]==1)
????????????{
????????????????printf("%d?",j);
????????????}
????????}?
????}
????if(judge=='D')
????{
????????for(int?j=1000;j>=0;j--)
????????{
????????????if(tab[j]==1)
????????????{
????????????????printf("%d?",j);
????????????}
????????}?
????}
????return?0;?
}