[문제]

N개의 정수가 주어진다. 이때, 최솟값과 최댓값을 구하는 프로그램을 작성하시오.


[입력]

첫째 줄에 정수의 개수 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 N개의 정수를 공백으로 구분해서 주어진다. 모든 정수는 -1,000,000보다 크거나 같고, 1,000,000보다 작거나 같은 정수이다.


[출력]

첫째 줄에 주어진 정수 N개의 최솟값과 최댓값을 공백으로 구분해 출력한다.

백준에서 제시하는 예시


간단한 문제로써, 사실 오늘 내로 코드 하나는 풀어야겠고, 시간은 없고 해서 이걸로 간단하게 하루를 마무리용으로 했다

(나중에는 더 실력을 쌓아서 좀 더 어려운 문제도 금방 금방 해결할 수 있기를...)

 

그런데 이 와중에 처음에는 "컴파일 에러"가 나왔다. 다음 코드처럼 if ~ if ~ 대신에 if ~ else if ~를 했기 때문이다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*본코드는 컴파일에러가 뜬다*/
#include <stdio.h>
 
int main(void)
{
    int N, num;
    int max = -1000001, min = 1000001;
    
    scanf("%d"&N);
    
    for (int i = 1; i <= n; i++){
        scanf("%d"&num);
        if (num > max){
            max = num;
        }
        else if (num < min){
            min = num;
        }
    }
    printf("%d %d", min, max);
 
    return 0;
}
cs

 

 

결국, else if가 아닌 if문으로 바꿨을 때야 비로소 제대로 맞았다고 나왔다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
 
int main(void)
{
    int N, num;
    int max = -1000001, min = 1000001;
    
    scanf("%d"&N);
    
    for (int i = 1; i <= n; i++){
        scanf("%d"&num);
        if (num > max){
            max = num;
        }
        if (num < min){
            min = num;
        }
    }
    printf("%d %d", min, max);
 
    return 0;
}
cs

 

출처: https://www.acmicpc.net/problem/10818

 

10818번: 최소, 최대

첫째 줄에 정수의 개수 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 N개의 정수를 공백으로 구분해서 주어진다. 모든 정수는 -1,000,000보다 크거나 같고, 1,000,000보다 작거나 같은 정수이다.

www.acmicpc.net

 

'코딩 문제풀이 및 연습 > [C언어]백준' 카테고리의 다른 글

[백준 6749번]Next in line  (0) 2021.03.17
[백준 2675번]문자열 반복  (0) 2021.03.16
[백준 9012번] Parenthesis  (0) 2020.10.08
[백준 2795번] Transactions  (0) 2020.09.05
[백준 2753번] 윤년  (0) 2020.09.05

[문제]

You know a family with three children. Their ages form an arithmetic sequence: the difference in ages between the middle child and youngest child is the same as the difference in ages between the oldest child and the middle child. For example, their ages could be 5, 10 and 15, since both adjacent pairs have a difference of 5 years.

Given the ages of the youngest and middle children, what is the age of the oldest child?

 


[입력]

The input consists of two integers, each on a separate line. The first line is the age Y of the youngest child (0 ≤ Y ≤ 50). The second line is the age M of the middle child (Y ≤ M ≤ 50).


[출력]

The output will be the age of the oldest child.

 

백준에서 제시하는 예제 입출력


영어라서 그렇지, 문제 자체는 매우 기초적인 문제이다!

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
 
int main()
{
    int youngest, middle;
    
    scanf("%d %d"&youngest, &middle);
    
    printf("%d\n", middle + (middle-youngest));
 
    return 0;
}
 
cs

 

출처: https://www.acmicpc.net/problem/6749

 

6749번: Next in line

You know a family with three children. Their ages form an arithmetic sequence: the difference in ages between the middle child and youngest child is the same as the difference in ages between the oldest child and the middle child. For example, their ages c

www.acmicpc.net

 

'코딩 문제풀이 및 연습 > [C언어]백준' 카테고리의 다른 글

[백준 10818번] 최소, 최대  (0) 2021.03.18
[백준 2675번]문자열 반복  (0) 2021.03.16
[백준 9012번] Parenthesis  (0) 2020.10.08
[백준 2795번] Transactions  (0) 2020.09.05
[백준 2753번] 윤년  (0) 2020.09.05

[문제]

문자열 S를 입력받은 후에, 각 문자를 R번 반복해 새 문자열 P를 만든 후 출력하는 프로그램을 작성하시오. 즉, 첫 번째 문자를 R번 반복하고, 두 번째 문자를 R번 반복하는 식으로 P를 만들면 된다. S에는 QR Code "alphanumeric" 문자만 들어있다.

QR Code "alphanumeric" 문자는 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\$%*+-./: 이다.

 


[입력]

첫째 줄에 테스트 케이스의 개수 T(1 ≤ T ≤ 1,000)가 주어진다. 각 테스트 케이스는 반복 횟수 R(1 ≤ R ≤ 8), 문자열 S가 공백으로 구분되어 주어진다. S의 길이는 적어도 1이며, 20글자를 넘지 않는다. 


