C, C++/๐Ÿฅ‰ BOJ

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

metamong 2024. 11. 13.

โ˜… 1330 ๋‘ ์ˆ˜ ๋น„๊ตํ•˜๊ธฐ โ˜…

//1330

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[]) {
	int A, B;
    cin >> A >> B;
    if (A > B){
        cout << '>' << '\n';
    }
    else if (A < B){
        cout << '<' << '\n';
    }
    else{
        cout << "==" << '\n';
    }
    
	return 0;
}

โ˜… 9498 ์‹œํ—˜ ์„ฑ์  โ˜…

#include <iostream>
#include <string>

using namespace std;
 
int main(int argc, char const *argv[])
{
    
    int score;
    cin >> score;
 
    if(score >= 90){
        cout << "A";
    } 
    else if(score >= 80){
        cout << "B";
    } 
    else if(score >= 70){
        cout << "C";
    } 
    else if(score >= 60){
        cout << "D";
    } 
    else {
        cout << "F";
    }
    
    return 0;
}

โ˜… 2753 ์œค๋…„ โ˜…

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

int main(){
    int year;
    cin >> year;

    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
        cout << 1 << '\n';
    }
    else{
        cout << 0 << '\n';
    }
    return 0;
}

 

๐Ÿค ๊ทธ๋ฆฌ๊ณ ๋Š” &&, ๋˜๋Š”์€ || ๊ธฐํ˜ธ๋ฅผ ํ™œ์šฉํ•ด ์กฐ๊ฑด ๋‚ด์— ๋ณตํ•ฉ ์กฐ๊ฑด์„ ๋ฌธ์ œ์— ๋งž๊ฒŒ ์ ์ ˆํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค.


โ˜… 14681 ์‚ฌ๋ถ„๋ฉด ๊ณ ๋ฅด๊ธฐ โ˜…

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

int main(){
    int x,y;
    cin >> x >> y;

    if (x > 0 && y > 0){
        cout << 1 <<'\n';
    }
    else if (x > 0 && y < 0){
        cout << 4 << '\n';
    }
    else if (x < 0 && y >0){
        cout << 2 << '\n';
    }
    else{
        cout << 3 << '\n';
    }
    return 0;
}

โ˜… 2884 ์•Œ๋žŒ ์‹œ๊ณ„ โ˜…

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

int main(){
    int H, M;
    cin >> H >> M;
    int seconds = 60 * H + M;
    int after_seconds = seconds - 45;
    if (after_seconds < 0){
        cout << (60 * 24 + after_seconds)/ 60 << ' ' << 60 * 24 + after_seconds - 60*((60 * 24 + after_seconds)/ 60) << '\n';
    }
    else{
        cout << after_seconds/60 << ' ' << after_seconds%60 << '\n';
    }

    return 0;
}

 

๐Ÿค ์‹œ๊ณ„๋Š” ๋ชจ๋‘ ๋ถ„์œผ๋กœ ๋ฐ”๊พผ ๋‹ค์Œ 45๋ถ„์„ ๋บ€ ๋’ค, ์Œ์ˆ˜๋ผ๋ฉด ์ „์ฒด 24 * 60๋ถ„์—์„œ ์Œ์ˆ˜๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.


โ˜… 2525 ์˜ค๋ธ ์‹œ๊ณ„ โ˜…

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

int main(){
    int hour, minute;
    cin >> hour >> minute;
    int cur_minute = 60*hour + minute;
    int play;
    cin >> play;

    int ans = cur_minute + play;
    if (ans >= 24*60){
        cout << (ans-24*60)/60 << ' ' << (ans-24*60) - ((ans-24*60)/60)*60 << '\n';
    }
    else{
        cout << ans/60 << ' ' << ans%60 <<'\n'; 
    }

    return 0;
}

 

๐Ÿค ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋‘ ๋ถ„์œผ๋กœ ๋ฐ”๊ฟ”์„œ ํ•ด๊ฒฐ


โ˜… 2480 ์ฃผ์‚ฌ์œ„ ์„ธ ๊ฐœ โ˜…

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

