c program

2008-01-06 5:29 am
Write a program that removes all duplicate values from a sorted array of integers, leaving only a single copy of each value.

Note : My computer cannot read iostream.

THX!

回答 (1)

2008-01-06 8:38 am
✔ 最佳答案
Here's the answer, but a detailed program complete with test cases is found at the following link:
http://mathmate.brinkster.net/programming.htm

int dedup(int s[], int n)
{
// s is an array of SORTED integers which stores unique values on return
// the return value is the number of unique elements.
int d=0;// number of duplicate elements
int i;
for(i=0;i+1+d<n;i++)
{
while(i+1+d<n && s[i]==s[i+1+d])d++; // increment duplicate count if found
s[i+1]=s[i+1+d];// move unique element into next position
}
return n-d;// number of elements - duplicate = unique
}


2008-01-06 00:39:15 補充:
Here is an example of the output:012223344456667789990 1 2 3 4 5 6 7 8 9there are 10 unique elements


收錄日期: 2021-04-13 14:53:02
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080105000051KK04382

檢視 Wayback Machine 備份