[백준] 1105 - 팔
1. 문제
2. 개요
L 이상 R 이하인 수 중에서 8의 개수가 가장 적은 수의 8의 개수를 출력하는 문제.
3. 풀이 및 코드
3-1. 풀이
두 수의 자릿수가 다른 경우 100…. 이 두 수 사이에 확정으로 들어가있기 때문에 답은 0이 된다.
또한 두 수의 같은 자리의 수가 다른 경우 그 뒷 자리 이하가 0… 이 확정이기 때문에 더 볼 필요가 없다.
따라서 두 수의 앞의 자릿수부터 둘 다 8인지, 서로 같기만 한 지를 비교해나가면 된다.
3-2. Python
import sys
L, R = sys.stdin.readline().split()
cnt = 0
if len(L) == len(R):
for i in range(len(L)):
if L[i] == R[i] == '8':
cnt += 1
elif L[i] == R[i]:
continue
else:
break
print(cnt)
3-3. C#
namespace boj_1105
{
internal class Program
{
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split();
string L = input[0], R = input[1];
int cnt = 0;
if (L.Length == R.Length)
{
for (int i = 0; i < L.Length; i++)
{
if (L[i] == '8' && L[i] == R[i])
cnt++;
else if (L[i] == R[i])
continue;
else
break;
}
}
Console.WriteLine(cnt);
}
}
}
Comments