山形数组按序不重复输出(双指针)

在这里插入图片描述

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	vector<int>v;
	int a;
	while (cin >> a)
	{
		v.push_back(a);
	}
	int i = 0, j = v.size() - 1;
	
	while (i <= j)
	{
		if (v[i] < v[j])
		{
			int temp = v[i];
			cout << temp << endl;
			while (v[i] == temp)
			{
				i++;
			}
		}
		else if (v[i] > v[j])
		{
			int temp = v[j];
			cout << temp << endl;
			while (v[j] == temp)
			{
				j--;
			}
			
		}
		else//相等的情况
		{
			int temp = v[i];
			cout << temp << endl;
			while (v[i] == temp)
			{
				i++;
			}
			while (v[j] == temp)
			{
				j--;
			}
		}
	}

	system("pause");
	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ShenHang_/article/details/107434269