C, C++/๐Ÿฅ‰ BOJ

(C++) โ˜…Implementation Upper-Beginner I - 8 Solvedโ˜…

metamong 2024. 11. 13.

โ˜… 10811 ๋ฐ”๊ตฌ๋‹ˆ ๋’ค์ง‘๊ธฐ โ˜…

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

void swap(int *a, int *b){
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N, M;
    cin >> N >> M;
    int* basket = new int[N];

    for(int x = 0; x < N; x++){
        basket[x] = x + 1;
    }

    for(int x = 0; x < M; x++){
        int i, j;
        cin >> i >> j;
        for(int a = 0; a <= (j-i)/2; a++){
            swap(basket[i-1+a], basket[j-1-a]);
        }
    }

    for(int i = 0; i<N; i++){
        cout << basket[i] <<' ';
    }
    return 0;

}

 

๐Ÿค ๋ฐ”๊ตฌ๋‹ˆ i๋ถ€ํ„ฐ ๋ฐ”๊ตฌ๋‹ˆ j๊นŒ์ง€ ์—ญ์ˆœ์œผ๋กœ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์€, iterator x๊ฐ€ ๋ฐ”๊ตฌ๋‹ˆ i๋ถ€ํ„ฐ (i+j)/2๊นŒ์ง€ ๋Œ๋ฉฐ ์–‘ ์˜†(x์™€ ์„ swapํ•˜๋Š” ๋ฐฉ์‹์„ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.


โ˜… 1546 ํ‰๊ท  โ˜…

//1546
#include <iostream>
#include <string>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N;
    cin >> N;
    float* grades = new float[N];
    int max = -1;
    float ans_sum = 0.0;

    for(int x = 0; x < N; x++){
        int grade;
        cin >> grade;
        grades[x] = grade;
    }

    //get max
    for(int x = 0; x < N; x++){
        if(grades[x] > max){
            max = grades[x];
        }
    }

    for(int x = 0; x < N; x++){
        grades[x] = ((grades[x])/float(max))*100;
        ans_sum += grades[x];
    }

    cout << ans_sum / N << '\n';

    return 0;

}

 

๐Ÿค float ํ˜•ํƒœ๋กœ ๋ฐฐ์—ด๊ณผ ans_sum์„ ๋งŒ๋“ค์–ด ๋†“์€ ๋’ค, ๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ๋Œ€๋กœ (grades[x]/float(max)) * 100์„ ๋ˆ„์ ์œผ๋กœ ๋”ํ•ด ๋‚˜๊ฐ€์„œ ans_sum / N์„ ๊ตฌํ•œ๋‹ค.


โ˜… 10809 ์•ŒํŒŒ๋ฒณ ์ฐพ๊ธฐ โ˜…

//10809
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    string S, alphabets = "abcdefghijklmnopqrstuvwxyz";
    cin >> S;

    for(int x = 0; x < alphabets.length(); x++){
        cout << (int)S.find(alphabets[x]) << ' ';
    }
    cout << '\n';

    return 0;

}

 

๐Ÿค ๋ฌธ์ž์—ด์˜ find() ํ•จ์ˆ˜(#include <string>)๋ฅผ ์‚ฌ์šฉํ•ด ์ฐพ๋Š” ๋ฌธ์ž์—ด์ด ์—†๋‹ค๋ฉด string::npos๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. ์ด๋Š” ํ˜„ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๊ฐ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„ ๋ฐ–์˜ ํฐ ์ˆ˜๋ฅผ ๋œปํ•œ๋‹ค. ํ•ด๋‹น ํฐ ์ˆ˜๋Š” int()๋ฅผ ํ†ตํ•ด ๋ณ€ํ™˜ํ•œ๋‹ค๋ฉด ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ด, ์ด ๋•Œ -1์ด ์ถœ๋ ฅ. ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์—†์„ ๋•Œ -1๋กœ ์ถœ๋ ฅํ•˜๋ผ๊ณ  ํ—€์œผ๋ฏ€๋กœ ์•ž์— (int)๋ฅผ ๋ถ™์ด๋ฉด ๋œ๋‹ค.


โ˜… 1152 ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ โ˜…

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    string S;
    int res = 1;

    getline(cin, S);

    if (S.length() == 1 && S[0] == ' '){
        cout << 0;
        return 0;
    }

    for(int x = 1; x < S.length()-1; x++){
        if(S[x] == ' '){
            res += 1;
        }
    }

    cout << res << '\n';

    return 0;

}

 

๐Ÿค ๊ณต๋ฐฑ์„ ํฌํ•จํ•œ ํ•œ ์ค„์„ ์ž…๋ ฅ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ๋Š” getline() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ. getline() ํ•จ์ˆ˜ ์•ˆ์— cin๊ณผ ์ž…๋ ฅ ๋ฐ›์„ ๋ฌธ์ž์—ด ๋ณ€์ˆ˜ ์ด๋ ‡๊ฒŒ 2๊ฐœ๋ฅผ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.

 

๐Ÿค ์ถ”๊ฐ€๋กœ ๊ธธ์ด๊ฐ€ 1์ด๋ฉด์„œ ๋‹จ์ˆœํžˆ ๊ณต๋ฐฑ ํ•˜๋‚˜๋ผ๋ฉด ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ 0์ด๋ฏ€๋กœ 0 ์ถœ๋ ฅ / ๋งจ ์•ž์ด ์•„๋‹Œ ๋‘๋ฒˆ์งธ ๋ฌธ์ž๋ถ€ํ„ฐ ๋งจ ๋’ค๊ฐ€ ์•„๋‹Œ ๋งจ ๋’ค ๋ฐ”๋กœ ์•ž์˜ ๋ฌธ์ž ๊นŒ์ง€์˜ ๋ฒ”์œ„์—์„œ ' ' ๊ณต๋ฐฑ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋ฏ€๋กœ for๋ฌธ ๋Œ๋ฆฌ๋Š” ๋ณ€์ˆ˜ ๋ฒ”์œ„์— ์ฃผ์˜. 


