`
阿尔萨斯
  • 浏览: 4180124 次
社区版块
存档分类
最新评论

C++ - 不重复地输出升序数组中的元素

 
阅读更多

#include <iostream>

using namespace std;

void outputUnique(char * str, int n)
{
	if(n <= 0)
	{
		return;
	}
	else if(n == 1)
	{
		putchar(str[0]);
	}
	else
	{
		int i = 0, j = 1;
		putchar(str[0]);
		while(j < n)
		{
			if(str[j] != str[i])
			{
				putchar(str[j]);
				i = j;
			}

			++j;
		}
	}
}

void main()
{
	outputUnique("abbbcdde", 8);
}

// Output:
/*
abcde
*/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics