https://www.acmicpc.net/problem/1065


이 문제는 일단 한수라는 개념을 알아야한다. 한수란 각 자리수가 등차수열이 되어야 한다. 예를 들면, 234, 357 등으로 각 자리수가 등차수열만 만족시키면 된다. 여기서 문제는 100이하는 어떻게 해야하는가인데, 결론은 100이하의 수는 무조건 한수이다. 예를 들면, 25, 36, 29 등 무조건 한수의 조건을 만족시킨다. 이 개념만 알고 시작하면 어려운 문제는 아니다. 일단 직관적으로 풀었다. 더 좋은 방법도 충분히 많이 있을 것이라 생각이 드는 문제이다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
 
using namespace std;
 
int han(int input, int count) {
    if (input < 100)
        return count += input;
    else if (input == 1000)
        han(input - 1, count);
    else {
        if ((input / 100 - (input % 100/ 10== (input % 100/ 10 - (input % 10/ 1)
            count++;
        han(input - 1, count);
    }
}
 
int main() {
    int N;
    int count = 0;
    cin >> N;
 
    cout << han(N, count);
 
    return 0;
}
cs


https://www.acmicpc.net/problem/1186


간단히 정렬하는 문제로 좀 특이한 점이 있다면 단어 길이 sorting를 신경써야한다는 점이다.


처음 든 생각은 length 와 string을 구조체에 넣고 계산할까 하다가


string 타입과 sort함수를 사용하면 코드가 간결해질 것 같은 느낌적인 느낌이 들어서 이들을 이용하였다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
 
using namespace std;
 
bool compare(const string& a, const string& b){
    if(a.length() == b.length())
        return (a < b);
    else
        return a.length() < b.length();
}
 
int main() {
    int N;
    string input[20001];
    
    cin >> N;
    
    for(int i = 0; i < N; i++)
        cin >> input[i];
    
    sort(input, input + N, compare);
 
    for(int i = 0; i < N; i++){
        if(input[i] == input[i-1&& i != 0)
            continue;
        cout << input[i] << endl;
    }
    
    return 0;
}
cs


https://www.acmicpc.net/problem/10820


문제는 간단하고 어렵지는 않지만 fgets()와 EOF에 대해 알 수 있는 문제이다.

배열의 크기를 101로 잡게 되면 틀린다!

getchar()를 이용해서 하나씩 받아와도 무관하다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>
 
using namespace std;
 
char input[102];
int main() {
    while(fgets(input, 102, stdin)) {
        int small = 0, capital = 0, num = 0, space = 0;
        for (int i = 0; input[i]; i++) {
            if ('a' <= input[i] && input[i] <= 'z'//small letter
                small++;
            else if ('A' <= input[i] && input[i] <= 'Z'//capital letter
                capital++;
            else if ('0' <= input[i] && input[i] <= '9'//number
                num++;
            else if (input[i] == ' '//space
                space++;
        }
        printf("%d %d %d %d\n", small, capital, num, space);
    }
    return 0;
}
cs



<Amazing Code From https://www.acmicpc.net/source/4430326>


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
int a,A,n,s;
char c;
int main() {
    while(~(c=getchar())) {
        if(c>='a' && c<='z') a++;
        if(c>='A' && c<='Z') A++;
        if(c>='0' && c<='9') n++;
        if(c==' ') s++;
        if(c=='\n'printf("%d %d %d %d\n", a,A,n,s), a=A=n=s=0;
    }
    return 0;
}
cs


+ Recent posts