티스토리 뷰

[1926] :  간단한 369  게임

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

int main(void){
    int N, cnt = 0, len;
    cin >> N;
    for(int i = 1; i < N + 1; i++){
        cnt = 0;
        if(i < 10)
            if (i % 3 == 0) cout << "- ";
            else cout << i << " ";
        else{
            string num = to_string(i);
            if(i < 100) len = 2;
            else len = 3;
            for(int j = 0; j < len; j++)
                if(num[j] == '3' || num[j] == '6' || num[j] == '9')
                    cnt++;
            if (cnt != 0){
                for(int j = 0; j < cnt; j++)
                    cout << '-';
                cout << " ";
            }
            else cout << i << " ";
        }
    }
}

[1989] : 초심자의 회문 검사

#include <iostream>
#include <string>
#include <vector>

using namespace std;
vector<int> answer;

void printAnswer(int);
int main(void){
    int testCase;
    cin >> testCase;
    for(int test = 0; test < testCase; test++){
        string str;
        unsigned long strlen, comp, pos;
        cin >> str;
        strlen = str.size();
        comp = strlen/2;
        for(pos = 0; pos < comp; pos++){
            if(str[pos] != str[strlen - 1 - pos])
                break;
        }
        if (pos == comp) answer.push_back(1);
        else answer.push_back(0);
    }
    printAnswer(testCase);
    return 0;
}

void printAnswer(int testCase){
    for(int i = 0; i < testCase; i++)
        cout << "#" << i + 1 << " " << answer[i] << endl;
}

[2005] : 파스칼의 삼각형

#include <iostream>

using namespace std;
void pasCal(int);

int main(void){
    int testCase;
    cin >> testCase;
    int ans[testCase];
    for(int i = 0; i< testCase; i++)
        cin >> ans[i];
    
    for(int i = 0; i < testCase; i++){
        cout << "#" << i + 1 << endl;
        pasCal(ans[i]);
    }
    return 0;
}

void pasCal(int len){
    int arr[len], i;
    for(i = 0; i < len; i++)
        arr[i] = 0; // initialize into 0
    arr[0] = 1; // first element = 1
    cout << 1 << endl; // cout first element
    for(i = 1; i < len; i++){
        int tmp[i + 1], aPos = 1;
        for(int j = 0; j < i; j++) // copy arr[] to tmp[]
            tmp[j] = arr[j];
        arr[i] = 1; // last element = 1
        for(int tPos = 0; tPos < i - 1; tPos++, aPos++)
            arr[aPos] = tmp[tPos] + tmp[tPos + 1];
        for(int j = 0; j < i + 1; j++)
            cout << arr[j] << " ";
        cout << endl;
    }
}

[2007]

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(void){
    int testCase;
    vector<int> answer;
    cin >> testCase;
    
    for(int i = 0; i < testCase; i++){
        string str;
        vector<char> pattern;
        bool foundPattern = false;
        int sPos = 1, pPos = 0;
        cin >> str;
        if(pattern.empty())
            pattern.push_back(str[0]);
        while(foundPattern != true){
            if(str[sPos] != pattern[0]){
                pattern.push_back(str[sPos++]);
                pPos++;
            }
            else{
                int tmp, i;
                for(i = 0, tmp = sPos; i < pPos; i++, tmp++)
                    if(pattern[i] != str[tmp])
                        break;
                if(i != pPos){
                    pattern.push_back(str[sPos++]);
                    pPos++;
                }
                else{
                    answer.push_back((int)pattern.size());
                    foundPattern = true;
                }
            }
        }
    }
    for(int i = 0; i < testCase; i++)
        cout << "#" << i + 1 << " " << answer[i] << endl;
}

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함