1.(10分)给定一个排好序的数组,去除数组中重复的元素,保证每个元素只出现一次例如 1,1,2返回1,2 输入输出格式 printf("Please input the arraysize\n"); scanf("%d"), for) ( printf("Please input the %d-th number\n"); scanf("%d"); 了 printf("values are %d\n"); for) ( printf("%d\n"): 〕 函数原型为 int removeDuplicates(int nums[], int numsSize) 注,不考虑非法输入
时间: 2023-10-30 07:05:59 浏览: 76
下面是代码实现:
```
#include <stdio.h>
int removeDuplicates(int nums[], int numsSize) {
if (numsSize <= 1) {
return numsSize;
}
int j = 0;
for (int i = 1; i < numsSize; i++) {
if (nums[i] != nums[j]) {
j++;
nums[j] = nums[i];
}
}
return j + 1;
}
int main() {
int n;
printf("Please input the array size:\n");
scanf("%d", &n);
int nums[n];
for (int i = 0; i < n; i++) {
printf("Please input the %d-th number:\n", i + 1);
scanf("%d", &nums[i]);
}
int len = removeDuplicates(nums, n);
printf("Values are:\n");
for (int i = 0; i < len; i++) {
printf("%d\n", nums[i]);
}
return 0;
}
```
如果输入为 1,1,2,输出为:
```
Please input the array size:
3
Please input the 1-th number:
1
Please input the 2-th number:
1
Please input the 3-th number:
2
Values are:
1
2
```
阅读全文