본문 바로가기

C++

백준 C++ 5217,6749,8393,9654,9713,10093,10170,10171,10172,10699,10718,10817,10926,11104,11319,11367,11549,

#5217

#include <iostream>
using namespace std;

int main(){

    //test 개수
    int t;
    int n;

    cin >> t;

    for(int i=0;i<t;i++){

        cin >> n;
        cout << "Pairs for " << n <<": ";

        int cnt = 0;

        for (int j=1; j<((n-1)/2)+1;j++){

            if(cnt++){
                cout << ", ";
            }

            cout << j << " " << n-j;
        }

        cout << "\n";

        /*
         * 2: 1,1 : 1 -> 0
         * 3: 1,2 2,1 : 2 -> 1
         * 4: 1,3 2,2 3,1 : 3 -> 1
         * 5: 1,4 2,3 3,2 4,1 : 4 -> 2
         * 6: 1,5 2,4 3,3 4,2 5,1 : 5 -> 2
         * n : (n-1)/2
         */


    }

    return 0;
}

#6749

#include <iostream>
using namespace std;

int main(){

    int fist,second,third;

    cin >> third;
    cin >> second;
    int sub = second - third;

    cout << second+sub;
    
    return 0;
}

#8393

#include <iostream>
using namespace std;

int main(){

    int n;
    cin >> n;
    int sum = 0;

    for (int i=1;i<=n;i++){
        sum += i;
    }

    cout << sum;

    return 0;
}

#9654

#include <iostream>
using namespace std;

int main(){

    cout << "SHIP NAME      CLASS          DEPLOYMENT IN SERVICE" << '\n';
    cout << "N2 Bomber      Heavy Fighter  Limited    21        " << '\n';
    cout << "J-Type 327     Light Combat   Unlimited  1         " << '\n';
    cout << "NX Cruiser     Medium Fighter Limited    18        " << '\n';
    cout << "N1 Starfighter Medium Fighter Unlimited  25        " << '\n';
    cout << "Royal Cruiser  Light Combat   Limited    4         " << '\n';

    return 0;
}

#9713

#include <iostream>
using namespace std;

int main(){

    int t,n;
    cin >> t;

    for (int i=0; i<t; i++){

        cin >> n;
        int sum =0;

        for (int i=1; i<=n;i++){

            if (i%2==1){
                sum +=i;
            }

        }

        cout << sum <<'\n';


    }

    return 0;

}

int sum=0;의 위치가 매우 중요하다 for문 안에 넣어주지 않으면 sum의 값이 초기화되지 않아서 다른 값이 나올 것이다.

 

#10093

#include <iostream>
using namespace std;

int swap(int a,int b){

    int temp;
    temp = a;
    a = b;
    b = temp;
}

int main(){

    long long a,b;
    cin >> a >> b;

    if (a>b){
        swap(a,b);
    }

    if(a==b){
        cout << 0 << endl;
    }

    else{

        long long cnt = b-a-1;
        cout << cnt << '\n';

        for (long long i =a+1;i<b;i++){

            cout << i << " ";
        }

    }

    return 0;

}

#10170

#include <iostream>
using namespace std;

int main(){

    cout << "NFC West       W   L  T" << '\n';
    cout << "-----------------------" << '\n';
    cout << "Seattle        13  3  0"<< '\n';
    cout << "San Francisco  12  4  0"<< '\n';
    cout << "Arizona        10  6  0"<< '\n';
    cout << "St. Louis      7   9  0"<< '\n';
    cout << '\n';
    cout << "NFC North      W   L  T"<< '\n';
    cout << "-----------------------"<< '\n';
    cout << "Green Bay      8   7  1"<< '\n';
    cout << "Chicago        8   8  0"<< '\n';
    cout << "Detroit        7   9  0"<< '\n';
    cout << "Minnesota      5  10  1"<< '\n';
    
    return 0;

}

#10171

#include <iostream>
using namespace std;

int main(){

    cout << "\\    /\\" << '\n';
    cout << " )  ( ')" << '\n';
    cout << "(  /  )"<< '\n';
    cout << " \\(__)|"<< '\n';
 
    return 0;

}

#10172

#include <iostream>
using namespace std;

