반응형
728x170
https://www.hackerrank.com/challenges/for-loop-in-c/problem
For Loop in C | HackerRank
Learn how to use for loop and print the output as per the given conditions
www.hackerrank.com
배경지식
일반적으로 for문은 이렇게 생겼다.
(<>표현은 따라하는 게 아니라 그냥 구분하기 쉬우라고 써놓은 것이다.)
for ( <expression_1> ; <expression_2> ; <expression_3> )
<statement>
-
expression_1 is used for intializing variables which are generally used for controlling the terminating flag for the loop.
-
제어변수에 관한 초기값을 넣는다.
-
expression_2 is used to check for the terminating condition. If this evaluates to false, then the loop is terminated.
-
종료조건을 넣는다.
-
expression_3 is generally used to update the flags/variables.
-
인자변환을 넣는다. (제어변수가 변해야 종료조건에 도달했을 때 종료된다. 그렇지 않으면 무한으로 반복되어서 오류남)
그건 그렇고
%기호가 나머지를 구할 떄 쓰이는데
a%b는 a를 b로 나눈 나머지란 뜻이다.
짝수는 2로나누면 0 홀수는 2로나누면 1을 나머지로 가질 것이다.
1.비교를 해야하는 입력이 있다.
2. 비교를 하려면 배열에 넣어서 비교가 편하다
3. 숫자도 구별해야하고 홀수 짝수도 비교해야한다.
4. 함수를 만들자
5. 만들어서 main함수에서 호출하자
6. 완성
728x90
반응형
그리드형
'문제풀이(Problem Solving)' 카테고리의 다른 글
C언어 예제 11 "1D Arrays in C" [HackerRank] (0) | 2020.03.19 |
---|---|
C언어 예제 8 "Sum of Digits of a Five Digit Number" [HackerRank] (0) | 2020.03.17 |
C언어 예제 5 "Pointers in C" [HackerRank] (0) | 2020.03.16 |
C언어 예제 4 "Functions in C" [HackerRank] (0) | 2020.03.16 |
C언어 예제 3 "Sum and Difference of Two Numbers" [HackerRank] (0) | 2020.03.15 |