๐Ÿ–ค Algorithm/CodeUp : C์–ธ์–ด ๊ธฐ์ดˆ 100์ œ

[์ฝ”๋“œ์—… CodeUp] C์–ธ์–ด ๊ธฐ์ดˆ 100์ œ 1071๋ฒˆ ~ 1077๋ฒˆ ํ’€์ด

์˜ˆ์ง„-D 2021. 8. 14. 21:27

๐Ÿ“Œ ๋น ๋ฅธ ๋ฐ”๋กœ๊ฐ€๊ธฐ 

 

 

 

๐Ÿ“ ์ง€๋‚œ ๊ธ€

2021.07.13 - [๐Ÿ–ค ์ฝ”๋“œ์—… CodeUp/C์–ธ์–ด ๊ธฐ์ดˆ 100์ œ] - [์ฝ”๋“œ์—… CodeUp] C์–ธ์–ด ๊ธฐ์ดˆ 100์ œ 1063๋ฒˆ ~ 1070๋ฒˆ ํ’€์ด

 

[์ฝ”๋“œ์—… CodeUp] C์–ธ์–ด ๊ธฐ์ดˆ 100์ œ 1063๋ฒˆ ~ 1070๋ฒˆ ํ’€์ด

๐Ÿ“ ์ง€๋‚œ ๊ธ€ 2021.06.27 - [๐Ÿ–ค ์ฝ”๋“œ์—… CodeUp/C์–ธ์–ด ๊ธฐ์ดˆ 100์ œ] - [์ฝ”๋“œ์—… CodeUp] C์–ธ์–ด ๊ธฐ์ดˆ 100์ œ 1053๋ฒˆ ~ 1062๋ฒˆ ํ’€์ด [์ฝ”๋“œ์—… CodeUp] C์–ธ์–ด ๊ธฐ์ดˆ 100์ œ 1053๋ฒˆ ~ 1062๋ฒˆ ํ’€์ด ๐Ÿ“ ์ง€๋‚œ ๊ธ€ 2021.06.19 - [๐Ÿ–ค..

hyj3463.tistory.com

 

 

์•„๋ž˜ ์ฝ”๋“œ๋“ค์€ ์ œ ๊นƒํ—ˆ๋ธŒ์—์„œ๋„ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. :-)

https://github.com/YejinHwang-D/Algorithm_CodeUp 

 

GitHub - YejinHwang-D/Algorithm_CodeUp: Code-up basic 100 answer using C

Code-up basic 100 answer using C. Contribute to YejinHwang-D/Algorithm_CodeUp development by creating an account on GitHub.

github.com

 


 

codeup.kr/problemsetsol.php

 

๋ฌธ์ œ์ง‘

 

codeup.kr

1071๋ฒˆ๋ถ€ํ„ฐ 1077๋ฒˆ๊นŒ์ง€๋Š” ๋ฐ˜๋ณต์‹คํ–‰๊ตฌ์กฐ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. 

 

 

๐Ÿ’ก ์•„์ฃผ ์†Œ์†Œํ•œ Tip

๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ ํ‘œํ˜„์€ ์ฝ”๋”ฉ ์Šคํƒ€์ผ ์„ค์ •์„ ์ด์šฉํ•˜๋ฉด ์ž๋™์œผ๋กœ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค. 

 

 


 

 

# 1071๋ฒˆ

์ถœ๋ ฅ ์„ค๋ช…: 0 ์ž…๋ ฅ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

#include <stdio.h>

int main() {
    int num;
    checkpoint:
        scanf("%d", &num);
        if(num!=0) {
            printf("%d\n", num);
            goto checkpoint;
        }
    return 0;
}

goto๋ฌธ์„ ์ด์šฉํ•ด ํŠน์ • ๋ถ€๋ถ„์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. scanf์—์„œ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์ด 0์ด ์•„๋‹ ๋•Œ checkpoint๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค.

 

 

# 1072๋ฒˆ

์ถœ๋ ฅ ์„ค๋ช…: n๊ฐœ์˜ ์ˆ˜ ๋ฐ˜๋ณต ์ถœ๋ ฅ

#include <stdio.h>

int main() {
    int num1, num2; 
    scanf("%d", &num1);
    print_num:
        scanf("%d", &num2);
        printf("%d\n", num2);
        num1 -= 1;
        if(num1 > 0) goto print_num;
        
    return 0;
}

์—ญ์‹œ goto๋ฌธ์„ ์ด์šฉํ•˜์—ฌ n๊ฐœ์˜ ์ˆ˜ ์ž…๋ ฅ, ์ถœ๋ ฅ์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ 0์ด ์•„๋‹Œ 0๋ณด๋‹ค ํฐ ์ •์ˆ˜์ผ ๋•Œ print_num ์œ„์น˜๋กœ ๋Œ์•„๊ฐ€๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

 

 

# 1073๋ฒˆ

์ถœ๋ ฅ ์„ค๋ช…: 0 ์ž…๋ ฅ๋  ๋•Œ๊นŒ์ง€๋งŒ ๋ฐ˜๋ณต

#include <stdio.h>

int main() {
	int num;
	while (1) {
	    scanf("%d", &num);
	    if (num == 0) 
	        break;
	    printf("%d\n", num);
	}
	return 0;
}