int main(){

    cout << "|\\_/|" << '\n';
    cout << "|q p|   /}" << '\n';
    cout << "( 0 )\"\"\"\\"<< '\n';
    cout << "|\"^\"`    |"<< '\n';
    cout << "||_/=\\\\__|"<< '\n';

    return 0;

}

#10699

#include <iostream>
using namespace std;

int main(){

    cout << "2023-04-04" << '\n';

    return 0;

}

#10718

#include <iostream>
using namespace std;

int main(){

    cout << "강한친구 대한육군" << '\n';
    cout << "강한친구 대한육군" << '\n';

    return 0;

}

#10817

#include <iostream>
using namespace std;
const int Max = 3;

int bubblesort (int list[], int n){

    int i,j,temp;
    //n-1인 이유: 버블정렬은 2개씩 잡아서 정렬을 하기 때문에 n-1로 전체범위를 잡는다.
    //첫번째 for문이 전체 범위를 설정해준다.
    for (i=0;i<n-1;i++){
        //두번째 for문은 실제로 정렬을 실행하는 부분이다.
        for (j=0;j<i;j++){
            if(list[j]<list[j+1]){
                temp = list[j];
                list[j]=list[j+1];
                list[j+1]=temp;
            }
        }
    }

    return 0;
}

int main(){

    int a,b,c;
    cin >> a >> b >> c;
    int list [Max] = {a,b,c};
    bubblesort(list,Max);

    cout << list[1];

    return 0;

}

위 문제에서 버블정렬 함수에서 retrun 0; 을 써주지 않아서 런타임 에러가 발생했다.

#10926

#include <iostream>
using namespace std;
const int Max = 3;

int main(){

    string n;
    cin >> n;
    cout << n << "??!";
    return 0;

}

#11104

https://wslim8256.tistory.com/m/26

 

[백준] [C++] 11104번 Fridge of your Dreams

https://www.acmicpc.net/problem/11104 11104번: Fridge of Your Dreams Eirik drinks a lot of Bingo Cola to help him program faster, and over the years he has burned many unnecessary calories walking all the way to the kitchen to get some. To avoid this he h

wslim8256.tistory.com

위 블로그를 참조했음 

 

#include <iostream>
#include <string>

using namespace std;

int main(){

    int t,sum;
    string num;
    cin >> t;

    for(int i=0;i<t;i++){

        cin >> num;
        //stoi: string to int
        //stoi(변경할 문자열, 사용 안 할 포인터를 걸러줌 하지만 여기서는 사용 안 해서 nullptr 사용, 진수)
        sum = stoi(num, nullptr,2);
        cout << sum << '\n';

    }

    return 0;

}

#11319

#include <iostream>
#include <string>

using namespace std;

int main(){

    string s;
    int t;
    cin >> t;
    getchar();

    for(int i=0; i<t; i++){

        getline(cin,s);
        int count_i =0 , count_j = 0;

        for (int j=0;j<s.size();j++){

            switch (s[j]) {

                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    count_i ++;
                    break;
                case ' ':
                    break;
                default:
                    count_j ++;
                    break;

            }


        }

        cout << count_j << " " << count_i << endl;

    }


}

#11367

#include <iostream>
#include <string>

using namespace std;

int main(){

    int t,n;
    cin >> t;
    string s,total;

    for (int i=0;i<t;i++){

        cin >> s >> n;

        if(97 <= n && n <=100){
            total = "A+";
        }
        else if(90<=n && n<=96){
            total = "A";
        }
        else if(87<=n && n<=89){
            total = "B+";
        }
        else if(80<=n && n<=86){
            total = "B";
        }
        else if(77<=n && n<=79){
            total = "C+";
        }
        else if(70<=n && n<=76){
            total = "C";
        }
        else if(67<=n && n<=69){
            total = "D+";
        }
        else if(60<=n && n<=66){
            total = "D";
        }
        else {
            total = "F";
        }

        cout << s << " " << total <<endl;

    }

    return 0;

}

#11549

#include <iostream>

using namespace std;

int main(){

    int t,a,b,c,d,e;
    int cnt = 0;
    cin >> t;
    cin >> a >> b >> c >> d>>e;
    int list[5] = {a,b,c,d,e};

    for (int i=0;i<5;i++){

        if (list[i] == t){
            cnt ++;
        }

    }

    cout << cnt;
    return 0;

}