int main(){
    int a, b, c;
    cin >> a >> b >> c;
    if (a == b && b == c){
        cout << 10000 + a * 1000 << '\n';
    }
    else if(a == b || b == c || c == a){
        if(a==b){
            cout << 1000 + a * 100 << '\n';
        }
        else if(b==c){
            cout << 1000 + b * 100 << '\n';
        }
        else{
            cout << 1000 + c * 100 << '\n';
        }
    }
    else{
        if (a > b && a > c) cout << a * 100 << '\n';
        else if (b > a && b > c) cout << b* 100 << '\n';
        else cout << c * 100 << '\n';
    }

    return 0;
}

โ˜… 2739 ๊ตฌ๊ตฌ๋‹จ โ˜…

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

int main(){
    int N;
    cin >> N;

    for(int i = 1 ; i < 10; i++) {
        cout << N << " * " << i << " = " << N * i << '\n';
    }

    return 0;
}

โ˜… 10950 A + B - 3 โ˜…

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

int main(){
    int T;
    cin >> T;

    for(int i = 0 ; i < T; i++) {
        int A, B;
        cin >> A >> B;
        cout << A + B << '\n';
    }

    return 0;
}

โ˜… 8393 ํ•ฉ โ˜…

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

int main(){
    int n;
    cin >> n;
    int ans = 0;
    for(int x = 1; x <= n; x++){
        ans += x;
    }
    cout << ans << '\n';
}

โ˜… 25304 ์˜์ˆ˜์ฆ โ˜…

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

int main(){
    int X;
    cin >> X;
    int N;
    cin >> N;
    int total = 0;
    for(int i = 1; i <= N; i++){
        int a, b;
        cin >> a >> b;
        total += (a*b);
    }
    if (total == X){
        cout << "Yes" << '\n';
    }
    else{
        cout << "No" << '\n';
    }
}

โ˜… 25314 ์ฝ”๋”ฉ์€ ์ฒด์œก๊ณผ๋ชฉ์ž…๋‹ˆ๋‹ค โ˜…

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

int main(){
    int N;
    cin >> N;

    for(int i = 0; i < N / 4; i++) {
        cout << "long ";
    }
    cout << "int";
}

 

๐Ÿค ํŒŒ์ด์ฌ๊ณผ ๋‹ฌ๋ฆฌ c++์€ ์ฃผ์–ด์ง„ N์„ 4๋กœ ๋‚˜๋ˆˆ ๋ชซ๋งŒํผ long์„ ๋ฐ˜๋ณตํ•˜๊ณ , int๋ฅผ ๋ถ™์—ฌ์„œ ์ถœ๋ ฅํ•  ๋•Œ ์ง์ ‘ for๋ฌธ์„ ๋Œ๋ ค์•ผ ํ•œ๋‹ค.


โ˜… 2438 ๋ณ„ ์ฐ๊ธฐ - 1 โ˜…

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

int main(){
    int N;
    cin >> N;

    for(int i = 1; i < N+1; i++) {
        for(int j = 1; j<=i; j++){
            cout << '*';
        }
        cout << endl;
    }
}

 

๐Ÿค ์ง์ ‘ 2์ค‘ for๋ฌธ์„ ์จ์„œ ๊ฐ ์ค„ ๋‚ด์— *์„ for๋ฌธ์œผ๋กœ ๋Œ๋ ค ๋ฐ˜๋ณต ์ถœ๋ ฅ


โ˜… 2439 ๋ณ„ ์ฐ๊ธฐ - 2 โ˜…

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

int main(){
    int N;
    cin >> N;

    for(int i = 1; i < N+1; i++) {
        for(int j = 1; j < N+1; j++){
            if (j<=(N-i)){
                cout << ' ';
            }
            else{
                cout << '*';
            }
            
        }
        cout << endl;
    }
}

 

๐Ÿค ์ค„ ๋ณ„ ๋นˆ์นธ์˜ ๊ฐœ์ˆ˜์™€ *์˜ ๊ฐœ์ˆ˜ ๊ทœ์น™์„ฑ์„ ์ฐพ์•„์„œ ์ถœ๋ ฅ


