用随机数生成1~20之间的数来填充一个长度为50的数组并输出,
然 后 剔 除 其 中 连 续 相 同 的 数 后 再 次 输 出 处 理 后 的 数 组 , 即
{....4,2,2,2,3....}剔除后变成{...4,2,3...}
#include <stdio.h>
#include <string.h>
void removeDuplicate(char str[]);
int main (void) {
char name[] = "hello";
removeDuplicate(name);
printf("%s\n", name);
return 0;
}
void removeDuplicate(char str[]) {
int len = strlen(str);
int p = 0;
int i;
int j;
for (i=0; i<len; i++) {
if (str[i] != '\0') {
str[p++] = str[i];
for (j=i+1; j<len; j++) {
if (str[i] == str[j]) {
str[j] = '\0';
}
}
}
}
str[p] = '\0';
}
简单,可以用C语言里面的数据结构,字典或集合都可以。
#include <stdio.h>
#include <time.h>
#define N 50
int main(void)
{
int count =N;
int a[N];
srand((unsigned int)time(NULL));
for(int i = 0; i < count; i++)
{
a[i] = rand() % 20 + 1;
printf("%4d", a[i]);
}
printf("\n\n");
for(int i = 0,j; i < count - 1; i++)
{
if(a[i] == a[i + 1])
{
for(j = i + 1; j < count - 1; j++)
{
a[j] = a[j + 1];
}
a[j]=-1;
count--;
i--;
}
}
for(int i = 0; i < count; i++)
{
printf("%4d", a[i]);
}
printf("\n");
return 0;
}