백준 BOJ C# 1193 분수찾기
2020. 2. 18. 20:01ㆍ백준 알고리즘 단계별/수학 1 단계
반응형

백준 BOJ C# 1193 분수찾기
출제 링크 : https://www.acmicpc.net/problem/1193
1193번: 분수찾기
첫째 줄에 X(1 ≤ X ≤ 10,000,000)가 주어진다.
www.acmicpc.net
접근 방법
규칙성을 찾는게 포인트 인거 같아 이리저리 찾아봤다.



이 규칙성들은.. 코드로 만들기에는 부족했다. 그 뒤로 찾아본 규칙성인데

각 대각선을 Level로 구성한다면
Level이 짝수 (파란색) : 분자++ 분모--
Level이 홀수 (노란색) : 분자-- 분모++
이 규칙성을 가지고 코드를 작성하면 될꺼같다.
1. N개가 어느 Level인지 판단할 코드
2. 각 분자, 분모가 순차적으로 증가될 값 판단
코드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using static System.Console; | |
class MainApp | |
{ | |
static void Main() | |
{ | |
int X = int.Parse(ReadLine()); | |
int level = 1; | |
int temp = 1; | |
while (temp < X) | |
{ | |
level++; | |
temp = temp + level; | |
} | |
//WriteLine("현재 temp는 : " + temp); | |
//WriteLine("현재 Level은 : " + level); | |
int den = temp - X + 1; | |
//WriteLine("현재 dir는 : {0}", den); | |
// 짝수( 분자++ 분모--) | |
if (level % 2 == 0) | |
WriteLine($"{level + 1 - den}/{den}"); | |
else // 홀수 ( 분자-- 분모++) | |
WriteLine($"{den}/{level + 1 - den}"); | |
} | |
} |
반응형