๐Ÿ™‡โ€โ™€๏ธ์ด๋ถ„ ๊ฒ€์ƒ‰

int n, key;
vector<int> seq;

void solve()
{
	cin >> n >> key;
	seq = vector<int>(n);
	for (int i = 0; i < n; ++i)
		cin >> seq[i];

	sort(seq.begin(), seq.end());

	int lt = 0, rt = n - 1, mid, res = 0;
	while (lt <= rt)
	{
		mid = (lt + rt) / 2;

		if (seq[mid] == key)
		{
			res = mid + 1;
			cout << res;
			return;
		}
		else if (seq[mid] > key)
		{
			rt = mid - 1;
		}
		else if (seq[mid] < key)
		{
			lt = mid + 1;
		}
	}
}

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int n, key;
vector<int> seq;

void solve()
{
	cin >> n >> key;
	seq = vector<int>(n);
	for (int i = 0; i < n; ++i)
		cin >> seq[i];

	sort(seq.begin(), seq.end());

	int lt = 0, rt = n - 1, mid, res = 0;
	while (lt <= rt)
	{
		mid = (lt + rt) / 2;

		if (seq[mid] == key)
		{
			res = mid + 1;
			cout << res;
			return;
		}
		else if (seq[mid] > key)
		{
			rt = mid - 1;
		}
		else if (seq[mid] < key)
		{
			lt = mid + 1;
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: