(代码哪里出错了,望告知)在数组r[n]中删除重复的元素

要求:设计算法,在数组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;
	}
}

编译没有错误,结果不对,算法是否有问题?

猜你喜欢

转载自blog.csdn.net/qq_45784099/article/details/114835492