알고리즘/백준 BOJ
[BOJ C#] 4796 캠핑
왹져박사
2023. 2. 1. 03:01
처음에 쉬워보여서 구조 자체는 빨리 짰지만 함정이 있던 문제. +오타, \n 조심
using System;
using System.Text;
using System.IO;
namespace _4796
{
class Program
{
static void Main()
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
StringBuilder sb = new StringBuilder();
int caseN = 0;
int result = 0;
while (true)
{
if (caseN > 0)
{
sb.AppendFormat("Case {0}: {1}", caseN, result);
result = 0;
}
string[] str = sr.ReadLine().Split();
int l = int.Parse(str[0]);
int p = int.Parse(str[1]);
int v = int.Parse(str[2]);
if (l == 0 && p == 0 && v == 0)
{
break;
}
else if (caseN > 0)
sb.AppendLine();
while (v != 0)
{
if (v >= p)
{
v -= p;
result += l;
}
else if (v < p && v > l)
{
result += l;
v = 0;
}
else
{
result += v;
v = 0;
}
}
caseN++;
}
sw.WriteLine(sb);
sr.Close();
sw.Flush();
sw.Close();
}
}
}
v의 값이 p보다 작지만 l보다 클 때, v를 더하는것이 아닌 l을 더해주어야 모든 케이스의 답이 나온다.
+다른분들의 코드는 나머지연산을 이용, 고쳐보기