[백준] 승수 3474, 문자열시간 2852, 브루트포스 1436, 문자열스택 9012, 4949

seunghyun·2023년 3월 13일
0

Test

목록 보기
5/19

3474 교수가 된 현우

문제 링크
수학 문제이다.
2, 5의 개수를 세는 문제가 아닐까? 2*5=10이므로!
2의 승수인 4,8을 찾고 5의 승수인 5,10,15,20,25를 찾으면 되지않을까 라는 아이디어!

#include<bits/stdc++.h>
using namespace std;
int n,a;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n; 
    for(int i=0; i<n; i++)
    {
        cin >> a;
        int ret2 = 0, ret5 = 0;
        for(int j=2; j<=a; j*=2)
        {
            ret2 += a/j;
        }
        for(int j=5; j<=a; j*=5)
        {
            ret5 += a/j;
        }
        cout << min(ret2, ret5) << '\n';
    }
    return 0;
}

2852 NBA 농구

문제 링크
구현,문자열 문제
분,초로 입력이 들어와도 하나의 단위로! 그것도 작은 단위인 초를 기준으로 통일한다.

substr() https://modoocode.com/235
c_str() https://modoocode.com/297

#include<bits/stdc++.h>
using namespace std;
#define prev sh
int n,o,A,B,asum,bsum;
string s, prev;

/** 포맷화해서 출력 */
string print(int a)
{
    string d = "00" + to_string(a/60); //분
    string e = "00" + to_string(a%60); //초
    return d.substr(d.size()-2,2) + ":" + e.substr(e.size()-2,2);
}

/** 하나의 작은 (초) 단위를 기반으로 통일 */
int changeToInt(string a)
{
    return atoi(a.substr(0,2).c_str()) * 60 
        + atoi(a.substr(3,2).c_str());
}

void go(int &sum, string s)
{
    sum+=(changeToInt(s) - changeToInt(prev));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> n; // 골이 들어간 횟수
    for(int i=0; i<n; i++)
    {
        cin >> o >> s; // 팀 번호, 골 들어간 시간
        if (A>B) go(asum, s);
        else if (B>A) go(bsum, s);
        o == 1 ? A++ : B++; // 1팀이 이기면 A, 2팀이 이기면 B
        prev = s; // 새로 값이 들어왔을 때 prev에 들어있는 값은 직전값이다
    }
    
    // 끝점 꼭 포함해주기
    if(A>B) go(asum, "48:00");
    else if(B>A) go(bsum, "48:00");
    
    cout << print(asum) << '\n';
    cout << print(bsum) << '\n';
}

1436 영화감독 숌

문제 링크

#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
    cin >> n; 
    int i = 666;
    for(;; i++) // 무한 루프
    {
        if(to_string(i).find("666") != string::npos) n--;
        if(n==0) break;
    }
    cout << i << '\n'; // N번째 영화의 제목
}

9012 괄호

문제 링크
스택을 활용하여 풀 수 있는 문제이다. 괄호, 짝짓기, 폭발은 스택으로 풀 수 있을 듯 하다.

#include<bits/stdc++.h>
using namespace std;
int n; // 입력 데이터의 수
string s;
bool check(string s)
{
    stack<char> stk; // 매번 초기화해주자
    for(char c:s)
    {
        if(c=='(') stk.puch(c);
        else // 오른쪽괄호 라면
        {
            if(!stk.empty()) stk.pop(); // 짝인 왼쪽괄호를 pop
            else return false; // 왼쪽도 없으니 끝낸다
        }
    }
    return stk.empty();
}

int main()
{
    cin >> n;
    for(int i=0; i<n; i++)
    {
        cin >> s;
        if(check(s)) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}

4949 균형잡힌 세상

문제 링크

#include <bits/stdc++.h> 
using namespace std; 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);  
    while(true){
        string s; 
        getline(cin, s);  // 띄어쓰기를 포함해서 입력받는다
        if(s == ".") break;
        stack<int> stk; // 매번 테스트케이스마다 새로이 초기화
        bool check = true;
        for(int i = 0; i < s.length(); i++){ 
            if(s[i] == ')'){ 
                if(stk.size() == 0 || stk.top() == '['){// 균형이 잡힌 스택인지 확인
                    check = false; 
                    break; 
                }else{// ()
                    stk.pop();
                }
            }
            if(s[i] == ']'){
                if(stk.size() == 0 || stk.top() == '('){// 균형이 잡힌 스택인지 확인
                    check = false; 
                    break; 
                }else{// []
                    stk.pop();
                }
            } 
            if(s[i] == '(') stk.push(s[i]);
            if(s[i] == '[') stk.push(s[i]); 
        }  
        if(check && stk.size() == 0)  cout << "yes\n";
        else cout << "no\n";
    } 
    return 0;
} 

0개의 댓글