문제 링크입니다: https://www.acmicpc.net/problem/14913
14913번: 등차수열에서 항 번호 찾기
k가 몇 번째 항인지 출력한다. 만약, k가 주어진 a와 d로 만들어진 등차수열의 수가 아니면 "X"를 출력한다.
www.acmicpc.net
예외 처리를 할 때, (k-a)가 d로 나누어 떨어지지 않는 경우와 (k-a)가 d로 나누어 떨어지지만 몫이 음수인 경우도 생각해줘야합니다.
This file contains 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
#include <iostream> | |
using namespace std; | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int a, d, k; | |
cin >> a >> d >> k; | |
if ((k - a) % d || (k-a) / d < 0) | |
cout << "X\n"; | |
else | |
cout << (k - a) / d + 1 << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 16236번 아기 상어 (2) | 2019.04.10 |
---|---|
백준 14911번 궁합 쌍 찾기 (0) | 2019.04.10 |
백준 14914번 사과와 바나나 나누기 (0) | 2019.04.09 |
백준 2239번 스도쿠 (0) | 2019.04.08 |
백준 14890번 경사로 (2) | 2019.04.04 |