โ˜… 10952 A + B - 5 โ˜…

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

int main(){
    while (true){
        int A, B;
        cin >> A >> B;
        if (A == 0 && B == 0){
            break;
        }
        else{
            cout << A + B << '\n';
        }
    }
}

โ˜… 10807 ๊ฐœ์ˆ˜ ์„ธ๊ธฐ โ˜…

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

int main(){
    int N, v, cnt = 0;
    cin >> N;

    int* a = new int[N];
    for(int i = 0; i < N; i++){
        cin >> a[i];
    }
    cin >> v;
    for(int i = 0; i < N; i++){
        if(a[i] == v){
            cnt += 1;
        }
    }

    cout << cnt << '\n';
}

 

๐Ÿค intํ˜• ์ •์ˆ˜๊ฐ€ N๊ฐœ ๋“ค์–ด๊ฐ„ ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๊ธฐ ์œ„ํ•ด int* a = new int[N];์ด๋ผ ์ฝ”๋“œ ์ž‘์„ฑ. ์ฝ”๋“œ ๊ธฐ์–ต. ์ดํ›„ index 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ indexing a[i]๋กœ ์ง„ํ–‰


โ˜… 10871 X๋ณด๋‹ค ์ž‘์€ ์ˆ˜ โ˜…

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

int main(){
    int A, X;
    cin >> A >> X;

    int* a = new int[A];
    for (int i = 0; i < A; i++){
        cin >> a[i];
    }

    for (int i = 0; i < A; i++){
        if(a[i] < X){
            cout << a[i] << " ";
        }
    }
}

 

๐Ÿค์ง์ ‘ ๋ฐฐ์—ด ๋‚ด์˜ ์›์†Œ๋กœ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ cin >> a[i]


โ˜… 10818 ์ตœ์†Œ, ์ตœ๋Œ€ โ˜…

//10818
#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;
    int min = 1000001;
    int max = -1000001;
    
    for(int i = 0; i < N; i++){
        int x;
        cin >> x;
        if (x <= min){
            min = x;
        }
        if (x>=max){
            max = x;
        }
    }

    cout << min << ' ' << max << '\n';

}

โ˜… 2562 ์ตœ๋Œ“๊ฐ’ โ˜…

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

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

    int cnt = 1, max = 0, max_cnt;
    for(int x = 0; x < 9; x++){
        int N;
        cin >> N;
        if (max < N){
            max = N;
            max_cnt = cnt;
        }
        cnt += 1;
    }

    cout << max << '\n' << max_cnt << '\n';

}

โ˜… 10810 ๊ณต ๋„ฃ๊ธฐ โ˜…

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

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] = 0;
    }

    for(int x = 0; x < M; x++){
        int i, j, k;
        cin >> i >> j >> k;
        for(int p = i -1; p < j; p++){
            basket[p] = k;
        }
    }

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

}

โ˜… 10813 ๊ณต ๋ฐ”๊พธ๊ธฐ โ˜…

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

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;
        int tmp = basket[i-1];
        basket[i-1] = basket[j-1];
        basket[j-1] = tmp;
    }

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

}

 

๐Ÿค ๋‘ ๋ฐ”๊ตฌ๋‹ˆ์˜ ๊ณต์„ ์„œ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด์„œ๋Š” swap ์ฝ”๋“œ ์ž‘์„ฑ ์‹œ tmp๋ผ๋Š” ๋ณ„๋„์˜ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  A์™€ B ์‚ฌ์ด์˜ ๋ณ€์ˆ˜ ๊ฐ’์„ ์„œ๋กœ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค.


โ˜… 5597 ๊ณผ์ œ ์•ˆ ๋‚ด์‹  ๋ถ„ ... ? โ˜…

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

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

    int* attendance_list = new int[30];
    int ans1 = 0, ans2 = 0;

    for(int x = 0; x < 28; x++){
        int i;
        cin >> i;
        attendance_list[i-1] = '!';
    }

    for(int x = 0; x < 30; x++){
        if (attendance_list[x] != '!'){
            if (ans1 == 0){
                ans1 = x + 1;
            }
            else{
                ans2 = x + 1;
            }
        }
    }

    if (ans1 > ans2){
        cout << ans2 << '\n' << ans1;
    }
    else{
        cout << ans1 << '\n' << ans2;
    }

}

