#20263: 檢查陣列內的數字是否重複


youweiko1212@gmail.com (柯祐緯1212)

學校 : 不指定學校
編號 : 113054
來源 : []
最後登入時間 :
2019-12-19 19:20:23
. Unfinished! | From: [36.232.25.37] | 發表日期 : 2019-12-19 19:34

要檢查陣列內的數字是否有重複,如果有的話是誰重複了,重複了幾次

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(void)
{
int n,number[1000],i,a;
srand(time(NULL));
replay:
printf("請問需要幾個亂數(需小於1000個):");
scanf("%d",&n);
if(n>1000)
{
printf("輸入的數不可大於1000\n");
goto replay;
}
else
{
for(i=0;i<n;i++)
{
number[i]=(rand()%100)+1;
}
}
printf("排序前:");
for(a=0;a<i;a++)
{
printf("%d ",number[a]);
}
printf("\n");
int k=0,j=0;
int temp=0;
for(k=0;k<n;k++)
{
for(j=k;j<n;j++)
{
if(number[j]<number[k])
{
temp=number[j];
number[j]=number[k];
number[k]=temp;
}
}
}
printf("排序後:");
for(j=0;j<n;j++)
{
printf("%d ",number[j]);
}
//-----------------------------------------------------------------以下是我自己打的程式,但有問題,求解
int l=0,x=0,y=0,re[1001];
for(x=0;x<n;x++)
{
if(number[x]==number[x+1])
{
y+=1;
for(x=x;number[x]==number[x+1];x++)
{
l+=1;
}

re[y]=l;
re[y+1]=number[x];
}
}
for(y=1;y<1001;y+=2)
{
if(re[y]!=0)
{
printf("\n%d重複了%d次",re[y+1],re[y]);
}
}


system("pause");
return 0;
}

 

 
ZeroJudge Forum