要求:设计算法,在数组r[n]中删除重复的元素,要求移动元素的次数较少并使剩余元素间的相对次序保持不变.
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
void test(int *a, int *b,int n);
int main()
{
int a[10] = {
1,1,1,4,3,6,5,7,3,3 };
int b[10] = {
0 };
test(a,b, 10);
return 0;
}
void test(int *a, int *b, int n)
{
int i = 0,j = 0,count=1,flag;
b[0] = a[0];
for (i = 1,flag=1; i < n; i++) {
for (j = 0; j < count; j++) {
if (a[i] == b[j]) {
flag = 0;//判断是否有重复元素
}
}
while (flag ==1) {
//有重复元素
b[count] = a[i];
cout << b[count] << "b[count]" << endl;
count++;
}
}
for (int k = 0; k < count; k++) {
cout << b[k]<<"b[k]" << endl;
}
}
编译没有错误,结果不对,算法是否有问题?