C, C++/๐Ÿฅˆ BOJ

(C++) โ˜…Implementation&Simulation Intermediate I - 2 Solvedโ˜…

metamong 2024. 11. 14.

โ˜… 2941 ํฌ๋กœ์•„ํ‹ฐ์•„ ์•ŒํŒŒ๋ฒณ โ˜…

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    string S;
    int x;
    cin >> S;
    vector<string> arr =  {"c=","c-","dz=","d-","lj","nj","s=","z="};

    for(int i =0 ; i < arr.size(); i++){
        while(true){
            x = S.find(arr[i]);
            
            if(x == string::npos){
                break;
            }
            else{
                S.replace(x, arr[i].length(), "!");
            }
        }
    }
    cout << S.length();

    return 0;

}

 

๐Ÿค vector<string>์œผ๋กœ ๋ฌธ์ž์—ด์ด ๋“ค์–ด๊ฐ„ ๋ฐฐ์—ด์„ ์ƒ์„ฑ(#include <vector>)

 

๐Ÿค find() ํ•จ์ˆ˜ ์•ˆ์— ํฌ๋กœ์•„ํ‹ฐ์•„ ๋ณ€๊ฒฝ๋œ ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž์—ด์„ ๋„ฃ์œผ๋ฉด, ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ๋“ค์–ด๊ฐ€๋Š” ์œ„์น˜๋ฅผ index๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ๋ชป์ฐพ์•˜๋‹ค๋ฉด(x == string.npos) ๋ฐ”๋กœ ์ข…๋ฃŒ. 2๊ฐœ ์ด์ƒ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ while(true)๋ฌธ ์ง„ํ–‰.

 

๐Ÿค ์ฐพ์•„์„œ ๋Œ€์ฒดํ•ด์•ผ ํ•œ๋‹ค๋ฉด replace() ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉ. replace์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž index, ๋‘๋ฒˆ์งธ ์ธ์ž ํ•ด๋‹น index์—์„œ ๋ฐ”๊ฟ€ ๊ฐ„๊ฒฉ length(), ์„ธ๋ฒˆ์งธ ์ธ์ž ๋ฐ”๊ฟ€ ๋‚ด์šฉ.


โ˜… 2563 ์ƒ‰์ข…์ด โ˜…

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

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

	int paper[100][100] = {0}; 
	int num, ans = 0;
    int x, y;
    cin >> num;

    for(int i = 0; i < num; i++){
        cin >> x >> y;
        for(int j=y;j<(y+10);j++){
            for(int k=x;k<(x+10);k++){
                if(!paper[j][k]){
                    paper[j][k] = 1;
                    ans+=1;
                }
            }
        }
    }

    cout << ans << '\n';
	return 0;
}

 

๐Ÿค ์•„์ด๋””์–ด ๋ฌธ์ œ. ์ง์ ‘ ๊ฒน์น˜๋Š” ๊ฒƒ ์ƒ๊ด€์—†์ด, ์˜์—ญ์— ํ•ด๋‹น๋˜๋Š” ์นธ๋งŒ 0์„ 1๋กœ ๋ฐ”๊ฟˆ. ๊ทธ๋ฆฌ๊ณ  ์ •์‚ฌ๊ฐํ˜•์ด๋ฏ€๋กœ ๋ฐฉํ–ฅ ๊ณ ๋ ค ์—†์ด x์—์„œ x+9๊นŒ์ง€, y์—์„œ y+9๊นŒ์ง€ ๋‹จ์ˆœํžˆ for๋ฌธ ๋Œ๋ฆฌ๋ฉด ๋จ. ์ด ๋•Œ 2์ฐจ์› ๋ฐฐ์—ด ๋งŒ๋“ค ๋•Œ ์ฒ˜์Œ์— {0}์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•„์ˆ˜


 

 

 

 

 

 

 

 

 

 

 

 

 

 

๋Œ“๊ธ€