โ˜… 3052 ๋‚˜๋จธ์ง€ โ˜…

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

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

    int* remainders_cnt = new int[42];
    int ans = 0;

    for(int x = 0; x < 42; x++){
        remainders_cnt[x] = 0;
    }

    for(int x = 0; x < 10; x++){
        int y;
        cin >> y;
        remainders_cnt[y%42] += 1;
    }

    for(int x = 0; x < 42; x++){
        if (remainders_cnt[x] != 0){
            ans += 1;
        }
    }

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

 

๐Ÿค 42๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋Š” 42๊ฐœ ์žˆ์œผ๋ฏ€๋กœ ์ด 42๊ฐœ์˜ int ์นธ์ด ๋“ค์–ด๊ฐ„ array ์ƒ์„ฑ ํ›„, ๊ฐ ์ˆ˜๋ฅผ 42๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ ์นธ์— +=1 count. count ๊ฒฐ๊ณผ 0์ด ์•„๋‹Œ ์ˆ˜๋งŒ ์ถœ๋ ฅ


โ˜… 27866 ๋ฌธ์ž์™€ ๋ฌธ์ž์—ด โ˜…

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

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

    string S;
    cin >> S;

    int i;
    cin >> i;

    cout << S[i-1] << '\n';

    return 0;

}

 

๐Ÿค #include <string>์œผ๋กœ string S ๋ฌธ์ž์—ด ์ •์˜. indexing ๊ฐ€๋Šฅ


โ˜… 2743 ๋‹จ์–ด ๊ธธ์ด ์žฌ๊ธฐ โ˜…

#include<iostream>
#include<algorithm>
using namespace std;
 
int main() {
	string S;
	cin >> S; 
	cout << S.length();
	return 0;
}

 

๐Ÿค #include <algorithm>์„ ์‚ฌ์šฉํ•ด ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ length() ํ•จ์ˆ˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ


โ˜… 11654 ์•„์Šคํ‚ค ์ฝ”๋“œ โ˜…

#include <iostream>
using namespace std;

int main(void) {
	char c;
	cin >> c;
	cout << (int)c;
	return 0;
}

 

๐Ÿค (int) ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ž…๋ ฅํ•œ char ํ˜•ํƒœ์˜ ํ•œ ๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์•„์Šคํ‚ค ์ฝ”๋“œ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.


โ˜… 11718 ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ โ˜…

#include <iostream>
using namespace std;

int main() {

	string s;

	while(true) {
		getline(cin, s);
		if(s == ""){
            break;
        }
		cout << s << '\n';
	}
	return 0;
}

 

๐Ÿค getline()ํ•จ์ˆ˜ ํ™œ์šฉ. ์ž…๋ ฅ ๋ฐ›์€ ๋‚ด์šฉ์ด ์—†๋‹ค๋ฉด getline()์˜ ๊ฒฐ๊ณผ๋กœ ""์ธ 1๊ฐœ์˜ ๋ฌธ์ž๋„ ์—†๋Š” ๊ฒฐ๊ณผ๊ฐ€ s๋ผ๋Š” ๋ณ€์ˆ˜์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.


โ˜… 27433 ํŒฉํ† ๋ฆฌ์–ผ 2 โ˜…

//27433
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
using namespace std;

long factorial(long N){
    if(N==1 || N==0){
        return 1;
    }
    return N * factorial(N-1);
}

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

    int N;
    cin >> N;

    cout << factorial(N) << '\n';


	return 0;
}

 

๐Ÿค ํฐ ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ int type ๋Œ€์‹  long type๋งŒ ์ฃผ์˜. ์žฌ๊ท€๋กœ ํ•ด๊ฒฐ


 

 

 

 

 

 

 

 

 

 

 

๋Œ“๊ธ€