반응형

1157번 문제 : 단어 공부

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


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    int alphabet[26= { 0 };
    string str;
    cin >> str;
 
    for (int i = 0; i < str.length(); i++)
    {
        int ASCIInum = (int)str[i];
 
        //ASCII code 'A':65 ~ 'Z': 90      'a': 97 ~ 'z': 122
        if(ASCIInum>=65 && ASCIInum <=90)
            alphabet[(int)str[i] - 65++;
        else if(ASCIInum >= 97 && ASCIInum <= 122)
            alphabet[(int)str[i] - 97++;
    }
 
    int max = 0;
    int index = 0;
    bool isTheOnlyMax = true;
    for (int i = 0; i < 26; i++)
    {
        if (alphabet[i] > max)
        {
            max = alphabet[i];
            index = i;
        }
    }
 
    for (int i = 0; i < 26; i++)
    {
        if (index == i)
            continue;
 
        if (alphabet[i] == max)
        {
            isTheOnlyMax = false;
            break;
        }
    }
 
    if (isTheOnlyMax)
        cout << (char)(index + 65);
    else
        cout << "?";
 
    return 0;
}
cs


메모: 

-ASCII Table상에서 'Z' 다음은'a' 가 아님


1316번 문제 : 그룹 단어 체커

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


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    int num = 0;
    int counter = 0;
    cin >> num;
    while (num > 0
    {
        string input;
        bool flag = false//used to break nested loop
        cin >> input;
 
        if (input.length() > 2)
        {
            for (int i = 1; i < input.length(); i++)
            {
                if (input[i - 1!= input[i])
                {
                    for (int k = 0; k < i; k++)
                    {
                        if (input[i] == input[k])
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                        break;
                }
 
                if (i == input.length() - 1)
                    counter++;
            }
        }
        else
        {
            counter++;
        }
 
        num--;
    }
 
    cout << counter;
 
    return 0;
}
cs


메모: 

-이중루프(nested loop)를 빠져나오기 위해 bool 형식의 flag를 사용하고 있음



1152번 문제 : 단어의 개수

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>
 
using namespace std;
 
int main() {
    string inp;
    getline(cin, inp);
    istringstream iss(inp);
    int counter = 0;
 
    string str;
    while (iss >> str)
    {
        counter++;
    }
    cout << counter << endl;
 
    return 0;
}
cs


메모: 

-C++string Split 할 수 있는 여러가지 방법들

http://www.martinbroadhurst.com/how-to-split-a-string-in-c.html

https://www.fluentcpp.com/2017/04/21/how-to-split-a-string-in-c/





반응형

'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글

규칙 찾기  (0) 2019.02.05
문자열 사용하기 3  (0) 2019.02.05
문자열 사용하기  (0) 2018.11.27
1차원 배열 사용하기  (0) 2018.11.27
함수 사용하기  (0) 2018.11.18

+ Recent posts