โ˜… 2908 ์ƒ์ˆ˜ โ˜…

//2908
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    string A, B;
    cin >> A >> B;

    for(int i = 2; i >= 0; i--){
        if(A[i] > B[i]){
            for(int x = 2; x >= 0; x--){
                cout << A[x];
            }
            return 0;
        }
        else if(B[i] > A[i]){
            for(int x = 2; x >= 0; x--){
                cout << B[x];
            }
            return 0;
        }
    }

    return 0;

}

 

๐Ÿค 0์ด ์—†๊ณ , ์ฃผ์–ด์ง„ ์„ธ ์ž๋ฆฌ ์ˆ˜ ๊ฐ๊ฐ ๋’ค์ง‘์—ˆ์„ ๋•Œ ๋ฌด์—‡์ด ๋” ํฐ ์ง€ ๋น„๊ตํ•˜๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ, ๊ฑฐ๊พธ๋กœ index 2๋ถ€ํ„ฐ ์ง์ ‘ String์˜ indexing์œผ๋กœ ๋น„๊ตํ•˜์—ฌ ๋ฌด์—‡์ด ๋” ํฌ๊ณ  ์ž‘์€ ์ง€ ๋น„๊ต๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉด ๋ฐ”๋กœ ๋ฆฌํ„ดํ•˜๊ณ  ์ข…๋ฃŒ.


โ˜… 5622 ๋‹ค์ด์–ผ โ˜…

//5622
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    string word;
    int time[] = {3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,8,9,9,9,10,10,10,10}, res = 0;
    cin >> word;

    for(int i = 0; i < word.length(); i++){
        res += time[word[i]-'A'];
    }

    cout << res;

    return 0;

}

 

๐Ÿค ์•„์Šคํ‚ค ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•ด 'B'-'A'๋กœ ์ฃผ์–ด์ง„ ๊ฐ„๊ฒฉ์ด 1์ด๋ผ๋ฉด ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด ๋†“์€ time์„ ํ™œ์šฉํ•ด ๊ฐ„๊ฒฉ๋งŒํผ์˜ indexing ๊ฒฐ๊ณผ ๋ˆ„์ ์œผ๋กœ ํ•ฉํ•ด์„œ ์ถœ๋ ฅ

 

๐Ÿค time[]์œผ๋กœ ๋ฏธ๋ฆฌ []์•ˆ์— ๋ช‡ ๊ฐœ์˜ ์›์†Œ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ์ง€ ์ •ํ•  ํ•„์š” x


โ˜… 2738 ํ–‰๋ ฌ ๋ง์…ˆ โ˜…

//2738
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;


int main() {

	int n, m;
    cin >> n >> m;

    int **arr1 = new int *[n];
    int **arr2 = new int *[n];

    for(int i = 0; i < n; i++){
        arr1[i] = new int[m];
        arr2[i] = new int[m];
    }

	for (int i = 0; i < n; i++) 
		for (int j = 0; j < m; j++) 
			cin >> arr1[i][j];

	for (int i = 0; i < n; i++) 
		for (int j = 0; j < m; j++) 
			cin >> arr2[i][j];
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) 
			cout << arr1[i][j] + arr2[i][j] << ' ';
		cout << '\n';
	}

	return 0;
}

 

๐Ÿค nxm ํ˜•ํƒœ์˜ 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค ๋•Œ, ** ์ด์ค‘ ํฌ์ธํ„ฐ๋กœ arr1๊ณผ arr2 ํฐ ํ‹€์˜ 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค(int **arr1 = new int*[n]). ๊ทธ๋ฆฌ๊ณ  for๋ฌธ์„ n๋ฒˆ ๋Œ๋ ค ๊ฐ ํ–‰ ๋‚ด์— arr1[i] = new int[m] ์ฝ”๋“œ๋ฅผ ๋„ฃ์Œ์œผ๋กœ์จ nxm ํ˜•ํƒœ์˜ 2์ฐจ์› ๋ฐฐ์—ด ํ‹€์„ ๋งŒ๋“ ๋‹ค.


โ˜… 10798 ์„ธ๋กœ์ฝ๊ธฐ โ˜…

//10798
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;


int main() {

	string arr[5]; 
	
	for (int i = 0; i < 5; i++) {
		cin >> arr[i];
    }

    for(int i = 0; i < 15; i++){
        for(int j = 0; j < 5; j++){
            if(i <= arr[j].size()-1){
                cout << arr[j][i];
            }
        }
    }

	return 0;
}

 

๐Ÿค 1์ฐจ์› ๋ฐฐ์—ด string์œผ๋กœ arr[5] ๋งŒ๋“  ๋‹ค์Œ, ๊ฐ ์ค„ ์ž…๋ ฅ์œผ๋กœ 1์ฐจ์› ๋ฐฐ์—ด ๋‚ด์— ๊ฐ๊ฐ์˜ ๊ธด ๋ฌธ์ž์—ด์ด ์›์†Œ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋งŒ๋“ฆ.

 

๐Ÿค i <= arr[j].size() - 1 ์กฐ๊ฑด์ด ๋งž์„ ๋•Œ๋งŒ arr[j][i]๋กœ, index๋ฅผ i์™€ j๋ฅผ ๋ฐ”๊ฟ”์„œ ์ถœ๋ ฅํ•˜๋ฉด ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋Œ€๋กœ ์„ธ๋กœ๋กœ ์ฝ์€ ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅ


 

 

 

 

 

 

 

 

 

 

๋Œ“๊ธ€