while (1)์˜ ์˜๋ฏธ๋Š” ๋ฌดํ•œ ๋ฐ˜๋ณต์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์„ ๊ผญ ๋‹ฌ์•„์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ์—์„œ๋Š” ์ž…๋ ฅ๋ฐ›์€ num์ด 0์ด ๋˜๋ฉด ์•„๋ž˜์˜ printf ์ถœ๋ ฅ๊นŒ์ง€ ๊ฐ€์ง€ ์•Š๊ณ  while๋ฌธ์„ ํƒˆ์ถœํ•ฉ๋‹ˆ๋‹ค.

 

 

# 1074๋ฒˆ

์ถœ๋ ฅ ์„ค๋ช…: ์ •์ˆ˜ ์ž…๋ ฅ๋ฐ›๊ณ  ์นด์šดํŠธ๋‹ค์šด ๋งŒ๋“ค๊ธฐ (0 ์ถœ๋ ฅ X)

#include <stdio.h>

int main() {
	int num;
	scanf("%d", &num);
	while (num != 0) {
	    printf("%d\n", num--);
	}
	return 0;
}

์ž…๋ ฅ๋ฐ›์€ ์ •์ˆ˜๋ฅผ 1์”ฉ ์ค„์—ฌ๊ฐ€๋ฉฐ ์นด์šดํŠธ๋‹ค์šด์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. while๋ฌธ์˜ ์กฐ๊ฑด์€ 0์ด ์•„๋‹ ๋•Œ ๋ฐ˜๋ณต. ์ฆ‰, 0์ด ๋˜๋ฉด ๋ฐ˜๋ณต์„ ๋๋‚ด๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. (0 ์ถœ๋ ฅ X) ๊ทธ๋ฆฌ๊ณ  while๋ฌธ ์•ˆ์—์„  ํ˜„์žฌ num์„ ์ถœ๋ ฅํ•œ ๋’ค 1์„ ๊ฐ์†Œ์‹œํ‚ค๋„๋ก ํ›„์œ„ ์—ฐ์‚ฐ์ž(N--)๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

# 1075๋ฒˆ

์ถœ๋ ฅ ์„ค๋ช…: ์ •์ˆ˜ ์ž…๋ ฅ๋ฐ›๊ณ  ์นด์šดํŠธ๋‹ค์šด ๋งŒ๋“ค๊ธฐ (0 ์ถœ๋ ฅ O)

#include <stdio.h>

int main() {
	int num;
	scanf("%d", &num);
	while (num > 0) {
	    printf("%d\n", --num);
	}
	return 0;
}

1074๋ฒˆ๊ณผ ๋‹ฌ๋ฆฌ ์ด๋ฒˆ์—” ์ž…๋ ฅ๋œ ์ˆ˜์˜ -1 ํ•œ ๊ฐ’๋ถ€ํ„ฐ 0๊นŒ์ง€ ์ถœ๋ ฅํ•˜๊ณ  ๋ฐ˜๋ณต์„ ๋๋ƒ…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ while๋ฌธ์˜ ์กฐ๊ฑด์€ 0๋ณด๋‹ค ํด ๊ฒฝ์šฐ๋งŒ ๋ฐ˜๋ณต์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๊ณ , printf์—์„œ๋Š” ์ „์œ„ ์—ฐ์‚ฐ์ž(--N)๋ฅผ ์‚ฌ์šฉํ•ด ๋จผ์ € ๊ฐ’์„ ๊ฐ์†Œ์‹œํ‚จ ๋’ค ์ถœ๋ ฅํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

 

# 1076๋ฒˆ

์ถœ๋ ฅ ์„ค๋ช…: ์˜์–ด ์†Œ๋ฌธ์ž ์ž…๋ ฅ๋ฐ›๊ณ  ๊ทธ ๋ฌธ์ž๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ

#include <stdio.h>

int main() {
    char al, temp='a';
    scanf("%c", &al);
    do {
        printf("%c ", temp);
        temp += 1;
    } while (temp <= al);
    
    return 0;
}

๋‹ค๋ฅธ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด๋„ ๋˜์ง€๋งŒ ๋ฌธ์ œ์— do-while๋ฌธ์„ ์ด์šฉํ•˜๋ผ๊ณ  ๋‚˜์™€์žˆ๋„ค์š”. ์ž…๋ ฅ๋ฐ›์€ ์•ŒํŒŒ๋ฒณ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ temp์—๋Š” ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ์ธ a๋ฅผ ์ €์žฅํ•ด ๋‘๊ณ , ๊ฐ’์„ 1์”ฉ ๋Š˜๋ ค๊ฐ€๋ฉด b, c, d...๋กœ ๋ณ€ํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  al๋‚˜ temp์˜ ๊ฐ’์€ int๊ฐ€ ์•„๋‹ˆ "char" ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค! ์ž…๋ ฅ๋ฐ›์„ ๋•Œ๋„ %d๊ฐ€ ์•„๋‹Œ %c์ž„์„ ์ฃผ์˜ํ•ด์ฃผ์„ธ์š”.

 

 

# 1077๋ฒˆ

์ถœ๋ ฅ ์„ค๋ช…: ์ •์ˆ˜ ์ž…๋ ฅ๋ฐ›๊ณ  ๊ทธ ์ˆ˜๊นŒ์ง€ ์ถœ๋ ฅ (0 ํฌํ•จ)

#include <stdio.h>

int main() {
	int num;
	scanf("%d", &num);
	for (int i=0; i<=num; i++) {
	    printf("%d\n", i);
	}
	return 0;
}

for๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!