๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/๊ตญ๋น„์ง€์›

[D+17] ์ฝ”๋”ฉ๋ฐ์ด

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2022. 10. 14.

 

 

๊ตญ๋น„์ง€์› D+17

-  ์ฝ”๋”ฉ๋ฐ์ด -

 


 

์ฝ”๋”ฉ๋ฐ์ด

Q) ํ•œ๋ฐ˜์˜ ์„ฑ์ ํ‘œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์˜ค๋ฆ„์ฐจ์ˆœํ•˜์—ฌ ๋“ฑ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด๋ณด์‹œ์˜ค.

 

• Main

public class Test1 {

	public static void main(String[] args) {
		ScoreSort1 score = new ScoreSort1();

		int[] arr = new int[] {56, 44, 87, 90, 68, 98, 77, 90, 97, 98};

		score.setScoreArr(arr);
		score.bubbleSort();
		score.printArr();

	}

}

 

 

 

• ScoreSort Class

public class ScoreSort1 {

	// ๋ณ€์ˆ˜ ์„ ์–ธ
	private int[] scoreArr;			// ์„ฑ์ ๋ฐฐ์—ด

	/////////////////////////////////////////////////////////
	// Setter

	public void setScoreArr(int[] scores)
	{
		scoreArr = new int[scores.length];

		for(int i = 0; i < scores.length; i++)
		{
			scoreArr[i] = scores[i];
		}
	}

	/////////////////////////////////////////////////////////
	// Getter

	public int[] getScoreArr()
	{
		return scoreArr;
	}

	/////////////////////////////////////////////////////////
	// ์˜ค๋ฆ„์ฐจ์ˆœ ๋ฒ„๋ธ”์ •๋ ฌ ๋ฉ”์†Œ๋“œ

	public boolean bubbleSort()
	{
		int temp;
		boolean swapFlag = false;

		for(int i = 0; i < scoreArr.length - 1; i++)
		{
			for(int j = 0; j < scoreArr.length - 1 - i; j++)
			{
				if(scoreArr[j] > scoreArr[j + 1])
				{
					temp = scoreArr[j];
					scoreArr[j] = scoreArr[j + 1];
					scoreArr[j + 1] = temp;

					swapFlag = true;
				}
			}

			// ๋ฒ„๋ธ”์ •๋ ฌ ์ตœ์ ํ™”
			if(swapFlag == true)	// swap๋œ ์  ์—†๋‹ค๋ฉด --> ์ •๋ ฌ์™„๋ฃŒ์ƒํƒœ
			{
				return true;
			}

			else 					// swap๋œ ์  ์žˆ๋‹ค๋ฉด --> ์ •๋ ฌ์ง„ํ–‰์ƒํƒœ
			{
				swapFlag = false;
			}
		}

		return true;
	}

	/////////////////////////////////////////////////////////
	// ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ๋“ฑ์ˆ˜๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ

	public void printArr()
	{
		int rank = 0;						// ๋“ฑ์ˆ˜
		int index = scoreArr.length - 1;	// ๋’ค์— ์žˆ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ’๋ถ€ํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•œ index ๋ณ€์ˆ˜

		int sameScoreCnt = 1;				// ๊ฐ™์€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜
		int beforeScore = -1;				// ์ด์ „ ๋ฐ์ดํ„ฐ์˜ ์ ์ˆ˜

		for(int i = 0; i < scoreArr.length; i++)
		{
			if(beforeScore != scoreArr[index])
			{
				rank = rank + sameScoreCnt;
				beforeScore = scoreArr[index];
				sameScoreCnt = 1;
			}

			else
			{
				sameScoreCnt++;
			}

			System.out.println(rank + "๋“ฑ : " + scoreArr[index]);

			index--;
		}
	}


}
 

Q) Queue๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์‹œ์˜ค.

 

 

• Main