[출력]

각 테스트 케이스에 대해 P를 출력한다.

백준에서 제시하는 예제 입출력

 


 

첫번째 시도는 다음과 같다. T를 for문을 통해서 돌렸는데, 계속해서 컴파일 에러가 났다.

(참고로, strlen(S) 대신에 R를 사용했는데, 이건 아예 틀렸다고 답이 나온다.. 분명 예제 입출력데로 나오는데도 말이다.

이유는 "문제"의 입력형식데로 맞춰야 하기 때문이라고 추정된다)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <string.h>
 
int main()
{
    int T, R;
    char S[21];
    scanf("%d"&T);
    for(int i=0; i<T; i++){
        scanf("%d %s"&R, &S);
        for(int j=0; j<strlen(S); j++){
                for (int k=0; k<R; k++){
                    printf("%c", S[j]);
            }
        }
        printf("\n");
    }
    return 0;
}
cs

결국 여러 번 시도한 끝에, for문을 바꿔야 하는 것으로 결론이 났다.

(다음과 같이 해야 제대로 맞았다고 나온다. while문을 통해서 T를 계속 하나씩 뺀다)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>
 
int main() {
    int T, R;
    char S[21];
    
    scanf("%d"&T);
    //T만큼의 테스트케이스
    while (T--) {
        scanf("%d %s"&R, S);
        
        //결과를 출력하기 위한 반복문
        for (int i = 0; i < strlen(S); i++) {
            for (int j = 0; j < R; j++)
                printf("%c", S[i]);
        }
 
        printf("\n");
    }
    return 0;
}
 
cs

 

 

출처1: www.acmicpc.net/problem/2675

 

2675번: 문자열 반복

문자열 S를 입력받은 후에, 각 문자를 R번 반복해 새 문자열 P를 만든 후 출력하는 프로그램을 작성하시오. 즉, 첫 번째 문자를 R번 반복하고, 두 번째 문자를 R번 반복하는 식으로 P를 만들면 된다

www.acmicpc.net

'코딩 문제풀이 및 연습 > [C언어]백준' 카테고리의 다른 글

[백준 10818번] 최소, 최대  (0) 2021.03.18
[백준 6749번]Next in line  (0) 2021.03.17
[백준 9012번] Parenthesis  (0) 2020.10.08
[백준 2795번] Transactions  (0) 2020.09.05
[백준 2753번] 윤년  (0) 2020.09.05

[문제]

