[BOJ] 1158 요세푸스 문제
Algorithm 2023. 1. 17. 22:32반응형
https://www.acmicpc.net/problem/1158
472ms
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace _1406
{
class Program
{
static void Main()
{
int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
int n = input[0];
int k = input[1];
Queue<int> q = new Queue<int>();
for(int i =1; i<= n; i++) q.Enqueue(i);
Console.Write("<");
while (q.Count > 1)
{
for (int i = 1; i < k; i++)
{
int temp = q.Dequeue();
q.Enqueue(temp);
}
Console.Write("{0}, ", q.Dequeue());
}
Console.WriteLine("{0}>", q.Dequeue());
}
}
}
372ms
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace _1158
{
class Program
{
static void Main()
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
StringBuilder sb = new StringBuilder();
int[] input = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
int n = input[0];
int k = input[1];
Queue<int> q = new Queue<int>();
for(int i =1; i<= n; i++) q.Enqueue(i);
// sw.Write("<");
sb.Append("<");
while (q.Count > 1)
{
for (int i = 1; i < k; i++)
{
int temp = q.Dequeue();
q.Enqueue(temp);
}
//sw.Write("{0}, ", q.Dequeue());
sb.Append(string.Format("{0}, ", q.Dequeue()));
}
// sw.WriteLine("{0}>", q.Dequeue());
sb.Append(string.Format("{0}>", q.Dequeue()));
sw.WriteLine(sb.ToString());
sr.Close();
sw.Flush();
sw.Close();
}
}
}
반응형
'Algorithm' 카테고리의 다른 글
[BOJ] 10808 알파벳 개수 (0) | 2023.01.17 |
---|---|
[BOJ] 10866 덱 (0) | 2023.01.17 |
[프로그래머스] 최소 직사각형 (0) | 2023.01.17 |
[BOJ] 10845 큐 (0) | 2023.01.17 |
[BOJ] 1406 에디터 (0) | 2023.01.17 |