import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		// ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐฐ์—ด์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
		System.out.print("์›ํ•˜๋Š” ๋ฐฐ์—ด์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : ");
		int size = scan.nextInt();

		Queue2 q = new Queue2(size);

		boolean bResult;
		int data;

		bResult = q.enqueue(11);
		bResult = q.enqueue(22);
		bResult = q.enqueue(33);
		bResult = q.enqueue(44);
		bResult = q.enqueue(55);

		bResult = q.enqueue(66);	// error

		data = q.dequeue();
		data = q.dequeue();
		data = q.dequeue();
		data = q.dequeue();
		data = q.dequeue();

		data = q.dequeue();	// error

		bResult = q.enqueue(77);
		bResult = q.enqueue(88);
		bResult = q.enqueue(99);

		data = q.dequeue();
		data = q.dequeue();

		q.print();

	}

}

 

• Queue Class

public class Queue2 {

	// Attribute
	private int[] frame;		// queue data
	private int count;			// queue count (queue์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋ช‡๊ฐœ ์žˆ๋Š”๊ฐ€?)

	private int front;			// dequeue์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์œ„์น˜๊ฐ’ (์ถœ๋ ฅ)
	private int rear;			// enqueue์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์œ„์น˜๊ฐ’ (์ž…๋ ฅ)

	//////////////////////////////////////////////////////////////////////
	// Constructor

	// ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
	public Queue2()
	{
		frame = null;
		count = 0;

		front = 0;
		rear = 0;
	}

	// ์˜ค๋ฒ„๋กœ๋”ฉ๋œ ์ƒ์„ฑ์ž
	public Queue2(int size)
	{
		frame = new int[size];
		count = 0;

		front = 0;
		rear = 0;
	}

	//////////////////////////////////////////////////////////////////////
	// Method

	// getter
	public int getFrameSize()
	{
		return frame.length;
	}

	public int getCount()
	{
		return count;
	}


	// queue๋ฅผ ์ดˆ๊ธฐํ™”
	public void init(int size)
	{
		frame = new int[size];
		count = 0;

		front = 0;
		rear = 0;
	}


	// ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ
	// true : ๋ฐ์ดํ„ฐ๋ฅผ ์ •์ƒ์ ์œผ๋กœ frame์— ์ €์žฅํ–ˆ๋‹ค.
	// false : ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. (queue-full)
	public boolean enqueue(int data)
	{
		// queue-full ๋ผ๋ฉด return
		if(getCount() == getFrameSize())
		{
			return false;
		}

		// rear์œ„์น˜์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
		frame[rear] = data;

		// rear์œ„์น˜๋ฅผ ์กฐ์ •ํ•œ๋‹ค.
		rear++;
		rear = rear % getFrameSize();

		// ์นด์šดํŠธ๋ฅผ ์ฆ๊ฐ€ํ•œ๋‹ค.
		count++;

		return true;
	}


	// frame์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ
	// ์—๋Ÿฌ๋ฐœ์ƒ์‹œ ์ •์ˆ˜ํ˜•์˜ ์ตœ์†Œ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
	public int dequeue()
	{
		int returnValue;

		// queue-empty๋ฉด return
		if(getCount() == 0)
		{
			return Integer.MIN_VALUE;
		}

		// front ์œ„์น˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
		returnValue = frame[front];

		// front์œ„์น˜๋ฅผ ์กฐ์ •ํ•œ๋‹ค.
		front++;
		front = front % getFrameSize();

		// count๋ฅผ ๊ฐ์†Œํ•œ๋‹ค.
		count--;

		return returnValue;
	}


	// ๋ฐ์ดํ„ฐ ๊ฐ’์ด ๋“ค์–ด๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ’๊ณผ ์ธ๋ฑ์Šค์œ„์น˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ
	public void print()
	{
		int index = front;

		for(int i = 0; i < getCount(); i++)
		{
			System.out.println("index = " + index + ", value = " + frame[index]);

			index++;
			index = index % getFrameSize();
		}
	}
}

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€