Parenthesis String (PS) consists of two parenthesis symbols ‘(’ and ‘)’ only. In parenthesis strings, some strings are called a valid PS (shortly, VPS). Let us give the formal definition of VPS. A single “( )” is a member of VPS, called the base VPS. Let x and y be a member of VPS. Then “(x)”, a VPS which encloses a VPS x with a single pair of parenthesis, is also a member of VPS. And xy, the concatenation of two VPS x and y, is a member of VPS. For example, “(())()” and ((()))” are all VPS, but “(()(”, “(())()))” and “(()” are not VPS. You are given a set of PS. You should decide if the input string is VPS or not. 


[입력]

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Then PS’s are given in the following T lines one by one. The length of each PS is between 2 and 50, inclusively.


[출력]

Your program is to write to standard output. Print the result in each line. If the input string is a VPS, then print “YES”. Otherwise print “NO”. 

 

백준에서 제시하는 입출력

 

 


몇 번이나 틀린 끝에 결국엔 성공한 코드.

중요한 부분은 char parenthesis[]의 방 크기를 50이 아닌 51(null문자까지 생각을 해야한다)로 하는 것이 좋다는 것.

 

또한, for문으로 for(int j=0; j<strlen(bracket); j++) 로 할 수도 있겠지만, 

while문으로  while(parenthesis[j]) 것이 더 좋을 수 있다는 것을 기억하자.

 

 

출처1: www.acmicpc.net/problem/9012

 

9012번: 괄호

괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고

www.acmicpc.net

출처2: ICPC > Regionals > Asia Pacific > Korea > Nationwide Internet Competition > Daejeon Nationalwide Internet Competition 2012 G번

'코딩 문제풀이 및 연습 > [C언어]백준' 카테고리의 다른 글

[백준 6749번]Next in line  (0) 2021.03.17
[백준 2675번]문자열 반복  (0) 2021.03.16
[백준 2795번] Transactions  (0) 2020.09.05
[백준 2753번] 윤년  (0) 2020.09.05
[백준 2752번] 세 수 정렬  (0) 2020.09.05

[문제]

Whenever somebody goes to an ATM to withdraw or deposit money, a calculation has to be done to keep the person's bank balance correct. Your task in this problem is to do such calculations. There is a bank rule that says that a customer may not have an overdraft of more than $200, so any withdrawal that would take the balance below –200 must be stopped. (A minus sign is used to indicate an overdraft, or negative balance).


[입력]

Input consists of a number of lines, each representing a transaction. Each transaction consists of an integer representing the starting balance (between –200 and +10,000), the letter W or the letter D (Withdrawal or Deposit), followed by a second integer representing the amount to be withdrawn or deposited (between 5 and 400). Input will be terminated by a line containing 0 W 0.


[출력]

Output consists of one line for each line of input showing the new balance for each valid transaction If a withdrawal would take the balance below -200, the output must be the words ‘Not allowed’.

백준에서 제시하는 예졔 입출력

 


영어로 되어있어서 그렇지 문제 자체는 쉽다.

ATM기계에서 돈을 빼고 넣는 결과를 출력하는 문제이다.

시작값, W / D, 바뀔값, 결과값을 변수로 잡으면 될 것이다.

 

문제를 간단히 하면 다음과 같다. 

1. 0 W 0 이라는 값이 들어가면 프로그램이 종료되도록 한다.

2. W가 입력되면 시작값 - 바뀔값 = 결과값

3. D가 입력되면 시작값 + 바뀔값 = 결과값

4. 결과값이 -200보다 작을 경우, "Not allowed"라 출력되도록 한다.

 

 

출처: https://www.acmicpc.net/problem/2975

 

[문제]

연도가 주어졌을 때, 윤년이면 1, 아니면 0을 출력하는 프로그램을 작성하시오.

윤년은 연도가 4의 배수이면서, 100의 배수가 아닐 때 또는 400의 배수일 때이다.

예를 들어, 2012년은 4의 배수이면서 100의 배수가 아니라서 윤년이다. 1900년은 100의 배수이고 400의 배수는 아니기 때문에 윤년이 아니다. 하지만, 2000년은 400의 배수이기 때문에 윤년이다.


[입력]

첫째 줄에 연도가 주어진다. 연도는 1보다 크거나 같고, 4000보다 작거나 같은 자연수이다.


[출력]

첫째 줄에 윤년이면 1, 아니면 0을 출력한다.

백준에서 제시하는 예제 입출력

 


최대한 짧게 코딩을 해보기 위해서 이번에는 노력했다.

원래라면 if문 안에서 &&와 ||를 if중춥문을 사용해서 일일이 했을 것이나,

이렇게 if문 한 개를 사용해서 만드는 것 또한 가능하다는 것을 알아두자.

 

출처: https://www.acmicpc.net/problem/2753

[문제]

동규는 세수를 하다가 정렬이 하고싶어졌다.

숫자 세 개를 생각한 뒤에, 이를 오름차순으로 정렬하고 싶어 졌다.

숫자 세 개가 주어졌을 때, 가장 작은 수, 그 다음 수, 가장 큰 수를 출력하는 프로그램을 작성하시오.


[입력]

숫자 세 개가 주어진다. 이 숫자는 1보다 크거나 같고, 1,000,000보다 작거나 같다. 이 숫자는 모두 다르다.


[출력]

제일 작은 수, 그 다음 수, 제일 큰 수를 차례대로 출력한다.

백준에서 제공하는 예제 입출력

 


약간의 노가다와 시간이 오래 걸린다는 점이 있지만, 논리는 일단 맞다.

a, b, c가 각각 큰 경우에 따라서 나와야 하는 숫자의 정렬을 각각 출력하며,

a=b=c가 될 경우는 따로 else문으로 처리하였다.

출처: https://www.acmicpc.net/problem/2752

2444번_별찍기-7(피라미드데칼코마니)

2444번_별찍기-7(피라미드데칼코마니)


 

2445번_별찍기_8(나비모양)

2445번_별찍기_8(나비모양)


 

2446번_별찍기_9(모래시계)

2446번_별찍기_9(모래시계)

 

 

 

출처: https://www.acmicpc.net/workbook/view/20 백준 문제집: 별 찍기 中

 

2438번_별찍기_1(피라미드오른쪽)

2438번_별찍기_1(피라미드오른쪽)

 


 

2439번_별찍기_2(피라미드왼쪽)

2439번_별찍기_2(피라미드왼쪽)

 


 

2440번_별찍기_3(5-4-3-2-1)

2440번_별찍기_3(5-4-3-2-1)

 


 

2441번_별찍기_4(역순_5-4-3-2-1)

2441번_별찍기_4(역순_5-4-3-2-1)


 

2442번_별찍기_5(피라미드)

2442번_별찍기_5(피라미드)


 

2443번_별찍기_6(역피라미드)

2443번_별찍기_6(역피라미드)

 

 

 

출처: https://www.acmicpc.net/workbook/view/20 백준 문제집: 별 찍기 中

[문제]

세 정수 A, B, C가 주어진다. 이때, 두 번째로 큰 정수를 출력하는 프로그램을 작성하시오. 


[입력]

첫째 줄에 세 정수 A, B, C가 공백으로 구분되어 주어진다. (1 ≤ A, B, C ≤ 100)


[출력]

두 번째로 큰 정수를 출력한다.

 

백준에서 제시하는 예제 입출력


내가 푼 방식은 다음과 같이 각각의 숫자가 최댓값일 때로 나눠서 총 3가지의 경우의 수로 if~else문을 만들었다.

 

조금 노가다의 향이 나기는 한다...

 

출처: https://www.acmicpc.net/problem/10817

+ Recent posts