Last active
April 21, 2020 14:55
-
-
Save paxbun/da6e7bf04c92a3f85e946f17b9629254 to your computer and use it in GitHub Desktop.
5-7장 과제 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
| // 직각삼각형 | |
| #include <stdio.h> | |
| int main() | |
| { | |
| int n; | |
| scanf("%d", &n); | |
| for (int i = 1; i <= n; ++i) | |
| { | |
| if (n % 2 != 0) | |
| for (int j = 1; j < i; ++j) printf(" "); | |
| for (int j = n; j >= i; --j) printf("*"); | |
| printf("\n"); | |
| } | |
| } |
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
| // 모래시계 | |
| #include <stdio.h> | |
| int main() | |
| { | |
| int n; | |
| scanf("%d", &n); | |
| for (int i = 1; i <= n; ++i) | |
| { | |
| for (int j = 1; j < i; ++j) printf(" "); | |
| for (int j = 1; j <= (n - i) * 2 + 1; ++j) printf("*"); | |
| printf("\n"); | |
| } | |
| for (int i = n - 1; i >= 1; --i) | |
| { | |
| for (int j = 1; j < i; ++j) printf(" "); | |
| for (int j = 1; j <= (n - i) * 2 + 1; ++j) printf("*"); | |
| printf("\n"); | |
| } | |
| } |
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
| // 격자 | |
| #include <stdio.h> | |
| int main() | |
| { | |
| int n; | |
| scanf("%d", &n); | |
| if (n == 1) | |
| printf("*"); | |
| else | |
| { | |
| for (int i = 1; i < n; ++i) printf("+-"); | |
| printf("+\n"); | |
| for (int i = 1; i < n; ++i) | |
| { | |
| for (int j = 1; j < i; ++j) printf(" "); | |
| printf("| "); | |
| for (int j = 1; j < n - i; ++j) printf("1 "); | |
| printf("|"); | |
| printf("\n"); | |
| for (int j = 1; j < i; ++j) printf(" "); | |
| printf("+-"); | |
| for (int j = 1; j < n - i; ++j) printf("+="); | |
| printf("+"); | |
| printf("\n"); | |
| } | |
| } | |
| } |
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
| // for문을 이용한 제곱근 구하는 프로그램 | |
| #include <stdio.h> | |
| int main() | |
| { | |
| double d; | |
| scanf("%lf", &d); | |
| double x = d; | |
| for (int i = 0; i < 1000; ++i) x -= (x * x - d) / (2 * x); | |
| printf("%lf", x); | |
| } |
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
| // while문을 이용한 제곱근 구하는 프로그램 | |
| #include <stdio.h> | |
| int main() | |
| { | |
| double d; | |
| scanf("%lf", &d); | |
| double x = d; | |
| int i = 0; | |
| while (i < 1000) | |
| { | |
| x -= (x * x - d) / (2 * x); | |
| ++i; | |
| } | |
| printf("%lf", x); | |
| } |
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
| // for문을 이용한 피보나치 구하는 프로그램 | |
| #include <stdio.h> | |
| int main() | |
| { | |
| int arr[45] = { 1, 1 }, n; | |
| scanf("%d", &n); | |
| for (int i = 0; i < n && i < 2; ++i) | |
| printf("%d ", arr[i]); | |
| for (int i = 2; i < n; ++i) | |
| { | |
| arr[i] = arr[i - 1] + arr[i - 2]; | |
| printf("%d ", arr[i]); | |
| } | |
| } |
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
| // while문을 이용한 피보나치 구하는 프로그램 | |
| #include <stdio.h> | |
| int main() | |
| { | |
| int n; | |
| scanf("%d", &n); | |
| int small = 1, big = 1; | |
| while (n-- > 0) | |
| { | |
| printf("%d ", small); | |
| int new = small + big; | |
| small = big; | |
| big = new; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment