๐Ÿซ  ๋ฐฑ์ค€ 1152(https://www.acmicpc.net/problem/1152)

 

1152๋ฒˆ: ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜

์ฒซ ์ค„์— ์˜์–ด ๋Œ€์†Œ๋ฌธ์ž์™€ ๊ณต๋ฐฑ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1,000,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ๋‹จ์–ด๋Š” ๊ณต๋ฐฑ ํ•œ ๊ฐœ๋กœ ๊ตฌ๋ถ„๋˜๋ฉฐ, ๊ณต๋ฐฑ์ด ์—ฐ์†ํ•ด์„œ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค. ๋˜ํ•œ ๋ฌธ์ž์—ด

www.acmicpc.net

๐Ÿง Algorithm _ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ ์„ธ๋Š” ๋ฒ•
1. ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋Œ€์‹  ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ ๊ณต๋ฐฑ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์—ˆ๋‹ค.
(โˆต ๋‹จ์–ด๋ฅผ ๊ตฌ๋ถ„์ง“๋Š” ๊ธฐ์ค€์ด ๋„์–ด์“ฐ๊ธฐ์ด๊ธฐ ๋•Œ๋ฌธ)

2. getline ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด cin์œผ๋กœ ๋ฐ›์€ ์ž…๋ ฅ๊ฐ’์„ str์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•ด ์ฃผ์—ˆ๋‹ค.
์ด๋•Œ, ์ €์žฅ๋˜๋Š” ๊ฐ’๋“ค์€ ๋ชจ๋‘ char ํ˜•์ด๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด hello๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, str[1] = 'e'๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

3. ๊ทธ ํ›„ ๊ณต๋ฐฑ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž ' ' ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์›Œ์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

4. ๋‹ค๋งŒ, ์ฃผ์˜ํ•  ์ ์€ ๊ฐ€์žฅ ์•ž์— ๊ณต๋ฐฑ์ด ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ์™€ ๊ฐ€์žฅ ๋’ค์— ๊ณต๋ฐฑ์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ์ด๋‹ค.
์ด์™€ ๊ด€๋ จํ•˜์—ฌ ๊ฐ€์žฅ ์•ž์— ๊ณต๋ฐฑ์ด ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋ฅผ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋กœ ์ฒ˜๋ฆฌํ•ด ์ฃผ์—ˆ๊ณ 
๊ฐ€์žฅ ๋’ค์— ๊ณต๋ฐฑ์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ๋Š” for๋ฌธ์—์„œ str.size()-1 ์„ ํ•ด์คŒ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด ์ฃผ์—ˆ๋‹ค.
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    string str;
    getline(cin, str);  // getline(cin, str, '\n');
    // cin.getline(char str, streamsize n, char dlim); delim์„ ๋งŒ๋‚˜๊ธฐ ์ „๊นŒ์ง€์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ฝ์–ด str๋ฐฐ์—ด์— ์ €์žฅ.

    int cnt_space = 0;

    for (int i = 0; i < str.size() - 1; i++) {
        if (str[i] ==' ')
            cnt_space++;
    }
    int cnt = (str[0] == ' ') ? cnt_space : cnt_space + 1;
    cout << cnt;
}


 

 

 

 

 

 

 

๐Ÿซ  ๋ฐฑ์ค€ 1157(https://www.acmicpc.net/problem/1157)

 

1157๋ฒˆ: ๋‹จ์–ด ๊ณต๋ถ€

์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž๋กœ ๋œ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ์ด ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

๐Ÿง Algorithm _ ์™œ ํ‹€๋ฆฌ๋Š”๊ฑฐ์ง€...
1. ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž์˜ ๊ตฌ๋ณ„์„ ์—†์• ๊ธฐ ์œ„ํ•ด ์†Œ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
2. ์•ŒํŒŒ๋ฒณ์„ ์ด 25๊ฐœ์ด๋ฏ€๋กœ ๊ฐ๊ฐ์˜ ์•ŒํŒŒ๋ฒณ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์ฃผ๊ธฐ ์œ„ํ•œ count๋ฐฐ์—ด ์ƒ์„ฑ
3. countํ•œ ๊ฐ’์ด 2๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด(max์™€ ๋™์ผํ•œ count๊ฐ’์ด ์žˆ๋‹ค๋ฉด) ? ์ถœ๋ ฅ
4. ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด max๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” index์— ํ•ด๋‹นํ•˜๋Š” ์•ŒํŒŒ๋ฒณ ์ถœ๋ ฅ

1๋ฒˆ.

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    string str;
    getline(cin, str);

    // 1. ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž์˜ ๊ตฌ๋ณ„์„ ์—†์• ์•ผ ํ•จ
    for (int i = 0; i < str.length(); i++){
        if (str[i] >= 'a' && str[i] <= 'z')
            str[i] = str[i] - 'a' + 'A';
    }
    // 2. ์•ŒํŒŒ๋ฒณ์€ ์ด 25๊ฐœ(Z-A = 25)์ด๋ฏ€๋กœ count๋ฅผ ์œ„ํ•œ ๋ฐฐ์—ด์„ ์ƒ์„ฑ ๋ฐ ์ดˆ๊ธฐํ™”ํ•ด์ค˜์•ผํ•จ
    int count[26];    for (int i = 0; i < 26; i++)    count[i] = 0;

    for (int i = 0; i < str.length(); i++){
        int idx_number = str[i] - 65;
        count[idx_number]++;
    }

    char c;
    int max = -1;

    for (int i = 0; i < 26; i++){
        if (count[i] == 0)
            continue;
        else if (count[i] > max){
            max = count[i];
            c = (char)(i + 65);
        }
        else if (count[i] == max){
            c = '?';
        }
    }
    cout << c;
}

 

 

 2๋ฒˆ.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    string str;
    getline(cin, str);

    // 1. ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž์˜ ๊ตฌ๋ณ„์„ ์—†์• ์•ผ ํ•จ
    for (int i = 0; i < str.length(); i++){
        if (str[i] >= 'a' && str[i] <= 'z')
            str[i] = str[i] - 'a' + 'A';
    }
    // 2. ์•ŒํŒŒ๋ฒณ์€ ์ด 25๊ฐœ(Z-A = 25)์ด๋ฏ€๋กœ count๋ฅผ ์œ„ํ•œ ๋ฐฐ์—ด์„ ์ƒ์„ฑ ๋ฐ ์ดˆ๊ธฐํ™”ํ•ด์ค˜์•ผํ•จ
    vector<int> alpha_count(26);    for (int i = 0; i < 26; i++)    alpha_count[i] = 0;

    for (int i = 0; i < 26; i++){
        int idx_number = str[i] - 65;
        alpha_count[idx_number]++;
    }

    int max = *max_element(alpha_count.begin(), alpha_count.end());
    int cnt = count(alpha_count.begin(), alpha_count.end(), max);
    int index = max_element(alpha_count.begin(), alpha_count.end()) - alpha_count.begin();

    if (cnt >= 2)
        cout << "?";
    else if (cnt < 2)
        cout << (char)(index + 65);
}

 

 

 

 

 

 

๐Ÿซ  ๋ฐฑ์ค€ 11720(https://www.acmicpc.net/problem/11720)

 

11720๋ฒˆ: ์ˆซ์ž์˜ ํ•ฉ

์ฒซ์งธ ์ค„์— ์ˆซ์ž์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ์ˆซ์ž N๊ฐœ๊ฐ€ ๊ณต๋ฐฑ์—†์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

๐Ÿง Algorithm _ ํ•œ์ค„์— ์ž…๋ ฅ์„ ๋ชจ๋‘ ๊ณต๋ฐฑ์—†์ด ๋ฐ›๊ธฐ
1. ๋จผ์ € ๊ณต๋ฐฑ์—†์ด ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ €์žฅํ•˜๋Š” string ๋ณ€์ˆ˜ str๊ณผ
2. string์„ char๋ฐฐ์—ด์— ๋„ฃ๊ธฐ ์œ„ํ•œ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.
char c[str.length()+1] ์ด๋ผ๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์ฃผ๊ณ 
์ด ๋ฐฐ์—ด์— str์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์„ ์ง‘์–ด๋„ฃ์–ด ์ฃผ๊ธฐ ์œ„ํ•ด copyํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์ค€๋‹ค.

3. ๊ทธ ํ›„ char ํ˜•์˜ 5๋ฅผ int ํ˜•์˜ 5๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•˜๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•ด ์ค€๋‹ค.
9 = '9'-'0'  (= 57 - 48)
์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜๋ฉด c[i] - '0'์€ ํ•ด๋‹น ์ˆซ์ž์™€ ๋™์ผํ•œ ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋œ๋‹ค.

4. ์ดํ›„ ์ตœ์ข…์ ์œผ๋กœ sum์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ์ตœ์ข…์ ์ธ ๊ฐ’๋“ค์˜ ํ•ฉ์„ ์ €์žฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int num;    cin >> num;
    string str; cin >> str;
    int sum = 0;

    char c[str.length() + 1];
    str.copy(c, str.length() + 1);

    int arr[str.length() + 1];
    for (int i = 0; i < str.length(); i++) {
        arr[i] = c[i] - '0';
    }

    for (int i = 0; i < str.length(); i++) {
        sum += arr[i];
    }
    cout << sum;
}

 

 

 

 

 

 

+ Recent posts