-replace KFC->BBQ변환하기

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

void replace(string& arr, string before, string after)
{
    int bn = before.length();
    int an = after.length();
    int a = 0;
    int b;
    while (1)
    {
        a = arr.find(before, a);
        if (a == -1)break;
        arr.erase(a, bn);
        arr.insert(a, after);
        a += an;
    }

}
int main()
{

string arr = "BBKFCAAAAAKFCAA";

// KFC 문자열을 BBQ로 바꾼 후 문자열 전체 출력~
// BBBBQAAAAABBQAA
replace(arr, "KFC", "BBQ");

return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main()
{

string arr = "ASDFASDF";
string before = "FA";
string changed = "KKK";

arr.replace(arr.find(before), before.length(), changed);
        // 바꾸고 싶은 문자열    // 바꾸고 싶은 문자열의 길이  //  바꿀 문자열 


return 0;
}  // replace  함수 사용법

- split빼고 출력하기

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

int main()
{
    string arr = "KFC#MC#MST#BBQ";  //
    int a, b;
    a = 0;
    vector<string>result;
    while (1)
    {
        b = arr.find('#', a);
        if (b == -1)
        {
            b = arr.length();
            string temp = arr.substr(a, b - a);
            result.push_back(temp);
            break;
        }
        string temp = arr.substr(a, b - a);
        result.push_back(temp);
        a = b + 1;
    }
    int t = result.size();
    for (int x = 0; x < t; x++)
    {
        cout << result[x]<<'\n';
    }

// KFC
// MC
// MST
// BBQ

return 0;
}

- kkk가 몇개 있는지 출력

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

int main()
{
    string arr = "sd9KKK89hKKK9d8d9f8KKKs9d8f9s";  //abs
    string target = "KKK";

int n = target.length();
int a, b;
int cnt = 0;
a = 0;
while (1)
{
    a = arr.find(target, a);
    if (a == -1)break;
    cnt++;
    a += n;


}
cout << cnt;

return 0;
}

- 문자열 숫자로 바꾼 후 7 더해서 출력

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

int findnum(string& arr, int index)
{
    int n = arr.length();
    for (int x = index; x < n; x++)
    {
        if (arr[x] >= '0' && arr[x] <= '9')
        {
            return x;
        }
    }
    return -1;

}
int findchar(string& arr, int index)
{

int n = arr.length();
for (int x = index; x < n; x++)
{
    if (!(arr[x] >= '0' && arr[x] <= '9'))
    {
        return x;
    }
}
return -1;
}
int main()
{
    string arr = "ABT1031BT1335Aab";
    //    0에서 9사이의 문자를 숫자로 바꾼 후
    //  7 더해서 출력하기
    int a, b;
    a = 0;
    while (1)
    {
        a = findnum(arr, a);
        if (a == -1)break;
        b = findchar(arr, a + 1);
        if (b == -1)break;

    string result = arr.substr(a, b - a);
    cout << stoi(result) + 7 << endl;
    a = b + 1;

}

return 0;
}

 

- 문자열 split

#include<iostream>
using namespace std;

int main()
{

string arr = "AB[426][5T[[[ATS]]]H]A[[K]";
int a, b;
a = 0;
while (1)
{
    a = arr.find('[', a);
    if (a == -1)break;

    b = arr.find(']', a+1);
    if (b == -1)break;
    
    while (1)
    {
        int c = arr.find('[', a + 1);
        if (c == -1)break;
        if (c < b)a = c;
        else break;
    }

    string result = arr.substr(a + 1, b - a - 1);
    cout << result << endl;
    a = b + 1;

}


return 0;
}

 

- 프로그래머스 프린터

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

int solution(vector<int> priorities, int location) {
    int answer = 0;
    struct node {
        int prio;
        int index;
    };
    queue<node>q;
    int t = priorities.size();
    for (int x = 0; x < t; x++) // 묶어서 큐 등록
    {
        q.push({ priorities[x],x });
    }


while (!q.empty())
{
    int Max = -21e8;
    for (int x = 0; x < q.size(); x++) {    // 우선순위가 가장 높은것 찾기
        if (Max < q.front().prio)
        {
            Max = q.front().prio;
        }
        q.push(q.front());
        q.pop();
    }

    if (Max > q.front().prio) { //큐에 프런트가 맥스보다 작으면 뒤로 가시오
        q.push(q.front());
        q.pop();
    }
    else // 큐에 프런트가 맥스라면
    {
        if (location == q.front().index) { // 입력받은 인덱스가 큐에 프런트의 인덱스와 같다면
            answer++;
            return answer;                  // 몇번째로 출력되는지 리턴하고
        }
        else {                 // 입력받은 인덱스가 큐에 프런트인덱스와 다르다면
            q.pop();        // 출력하고 
            answer++;       // 몇번째 증가
        }
    }
}
return answer;
}
int main()
{
    vector<int> priorities = {1,1,9,1,1,1};
    int location = 0;
    int ret=solution(priorities, location);
    cout << ret;
    return 0;
}

- 프로그래머스 송전탑

#include<iostream>
#include<vector>
#include<stack>
using namespace std;
vector<int>solu(vector<int> heights)
{
    vector<int>answer;
    // 뒤에서 부터 반복문 시작
    for (int y = heights.size() - 1; y >= 0; y--)
    {
        int index = 0;
        for (int x = y-1; x >= 0; x--) {
            if (heights[x] > heights[y]) // 앞에 타워랑 비교 후
            {
                index = x + 1;
                break;
            }
        }
        answer.insert(answer.begin(), index);
    }
    return answer;
}
int main()
{
    vector<int>heights = { 6,9,5,7,4 };
    vector<int>result = solu(heights);
    int size = result.size();
    for (int x = 0; x < size; x++)
    {
        cout << result[x]<<" ";
    }

}

- 큐 사용하지 않고 죠셉퍼스 구현

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

string str = "ABCDEFGH";
int used[100] = { 0 };

int nextPer(int idx)
{
    for (int i = idx + 1; i < str.length() * 2; i++) {
        int tx = i % str.length();
        if (used[tx] == 1) continue;
        return tx;
    }
    return 0;
}

int selectDie(int idx)
{
    //다섯번째 사람을 선택

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

    return idx;
}

int main()
{
    int cnt = str.length();
    int idx = -1;

    while (cnt != 1) {
        idx = selectDie(idx);
        used[idx] = 1;
        cnt--;
    }

    for (int i = 0; i < str.length(); i++) {
        if (used[i] == 0) {
            cout << str[i] << endl;
            break;
        }
    }
    

    return 0;
}

- 다양한 자료형의 큐 구현하기

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

struct Node{
    int a, b;
};

queue<vector<int>> q;

queue<int> q1[5];

int main()
{
    q.push({ 1, 1 });
    q.push({ 5, 2, 2, 3 });
    q.push({ 3, 3, 1 });
    q.push({ 4, 4, 1, 2, 3, 4, 5 });
    q.push({ 6, 5 });

    vector<int> ret = q.front();
    q.pop();

    q1[0].push(1);
    q1[3].push(3);

    return 0;
}

 

 

- 죠셉퍼스 : 번갈아가며 5번째 사람부터 없애기 마지막 살아남는 사람은?(스택)

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

queue<char> q;

void init()
{
    q.push('G');
    q.push('A');
    q.push('T');
    q.push('R');
}

int main()
{
    init();

    while (q.size() > 1) {

        //1. 네번 넣다 뻈다
        for (int i = 0; i < 4; i++) {
            int ret = q.front();
            q.pop();
            q.push(ret);
        }

        //2. 다섯번쨰는 뺀다.
        q.pop();
    }

    return 0;
}

- 스택에 노드 추가하기

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

struct Node {
    char ch;
    int a, b;
};

stack<Node> st;

int main()
{
    st.push({ 'A', 6, 3 });
    st.push({ 'C', 5, 2 });
    st.push({ 'B', 6, 5 });
    st.push({ 'A', 7, 3 });

    while (!st.empty()) {
        cout << st.top().ch << st.top().a << st.top().b << endl;
        st.pop();
    }
    
    return 0;
}

- 커서위치에따른 명령 수행하기(스택)

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

string str = "A<<<BCD<<<dd<";
stack<char> ll;
stack<char> rr;

void leftGo()
{
    if (ll.empty()) return;

    rr.push(ll.top());
    ll.pop();
}

void rightGo()
{
    if (rr.empty()) return;

    ll.push(rr.top());
    rr.pop();
}

void del()
{
    if (rr.empty()) return;
    rr.pop();
}

void insertChar(char ch)
{
    ll.push(ch);
}
int main()
{
    for (int i = 0; i < str.size(); i++) {
        if (str[i] == '<') leftGo();
        else if (str[i] == '>') rightGo();
        else if (str[i] == 'd') del();
        else insertChar(str[i]);
    }

    while (!ll.empty()) {
        leftGo();
    }

    while (!rr.empty()) {
        cout << rr.top();
        rr.pop();
    }
    
    return 0;
}

- 2차원스택의 짚

//2 x 5 배열은 고정 사이즈

1. 쌓인 짚을 출력할 좌표 (첫줄)
2. 짚 정보 개수 (n)
3. 짚 좌표와 아이디 정보들 (n개)


0 0
9
0 0 14
0 0 2
1 2 2
1 2 4
1 4 6
0 4 1
0 0 7
0 4 5
0 4 3
#include <iostream>
#include <queue>
#include <stack>
using namespace std;

stack<int> st[2][5];

int main()
{
    freopen_s(new FILE *, "Text.txt", "r", stdin);
    int tarY, tarX;
    cin >> tarY >> tarX;
    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int y, x, id;
        cin >> y >> x >> id;

        st[y][x].push(id);
    }

    stack<int> temp;

    while (st[tarY][tarX].empty() == false)
    {
        temp.push(st[tarY][tarX].top());
        st[tarY][tarX].pop();
    }

    while (!temp.empty()) {
        cout << temp.top() << " ";
        temp.pop();
    }
    
    return 0;
}

- 큐에 노드 추가하기 ----->추후 다시 다룰예정 (링크드리스트?)

#include <iostream>
#include <queue>
#include <stack>
using namespace std;

struct Node {
    int n;
    Node *next;
};

Node *st;

void push(int n)
{
    st = new Node({ n, st });
}

int top()
{
    return st->n;
}

void pop()
{
    st = st->next;
}

int main()
{
    push(1);
    push(2);
    push(3);
    push(4);

    for (int i = 0; i < 4; i++) {
        cout << top();
        pop();
    }
    
    return 0;
}

- 큐(12345를 뒤에서부터 하나씩 지우면서 반복하기)-> 출력 54321

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

queue<int> q; 
int dat[10];

void go(int n)
{
    dat[q.front()]++;
    q.pop();
}

int main()
{
    for (int y = 1; y <= 5; y++) {
        for (int x = 1; x <= y; x++) {
            q.push(x);
        }

        for (int x = 1; x <= y; x++) {
            go(x);
        }
    }

    for (int i = 1; i <= 5; i++) {
        cout << dat[i] << " ";
    }

    return 0;
}

DAT- Direct Address Table

- vector 기본사용법

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int arr[5] = { 1,2,3,4,5 };
    vector<int> vect = { 1,2,3,4,5 };
    
    vector<int>vect2;
    for (int x = 0; x < 5; x++)
    {
        int a;
        cin >> a;
        vect2.push_back(a);
    }
    vector<int>vect3(10);
    for (int x = 0; x < 5; x++)
    {
        cin >> vect3[x];
    }
    return 0;
}
    vector<int>vect(10, 4); //10크기에 4로 초기화
    vect.insert(vect.begin(), 6); //맨앞에 6추가
    vect.insert(vect.begin()+3, 6); //원하는 자리(앞에서 3번째 자리) 6추가
    
    vect.pop_back(); //push_back() 반대 = 맨뒤 제거
    
    vect.erase(vect.begin() + 3); //원하는 자리 지우기

-2차원vector

    vector<vector<int>>t = { {1,2,3},{4,5,6} }; //이차원백터

	for (int y = 0; y < t.size(); y++) {
		for (int x = 0; x < t[y].size(); x++) {
			cout << t[y][x] << " ";
		}
		cout << endl;
	}
  int n;
    cin >> n;
    //vector<vector<int>>map(n);
    //for (int y = 0; y < n; y++)
    //{
    //    for (int x = 0; x < n; x++)
    //    {
    //        int a;
    //        cin >> a;
    //        map[y].push_back(a);
    //    }
    //}
    vector<vector<int>>map(n,vector<int>(n));
    for (int y = 0; y < n; y++) {
        for (int x = 0; x < n; x++)
        {
            cin >> map[y][x];
        }
    }

- 인접리스트 -> vector 사용시 편함

#include<iostream>
using namespace std;
struct node {
    int num;
    node* next;
};
node *arr[3];
void addnode(int from, int value)
{
    arr[from] = new node({ value,arr[from] });
}
int main()
{
    addnode(0, 1);
    addnode(0, 2);
    addnode(0, 3);
    addnode(1, 3);
    return 0;
}

 

- dat활용 counting sort

#include<iostream>

using namespace std;

int main()
{
	int arr[] = { 1,3,1,5,7,4,5 };
	int bucket[10] = {0};
	int sort[10] = {0};

	for (int i = 0; i < 7; i++) { //버켓등록 bucket[arr[x]]++;
		int target = arr[i];
		bucket[target] +=1;
	}

	for (int i = 1; i < 10; i++) { //누적합
		bucket[i] += bucket[i - 1];
	}

	for (int x = 0; x < 7; x++) { //설계 후 값 채워넣기
		int t = arr[x];
		sort[bucket[t]--] = t;
	}

	for (int i = 1; i <= 7; i++)
		cout << sort[i] << " ";

	return 0;

}

- 구조체배열(민코딩19-6)

#include<iostream>

using namespace std;

struct Train
{
	int win;
	char name[8];
};

int main()
{
	string name;
	int age;

	cin >> name >> age;
	Train t[7] = { {15, "summer"}, {33, "cloe"}, {24, "summer"}, {28, "niki"}, {32, "jenny"}, {20, "summer"}, {40, "coco"} };

	for (int i = 0; i < 7; i++) {
		if(t[i].name==name && t[i].win==age)
			cout << i;
	}
	
	return 0;

}

- direct(민코딩19-1)

#include <iostream>

using namespace std;

int map[3][3] = {
		3,5,4
		,1,1,2
		,1,3,9
};

int directY[] = { 1,-1,0,0 };
int directX[] = { 0,0,1,-1 };

int getNum(int y, int x)
{
	int sum = 0;
	for (int t = 0; t < 4; t++) {
		int ny = y + directY[t];
		int nx = x + directX[t];
		if (ny < 0 || nx < 0 || ny > 3 || nx > 3) continue;
		sum += map[ny][nx];
	}
	return sum;
}

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

	cout<< getNum(y, x);


	return 0;

}

- 이중포인터(민코딩 L19-4)

#include <iostream>

using namespace std;

int main() {
	char G;
	char* p=&G;
	char** t = &p;
	char* K=&G;
	char** Q = &K;

	cin >> G;

	cout << **t <<" " << *K;

	return 0;
}

 

- 백트래킹 -> 가지치기

- 완전탐색(브루트포스) -> for문/ 재귀 

- DFS -> 그래프탐색

 

//--------------------0802 - 백트래킹

//5국가 중 3국가 중복없이 선택(JKB != JBK) -> 조합
#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;

char a[6] = "JKBSC";
char path[10];
int used[4];

void abc(int level, int st) {
	if (level == 3) {
		cout << path << endl;
		return;
	}

	for (int i = st; i < 5; i < i++) {
		if (used[i] == 1) continue;
		used[i] = 1;
		path[level] = a[i];
		abc(level + 1, i+1);
		used[i] = 0;
	}
	
}

int main() {
	
	abc(0,0);

	return 0;
}

//올림픽 금은동 J가 무조건 금 중복x // JKB != JBK -> 순열
#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;

char a[5] = "JKBS";
char path[10];
int used[4];

void abc(int level) {
	if (level == 3) {
		if (path[0] == 'J')
			cout << path << endl;
		return;
	}

	for (int i = 0; i < 4; i < i++) {
		if (used[i] == 1) continue;
		used[i] = 1;
		path[level] = a[i];
		abc(level + 1);
		used[i] = 0;
	}

}

int main() {

	abc(0);

	return 0;
}

//h세로, w가로 - 각 세로라인에서 값 하나씩 선택(같은 세로라인 선택금지) 최소값 구하기
//4 4
//3 5 6 3
//1 -3 -6 1
//5 -7 -6 5
//1 1 5 4
 
#include <iostream>
using namespace std;

int w;
int h;
int map[10][10];
int mini = 21e8;
int used[10];

void run(int lev, int sum)
{
	if (lev == h) {
		if (sum < mini) mini = sum;

		if (sum == -9) {
			int d = 1;
		}
		return;
	}

	for (int i = 0; i < w; i++) {
		if (used[i] == 1) continue;
		used[i] = 1;
		run(lev + 1, sum + map[lev][i]);
		used[i] = 0;
	}

}

int main()
{
	freopen_s(new FILE *, "Text.txt", "r", stdin);
	cin >> h >> w;

	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> map[y][x];
		}
	}
	run(0, 0);
	cout << mini;

	return 0;
}


//n개의 좌표입력받아 *3 *2 *1하였을때 최대, 최소 구하기-------ⓗ
#include <iostream>
using namespace std;

int map[3][4] = {
	1, 2, 3, 4,
	5,4, -1, -2,
	-3, 0, 1, 2
};

int n = 3;
int used[3][4];

int maxi = -21e8;
int mini = 21e8;

void run(int lev, int sum)
{
	if (lev == n) {

		if (sum > maxi) maxi = sum;
		if (sum < mini) mini = sum;

		return;
	}

	for (int y = 0; y < 3; y++) {
		for (int x = 0; x < 4; x++) {
			if (used[y][x] == 1) continue;
			used[y][x] = 1;
			run(lev + 1, sum + (n - lev) * map[y][x]);
			used[y][x] = 0;
		}
	}

}

int main()
{
	//n 곳을 선택
	//같은 좌표 중복 선택 불가
	//3배, 2배, 1배 가중치 값 존재
	//합의 최댓값과 최솟값 둘다 출력하기

	run(0, 0);

	cout << maxi << endl;
	cout << mini << endl;

	return 0;
}

//direct 사용하여 대각선방향 합의 최대 구하기
#include <iostream>
using namespace std;

int map[5][6] = {
	1, 0, 2, 1, 2, 1,
	-1, 0, 1, 1, 2, 2,
	3, 3, 1, 2, 3, 4,
	5, 5, 5 ,3, 1, 2,
	1, 2, 3, 4, 0, 0
};

int directY[5] = { -1, -1, 1, 1, 0 };
int directX[5] = { -1, 1, -1, 1, 0 };

int getNum(int y, int x)
{
	int sum = 0;
	for (int t = 0; t < 5; t++) {
		int ny = y + directY[t];
		int nx = x + directX[t];
		if (ny < 0 || nx < 0 || ny >= 5 || nx >= 6) continue;
		sum += map[ny][nx];
	}
	return sum;
}

int main()
{
	//세 좌표를 입력받고
	//다섯 곳의 합을 구하는데
	//세 곳중 MAX값 출력

	int maxi = -21e8; //-21억
	for (int t = 0; t<3; t++) {
		int y, x;
		cin >> y >> x;

		int ret = getNum(y, x);
		if (maxi < ret) maxi = ret;

	}
	cout << maxi;

	return 0;
}


//배열에서 중복없이 세가지수 조합 -------------------->중요★
//2.재귀사용
#include <iostream>
#include <vector>
using namespace std;

vector<int> v = { 4, 5, 1, 7, 9, 2, 6 };
char path[10];

void run(int lev, int start)
{
	if (lev == 3) {
		cout << path << endl;
		return;
	}

	for (int i = start; i < v.size(); i++) {
		path[lev] = v[i] + '0';
		run(lev + 1, i + 1);
	}
}

int main()
{
	run(0, 0);

	return 0;
}

//1. 3중 for문 사용
#include <iostream>
#include <vector>
using namespace std;

vector<int> v = { 4, 5, 1, 7, 9, 2, 6 };

int main()
{
	for (int a = 0; a < v.size(); a++) {
		for (int b = a + 1; b < v.size(); b++) {
			for (int c = b + 1; c < v.size(); c++) {
				cout << v[a] << v[b] << v[c] << endl;
			}
		}
	}

	return 0;
}
 

// 백트래킹을 이용한 미로찾기------>추후 다시 다룰예정
#include <iostream>
#include <Windows.h>
using namespace std;

int map[8][8] = {
	0,1,0,0,1,0,0,0,
	0,1,0,0,0,0,1,0,
	0,0,0,0,1,1,1,0,
	0,1,1,0,1,0,1,0,
	0,1,0,0,0,0,1,0,
	0,0,0,0,1,1,0,0,
	0,1,1,0,1,1,0,1,
	0,1,1,0,1,0,0,0,
};

int used[8][8];
int direct[4][2] = { -1, 0, 1, 0, 0, 1, 0, -1 };

void print(int nowY, int nowX)
{
	system("cls");
	for (int x = 0; x < 10; x++) cout << "■";
	cout << endl;
	for (int y = 0; y < 8; y++) {
		cout << "■";
		for (int x = 0; x < 8; x++) {
			if (y == nowY && x == nowX) {
				cout << "★";
			}
			else if (map[y][x] == 0) {
				cout << "  ";
			}
			else {
				cout << "■";
			}
		}
		cout << "■";
		cout << endl;
	}
	for (int x = 0; x < 10; x++) cout << "■";
	Sleep(50);
}

void run(int nowY, int nowX)
{
	print(nowY, nowX);
	if (nowY == 7 && nowX == 7) {
		cout << "발견" << endl;
		system("color 1F");
		Sleep(1000);
		system("color 07");
		return;
	}

	for (int t = 0; t < 4; t++) {
		int ny = nowY + direct[t][0];
		int nx = nowX + direct[t][1];

		if (ny < 0 || nx < 0 || ny >= 8 || nx >= 8) continue;
		if (map[ny][nx] == 1) continue;
		if (used[ny][nx] == 1) continue;
		used[ny][nx] = 1;
		run(ny, nx);
		print(nowY, nowX);
		used[ny][nx] = 0;
	}

}

int main()
{
	used[0][0] = 1;
	run(0, 0);

	return 0;
}

//ABCD친구들이 영화관가는 경우의수 0~ABCD
#include<iostream>
using namespace std;
char path[10];
char name[3] = "ox";
char fri[5] = { "ABCD" };
void abc(int level)
{
	if (level == 4)
	{
		for (int x = 0; x < level; x++)
		{
			if (path[x] == 'o')cout << fri[x];
		}
		cout << endl;
		return;
	}
	for (int x = 0; x < 2; x++)
	{
		path[level] = name[x];
		abc(level + 1);
		path[level] = 0;
	}
}
int main()
{
	abc(0);
	return 0;
}

//A~입력받은 알파벳까지 중복제외 조합 모두출력 (AB 또는 BA처럼 연달아 출력 금지)
#include<iostream>
using namespace std;

char path[10];
char ch;
int used[100];

int abs(int v)
{
	if (v < 0) return -v;
	return v;
}

void run(int lev)
{
	if (lev >= 2 && abs(path[lev - 2] - path[lev - 1]) <= 1) return;

	if (ch - 'A' + 1 == lev) {
		cout << path << endl;
		return;
	}

	for (char c = 'A'; c <= ch; c++) {
		if (used[c] == 1) continue;
		used[c] = 1;
		path[lev] = c;
		run(lev + 1);
		used[c] = 0;
	}
}

int main()
{
	cin >> ch;

	run(0);

	return 0;
}

//설계후 구현하기 - 2차원 배열의 왼쪽과 위 비교후 작은값의 +3해서 완성하기
#include<iostream>
using namespace std;

int arr[4][6] = {
	{5,8,3,2,6,5},
	{4,0,0,0,0,0},
	{2,0,0,0,0,0},
	{6,0,0,0,0,0}, };

int main()
{

	for (int i = 1; i < 4; i++) {
		for (int j = 0; j < 5; j++) {
			if (arr[i][j] > arr[i - 1][j + 1])
				arr[i][j + 1] = arr[i - 1][j + 1] + 3;
			else {
				arr[i][j + 1] = arr[i][j] + 3;
			}
		}
	}

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 6; j++) {
			cout << arr[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}

//숫자카드에서 이웃하는 카드의 합 15미만
#include<iostream>
using namespace std;

int nums[5] = { 11, 5, 7, 4, 9 };
int path[10];

void run(int lev)
{
    if (lev >= 2 && path[lev - 2] + path[lev - 1] >= 15) return;

    if (lev == 4) {
        for (int i = 0; i < lev; i++) {
            cout << path[i] << " ";
        }
        //cout << endl;
        cout << "\n";
        return;
    }

    for (int i = 0; i < 5; i++) {
        //if (lev >= 1 && path[lev - 1] + nums[i] >= 15) continue;
        path[lev] = nums[i];
        run(lev + 1);
        path[lev] = 0; //옵션
    }
}


int main()
{
    run(0);
    

    return 0;
}

//n개의 주사위 합 10 이하인경우만 0+0+0=0 출력
#include<iostream>
using namespace std;

int n = 3;
char vect[10];

void run(int lev, int sum)
{
	if (sum > 10) return;

	if (lev == n) {
		int i = 0;
		for (i = 0; i < n - 1; i++) {
			cout << vect[i] << " + ";
		}
		cout << vect[i];
		cout << " = " << sum;
		cout << endl;
		return;
	}

	for (int i = 1; i <= 6; i++) {
		vect[lev] = '0' + i;
		run(lev + 1, sum + i);
	}

}

int main()
{
	run(0, 0);


	return 0;
}

'Algorithm > C++' 카테고리의 다른 글

[21.08.04]큐/스택 활용하기  (0) 2021.08.04
[21.08.03] dat활용 counting sort와 vector사용법  (0) 2021.08.03
[21.07.30] 재귀함수, 트리, DAT  (0) 2021.08.01
[21.07.29]스택, 큐  (0) 2021.08.01
[21.07.28] 문자열 파싱하기  (0) 2021.08.01
------------------------0730-재귀함수
* 
* ------------------체스판 코드 확인하기
#include <iostream>

using namespace std;

char path[10];
char name[5] = "ABCD";
int input;
int s = 0;
char used[4];

void abc(int level) {
	if (level == input) {
		s++;
		return;
	}

	for (int i = 0; i < input; i++) {
		if (used[i] == 1) continue;
		used[i] = 1;
		path[level] = name[i];
		abc(level + 1);
		path[level] = 0;
		used[i] = 0;
	}

}

int main() {

	cin >> input;

	abc(0);

	cout << s;

	return 0;
}
* 
* 
* //이웃해서 같은카드 받지않는 소스 AAA(x) ABA(o) 

#include<iostream>
using namespace std;

char path[10];


void abc(int level) {
	if (level>=2 && path[level - 1] == path[level - 2]) return; //2번방법

	if (level == 3) {
		cout << path << endl;
		return;
	}

	for (int i = 0; i < 4; i++) {
		//if (level>0 && path[level - 1] != i + 'A')) { //1번방법
			path[level] = i + 'A';
			abc(level + 1);
			path[level] = 0;
		//}
	}
}


int main()
{
	abc(0);

	return 0;

}

* 
* 
//B가 들어가는 경우 모두 제외
//1 진입하지 않는경우 2 진입했다가 return

#include<iostream>
using namespace std;

char path[10];


void abc(int level) {
	if (level>0 && path[level - 1] == 'B') return; //2번방법

	if (level == 3) {
		cout << path << endl;
		return;
	}

	for (int i = 0; i < 4; i++) {
		//if (!(i + 'A' == 'B')) { //1번방법
			path[level] = i + 'A';
			abc(level + 1);
			path[level] = 0;
		//}
	}
}


int main()
{
	abc(0);

	return 0;

}
* 
//C로 시작하는 경우 제외
//1 진입하지 않는경우 2 진입했다가 return

#include<iostream>
using namespace std;

char path[10];


void abc(int level) {
	//if (path[0] == 'C') return; //2번방법

	if (level == 3) {
		cout << path << endl;
		return;
	}

	for (int i = 0; i < 4; i++) {
		//if (!(level == 0 && i + 'A' == 'C')) { //1번방법
			path[level] = i + 'A';
			abc(level + 1);
			path[level] = 0;
		//}
	}
}
* -------카드3장뽑기 중복제외 순열
#include<iostream>
using namespace std;

char path[10];
int used[4]; // A B C D 사용됐는지 확인

void abc(int level) {
	if (level == 3) {
		cout << path << endl;
		return;
	}

	for (int i = 0; i < 4; i++) {
		if (used[i] == 1) continue; //다음 반복 실행
		used[i] = 1;
		path[level] = i+ 'A';
		abc(level + 1);
		path[level] = 0;
		used[i] = 0;//출력된 경우 다시 초기화
	}
}


int main()
{
	abc(0);

	return 0;

}
* ------------------5개배열에 OX출력하는 모든 경우의수
//lv :5, br : 2

#include<iostream>
using namespace std;

int n[5] = { 0 };
char path[10];
char name[3] = "OX";

void abc(int level) {
	if (level == 5) {
		cout << path << endl;
		return;
	}

	for (int x = 0; x < 2; x++) {
		path[level] = name[x];
		abc(level + 1);
		path[level] = 0;
		//path[level] = 0;
	}
}

int main()
{
	abc(0);

	return 0;

}
* 
* -------------------주사위 n개 돌렸을때 나올수있는 모든 경우의수
//lv :n, br : 6

#include<iostream>
using namespace std;

int n;
char path[7];
void abc(int level) {
	if (level == n) {
		cout << path <<endl;
		return;
	}

	for (int x = 0; x < 6; x++) {
		path[level] = '1' + x;
		abc(level + 1);
		//path[level] = 0;
	}
}

int main()
{
	cin >> n;
	abc(0);

	return 0;

}
* 
* -----------------ABCD카드더미에서 3가지글자조합 모든경우의수 출력
#include<iostream>
using namespace std;

char path[5];
void abc(int level) {
	if (level == 3) {
		cout << path <<endl;
		return;
	}

	for (int x = 0; x < 4; x++) {
		path[level] = 'A' + x;
		abc(level + 1);
		path[level] = 0;
	}
}

int main()
{
	abc(0);

	return 0;

}
* 
* -------------트리의 가장 하위레벨 출력하기
#include<iostream>
using namespace std;

int path[10];
void abc(int level) {
	if (level == 2) {
		for (int x = 0; x < level; x++) {
			cout << path[x] << " ";
		}

		cout << endl;
		return;
	}

	for (int x = 0; x < 3; x++) {
		path[level] = x + 1;
		abc(level + 1);
		path[level] = 0;
	}
}

int main()
{
	abc(0);

	return 0;

}
* 
* 
* 문자열에 사용된 알파벳 알아보기(중복출력x dat문)
* #include<iostream>
using namespace std;

int de;
int main()
{
    char vect[9] = "ABCQADBC"; 
    
    int flag[100] = { 0 }; 
    for (int i = 0; i < 8; i++) {
        int val = vect[i]; 
        flag[val] = 1;
    }

    for (char target = 'A'; target <= 'Z'; target++) {
        if (flag[target] == 1)
            cout << target;
    }

    return 0;
    
}
* -------------------문자열에 사용된 알파벳 알아보기(중복출력x 이중for문)
* #include<iostream>
using namespace std;

int de;

int main()
{
	char arr[9] = "ABCQADBC";
	char s[8];

	for (char target = 'A'; target <= 'Z'; target++) {
		int flag = 0;
		for (int i = 0; i < 8; i++) {
			if (arr[i] == target) {
				flag = 1;
			}
		}
		if (flag == 1)cout << target;
	}


	return 0;

}
* 
*  ---------------------------배열에 1~9까지 각각 몇개있는지(dat이용)
* #include<iostream>
using namespace std;

// dat이용하기 cnt 변수를 여러개 만들었음
int de;
int arr[8] = { 3,2,1,2,5,3,2,1 }; // 수 0~9

int main()
{
	int cnt[10] = { 0 }; // cnt 변수 10개
	for (int i = 0; i < 8; i++) {
		int val = arr[i];
		cnt[val] ++; // counting 3 2 1 2 5 3 2 1
	}

	for (int val = 0; val < 10; val++) {
		if (cnt[val] != 0) {
			cout << val << " : " << cnt[val] << endl;
		}
	}


	return 0;

}
* 
* ---------------------------배열에 1~9까지 각각 몇개있는지(2중for문)
* #include <iostream>

using namespace std;

int arr[] = { 3,2,1,2,5,3,2,9 };

int main() {
	int c[10] = { 0 };

	for (int target = 0; target < 10; target++) {
		int cnt = 0;
		for (int i = 0; i < 8; i++) {
			if (arr[i] == target) cnt++;
		}
		if (cnt != 0)
			cout << target << " : " << cnt << "개" << "\n";
	}

	return 0;
}
* 
* ----------원하는 순서로 출력하기
#include <iostream>

using namespace std;

int arr[7] = { 3,7,5,9,8,7 };
int order[6] = { 2,3,1,5,-1,4 };

void run(int now) {
	cout << arr[now] << " ";
	if (order[now] == -1) {
		return;
	}
	run(order[now]);

	return;
}

int main() {

	run(0);

	return 0;
}
* 
* 
#include <iostream>

using namespace std;

void abc(int level) {


	if (level == 2) {
		return;
	}

	//abc(level +1);
	//abc(level +1);

	for (int i = 0; i < 2; i++) {
		abc(level + 1);
	}
	int d = 1;
}

int main() {

	abc(0);

	return 0;
}

* 
* 
#include <iostream>

using namespace std;

int n[] = {4,7,9,1,5};

void abc(int x) {

	cout << n[x]<<" ";

	if (x == 4) {
		return;
	}


	abc(x + 1);
	cout << n[x] << " ";

	int d = 1;
}

int main() {

	abc(0);

	return 0;
}

* 
* 
#include <iostream>

using namespace std;

int n;

void abc(int x) {
	cout << x<<" ";

	if (x == 1) {
		return;
	}


	abc(x - 1);
	cout << x << " ";

	int d = 1;
}

int main() {

	cin >> n;
	abc(n);

	return 0;
}
* 
* 
#include <iostream>

using namespace std;

void abc(int x) {
	if (x == 2) {
		return;
	}

	abc(x + 1);

	int d = 1;
}

int main() {

	abc(0);

	return 0;
}

* 
* 
#include <iostream>

using namespace std;
int n;
int path[6];

void abc(int level) {
	if (level == n)
	{
		for (int x = 0; x < level; x++) {
			cout << path[x] << " ";
		}
		cout << endl;
		return;
	}

	for (int x = 1; x <= 6; x++) {
		path[level] = x;
		abc(level + 1);
	}

}

int main() {
	cin >> n;

	abc(0);

	return 0;
}

 

- 체스판 참고

#include<iostream>
using namespace std;

char map[4][5]; // 디버깅용도
int used[4]; // 0 1 2 3 세로 라인사용여부 
int cnt = 0;
int input;

void run(int y) {
	if (y == input) {
		cnt++;
		return;
	}
	for (int x = 0; x < input; x++) {
		if (used[x] == 1) continue;
		used[x] = 1;// 사용함
		map[y][x] = '#';
		run(y + 1);
		map[y][x] = '_';
		used[x] = 0; // 원상복구
	}
}
int main()
{

	cin >> input;
	for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) map[y][x] = '_'; }
	run(0);
	cout << cnt;
	return 0;
}
------------------------------------0729
* 
* 
#include<iostream>
#include<queue>

using namespace std;
int de;

void BBQ(int* q) {
	*q = 10;
	return;
}

void ABC(int* p){
	BBQ(p);
	return;
	}


int main() {
	int ret;
	ABC(&ret);
	cout << ret;
	return 0;
}
* 
* ---------------큐
#include<iostream>
#include<queue>

using namespace std;

int main() {
	string str = "___AB_Q_R_T___C";
	queue<char> qu;

	int n = str.length();
	for (int i = 0; i < n; i++) {
		if (str[i] != '_')
		{
			qu.push(str[i]);
			str[i] = '_';
		}
	}
	int t = 0;
	while (!qu.empty()) {
		str[t] = qu.front();
		qu.pop();
		t++;
	}

	return 0;
}

* 
*
#include<iostream>
#include<queue>

using namespace std;

int main() {

	queue<int> qu;

	qu.push(3);
	qu.push(5);
	qu.push(10);
	qu.push(15);
	qu.push(20);

	int ret = qu.front(); //읽기
	qu.pop(); //제거
	ret = qu.front();
	qu.pop();

	qu.push(7);
	qu.push(11);

	while (!qu.empty()) {
		int ret = qu.front();
		qu.pop();

		cout << ret << " ";
	}
	return 0;
}

*
*------------------스택
#include<iostream>
#include<stack>

using namespace std;

int main() {
	int arr[] = { 3,2,1,5,7 };

	stack<int> st;

	for (int i = 0; i < 5; i++) {
		cout << arr[i] << " ";
		st.push(arr[i]);
	}

	while (!st.empty()) {
		int ret = st.top();
		st.pop();
		cout << ret << " ";
	}

	return 0;
}
* 
#include<iostream>
#include<stack>

using namespace std;

int main() {

	stack<int> bucket;
	bucket.push(3);
	bucket.push(2);
	bucket.push(1);
	bucket.push(7);
	bucket.push(5);

	int ret = bucket.top();
	bucket.pop();
	ret = bucket.top();
	bucket.pop();

	for (int i = 1; i <= 5; i++) {
		bucket.push(i);
	}

	while (!bucket.empty()) {
		int ret = bucket.top();
		bucket.pop();

		cout << ret << endl;
	}

	return 0;
}

* ------------------------------정올 큐
* #include<iostream>
#include<queue>

using namespace std;

int main() {
	int a, n;
	char order;
	queue<int> st;

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> order;
		if (order == 'i') {
			cin >> a;
			st.push(a);
		}
		else if (order == 'o') {
			if (st.empty()) cout << "empty"<<endl;
			else {
				int ret = st.front();
				st.pop();
				cout << ret << endl;
			}
		}
		else if (order == 'c') {
			cout << st.size() << endl;
		}

	}

	return 0;
}
*------------------------------정올 스택
#include<iostream>
#include<stack>

using namespace std;

int main() {
	int a,n;
	char order;
	stack<int> st;

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> order;
		if (order == 'i') {
			cin >> a;
			st.push(a);
		}else if (order == 'o') {
			if (st.empty()) cout << "empty"<<endl;
			else {
				int ret = st.top();
				st.pop();
				cout << ret << endl;
			}
		}
		else if (order == 'c') {
			cout << st.size() << endl;
		}

	}

	return 0;
}

ASCII/ Unicode 차이

char 정리

char string.h

strlen을 for안에 넣으면 안됨

strcpy 대신 memcpy 쓰는것이 더 효율적

string class 소개 (strlen, strcpy, strcat, strstr)

 

strcmp(str1, str2) == 0 (동일 결과 나옴 !strcmp(str1,str2)) -> 같음

-> size, find, substr

 

-------------------------------------------0728
* 
* 
#include<iostream>

using namespace std;

int myswap (int* p, int* q) {
	int backup = *p;
	*p = *q;
	*q = backup;

	*p = 7;
	return 3;
}

int main() {
	int a = 3;
	int b = 7;

	myswap(&a, &b);

	cout << a << b;

	return 0;
}

* 
* 
#include<iostream>

using namespace std;

int ABC (int* p) {
	*p = 7;
	return 3;
}

int main() {
	int a = 3;
	int b = ABC(&a);

	cout << a << endl;
	cout << b;

	//int ret;

	//int a = ABC(&ret);
	//cout << ret << endl; //7
	//cout << a; //3
	//
	return 0;
}

* 
* 
#include<iostream>
using namespace std;

int main() {
	int a; // 선언
	a = 3;  // 대입연산 저장

	// 1. 포인터변수는 주소값을 저장한다.
	int* p; // 포인터 변수 선언
	p = &a; // 주소값 저장

	// 2. 주소값을 저장하면 가리킨다 라고 표현을 합니다.

	// 3. 가리키게 되면 원격조종을 할 수 있다.(p를 통해서 a를 조종)
	cout << *p << endl; //3
	*p = 17;
	cout << a; //17

	return 0;

}

* /////////////////////////////////////////////////////////////////////
* 구조체 예시2
#include<iostream>
#include<vector>

using namespace std;

struct Node { //type을 만듦, 변수만든것 x
	int a;
	char b;
};


int main() {

	Node arr[6];


	for (int i = 0; i < 6; i++) {
		arr[i].a = 9-i;
		arr[i].b = char('a'+i);

	}

	for (int i = 0; i < 6; i++) {
		cout << arr[i].a << " ";
	}
	cout << endl;
	for (int i = 0; i < 6; i++) {
		cout << arr[i].b << " ";
	}

	return 0;
}

* 
* 구조체 예시1
#include<iostream>
using namespace std;

struct ABC { //type을 만듦, 변수만든것 x
	int a;
	int b;
};

int main() {
	ABC t = { 3,7 }; // 선언 + 초기화;

	ABC q;
	q.a = 3;
	q.b = 7;

	ABC v;
	v = { 3,7 };

	return 0;
}

* 
* 
//유효성검사
#include <iostream>
#include <string>
using namespace std;

string id;

bool isValid()
{
	int n = id.size();
	//id가 3 ~ 8 글자 까지 허용 (3, 8 포함)
	if (!(n >= 3 && n <= 8)) return false;

	//첫글자는 대문자로 시작해야함
	if (!(id[0] >= 'A' && id[0] <= 'Z')) return false;

	//id에 영어 대소문자만 허용
	for (int i = 0; i < n; i++) {
		if (id[i] >= 'A' && id[i] <= 'Z') continue;
		if (id[i] >= 'a' && id[i] <= 'z') continue;
		return false;
	}


	//탈락조건만 넣고,
	//여기까지 살아남았으면 합격
	return true;
}

int main()
{
	//cin >> id;
	id = "SDasd";
	bool ret = isValid();

	cout << ret;

	//출력결과 (둘중 하나 출력)
	//OK VALID
	//NO INVALID

	return 0;
}

* 
* 
// |기준 문자열 파싱
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str = "ABO|OOO|ABCAB|C";
	string table[4];
	int tn = 0;

	// '|' bar를 기준으로 문자열들을 파싱하여
	//각 테이블 index에 순서대로 채워넣기

	//table[0] = ABO
	//table[1] = OOO
	//...

	int a = 0;
	int b = 0;

	str += "|";
	while (1) {
		b = str.find('|', a);
		if (b == -1) break;
		int size = b - a;

		table[tn] = str.substr(a, size);
		tn++;
		a = b + 1;
	}


	return 0;
}

* 
* 
//////////////////////////문자열변환 (미완) 유튜브 다시보기
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str = "ABCOOOABCOOOOOOABCABC";

	//ABC 다 찾아서
	//KFCWORLD 라는 문자열로 전부 교체 replace

	int a = 0;
	//int sum=0;
	int b = 0;
	while (1)
	{
		a = str.find('ABC', a);
		if (a == -1) break;

		str.erase(a, 2);
		str.insert(a,"KFCWORLD");

		a += 8;
	}

	cout << str;

	//괄호 안에 있는 수들의 전체 합 출력 (정답 : 9,555)

	return 0;
}

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

int main()
{
	string str = "QWE[1234]TR[1000][3000]BBQ[4321]AA";

	//괄호 안에 있는 수들의 전체 합 출력 (정답 : 9,555)

	int a = 0;
	int b = 0;
	int sum=0;

	while (1)
	{
		a = str.find('[', a);
		if (a == -1) break;
		b = str.find(']', a + 1);

		int size = b - a - 1;
		string temp = str.substr(a + 1, size);
		int n = stoi(temp);
		sum += n;

		a = b + 1;
	}

	cout << sum;

	return 0;
}

* 

#include <iostream>
using namespace std;

int main()
{
	string str = "ABCDEF[ABCD]ABCDE[ERW]QQ[RRTYU]QQ[Q]";

	//괄호 안에 있는 [ ] 문자열을 파싱하여서
	//1. 한 문자열로 합치기 (+)
	//2. 역순으로 출력 (for 출력)

	//ABCDERWRRTYU
	//UYTRRWREDCBA

	int a = 0;
	int b = 0;
	string sum;
	while (1)
	{
		a = str.find('[', a);
		if (a == -1) break;
		b = str.find(']', a + 1);

		int size = b - a - 1;
		string temp = str.substr(a + 1, size);
		sum += temp;

		a = b + 1;
	}


	for (int i = sum.size() - 1; i >= 0; i--) {
		cout << sum[i];
	}

	return 0;
}

* 

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

int main()
{
	string str = "ABCDEF[1234]ABCDE";

	//열기 괄호 [ 와
	//닫기 괄호 ] 사이에 있는 문자열을 추출(파싱)

	//출력 find 메서드 substr 메서드 사용하시면 됩니다.

	int a = str.find('[');
	int b = str.find(']', a + 1);
	int size = b - a - 1;

	string ret = str.substr(a + 1, size);

	int n = stoi(ret);

	//int sum = 0;
	//for (int i = 0; i < ret.size(); i++) {
	//	sum = sum * 10 + (ret[i] - '0');
	//}
	//cout << sum;

	return 0;
}

*

#include <cstring> //strcmp strlen 들어있는 "C언어" Lib
#include <string.h> //strcmp strlen 들어있는 "C언어" Lib

#include <string> //strcmp strlen 대신 string class를 쓸때 사용하는 Lib
using namespace std;

* 

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

string str = "ABCDABCEABCABCABABCDE";

int main()
{
	//ABC가 총 몇개있는지 출력하는 프로그램
	int a = 0;
	int cnt = 0;
	while (1)
	{
		int ret = str.find("ABC", a);
		if (ret == -1) break;
		cnt++;
		a = ret + 1;
	}

	cout << cnt;

	return 0;
}

* 

//패턴찾기
#include <iostream>
using namespace std;

//string 을 안쓰고 푸는 문제
char vect[100] = "AKJSDKAJKLALAJKLASDLKASJLKASJLD";
char target[10] = "AJK";

//몇개가 있는지 검색하는 문제
//Library 없이 구현 (string.h 도 안씀)

int tn;

int isSame(int index)
{
	for (int i = 0; i < tn; i++) {
		if (vect[index + i] != target[i]) return 0;
	}
	return 1;
}

int main()
{
	tn = strlen(target);
	int n = strlen(vect);

	int cnt = 0;
	for (int i = 0; i <= n - tn; i++) {
		int ret = isSame(i);
		if (ret == 1) cnt++;
	}

	//strcmp사용(not 필수)
	//for (int i = 0; i <= n - tn; i++) {
	//	if (!strncmp(&vect[i], target, tn)) cnt++;
	//}

	
	cout << cnt;

	return 0;
}

* 

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

int main()
{
	//아이디랑 pass를 입력받음
	//아이디가 "good" password가 "1234" 이면
	//"로그인성공"
	//아이디는 맞는데 비번이 틀리면
	//"비번 노노"
	//아이디가 틀렸으면
	//"그런사람 없어요"

	string id;
	string pass;
	cin >> id >> pass;

	//if (!strcmp(id, "good") && !strcmp(pass, "1234"))
	if (id == "good" && pass == "1234") {
		cout << "로그인성공";
	}
	else if (id != "good") {
		cout << "그런사람 없어요";
	}
	else if (id == "good" && pass != "1234") {
		cout << "비번 ㄴㄴ";
	}
	return 0;
}

*

//strcmp와 string비교
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str = "ABCDE";
	string abc = "QWE";

	string bbq = str + abc;

	if (str == abc) {
		cout << "비교";
	}
	else {
		cout << "ㄴ";
	}

	char buf[10] = "ABCDE";
	char buf2[10] = "ABCDE";

	if (strcmp(buf, buf2) == 0) {
		cout << "같음";
	}

	if (!strcmp(buf, buf2)) {
		cout << "같음";
	}
	return 0;
}

* 
 
// 문자열 거꾸로 출력
strig str;
cin>>str;

for(int i=str.size() -1; i>=0; i--){
	cout <<str[i] <<" ";
}
* 

//string 기본 사용 방법

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

int main()
{
	//C++ 의 문자열 다루는 방법
	string str = "ABCDE";

	//printf("%s", str.c_str());
	cout << str << "\n";

	for (int i = 0; i < str.size(); i++) {
		cout << str[i] << " "; //char 배열처럼 사용 가능
	}

	return 0;
}

* 

#include <iostream>
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

int main()
{
	char vect[10] = "ABCDE";
	char vect2[10];

	APS (P.S)

	//문자열 P.S 문제
	//간단한것은 char 로 처리
	//복잡한것은 string class (C++) 을 쓰면
	//문자열을 쉽게 처리할 수 있음

	//P.S할때는 strlen, strcpy, strcat, strstr 적게쓰는 편
	//임베디드 개발할때는 c언어 string.h 로그관련 처리에서 무진장 씁니다.


	string class

	strlen
	strcpy
	strcat
	strstr

	return 0;
}

*

#include <iostream>
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

int main()
{
	char vect[10] = "ABCDE";
	char vect2[10];

	//strcpy(vect2, vect); < memcpy를 더 많이씀
	memcpy(vect2, vect, 10);

	cout << vect2;

	return 0;
}

* 
include <iostream>
#include <Windows.h>

#include <string.h>
using namespace std;

int main()
{
	//문자열 다룰때 기본내용 정리
	//3분 컷

	//1. 초기화 방법
	char vect[5] = { 'A', 'B', 'C', 'D', 'E' }; //널문자 추가 안됨 = 5개필요
	char vect2[6] = "ABCDE"; //널문자가 자동으로 추가됨 = 6개 필요

	int n = strlen(vect2);
	cout << n;

	//이게 안좋은 코딩인 이유? - O(N^2) 처럼 동작
	for (int i = 0; i < strlen(vect2); i++) {
		cout << vect2[i] << " ";
	}

	//이게 O(N)과 같이 동작
	int vn = strlen(vect2);
	for (int i = 0; i < vn; i++) {
		cout << vect2[i] << " ";
	}

	return 0;
}

*
#include <iostream>
using namespace std;

int main()
{
	//MessageBoxW(NULL, L"후후",L"키키",NULL);
	char ch;
	cin >> ch; //'B'

	if (ch >= 'A' && ch <= 'Z') {
		cout << (char)(ch - 'A' + 'a');
	}
	else if (ch >= 'a' && ch <= 'z') {
		for (char x = 'A'; x <= 'Z'; x++) {
			cout << x;
		}
		cout << "\n";
		for (char x = 'Z'; x >= 'A'; x--) {
			cout << x;
		}
	}

	return 0;
}
/*
-----------------------------------------0727
#include <iostream>
#include <vector> //str사용시

using namespace std;

int de;
int main(){
	int n = 5;
	vector<int> vect(5); //배열크기 초기선언

	for (int i = 0; i < 5; i++) {
		int a;
		cin >> a;
		vect.push_back(a); //배열의 크기가 초기선언되어있을때는 그후로 추가되며 전체크기가 커짐
	}

	vect.size(); //for문 사용시 O(1)

	int ret=vect.back(); //배열 확인후 제거
	vect.pop_back();
	ret=vect.back();
	vect.pop_back();
	
	//vector<int> vect(n);
	//for (int i = 0; i < 5; i++) {
	//	cin >> vect[i];
	//}
	

return 0;
}
--------------------------------------
int arr[5] = { 1,2,3,1,2 };

	cout << arr[1 + 1]; //a[x]위치에서 y만큼 떨어진

-------------------------------------시계출력
#include <iostream>

using namespace std;

int main() {
	for (int 시침 = 0; 시침 < 12; 시침++) {
		for (int 분침 = 0; 분침 < 60; 분침++) {
			cout << 시침 << "h" << 분침 << "m" << endl;
		}
	}
}
------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <vector>


using namespace std;

int de;

int main() {

	vector<int> vect;
	de = -1;
	vector<int> arr = { 5,3,7,1,9 };

	vect.push_back(5);
	vect.push_back(3);
	vect.push_back(7);
	vect.push_back(1);
	vect.push_back(9);

	sort(vect.begin(), vect.end());

	sort(arr.begin(), arr.end(), greater<int>());
	de = -1; //브레이크포인트 더미 잡아주기

	for (int i = 0; i < 5; i++) {
		cout << vect[i] << " ";
	}

	cout << "\n";

	for (int i = 0; i < 5; i++) {
		cout << arr[i] << " ";
	}

	return 0;
}
-------------------------------------------------------------


int arr[5] = { 4,2,1,3,7 };

//sort(arr, arr + 5, greater<int>()); 내림차순 정렬
sort(arr, arr + 5); //default 오름차순 정렬

for (int i = 0; i < 5; i++) {
	cout << arr[i] << " ";
}

cout << "\n";

sort(arr, arr + 5, greater<int>());

for (int i = 0; i < 5; i++) {
	cout << arr[i] << " ";
}

// de = -1; //디버깅확인할때사용

-----------------------------------------

#include <cstring> //#include <sting.h>와 같고 <string>과는 다름
#include <cmath>

char arr[10] = "asda";
string str = "asda;"

if (str == "asda") {
	cout << "같다";
}
if (strcmp(arr, "asda") == 0) { //strcmp

}

*/

 

- VS 단축키

break point : F9
debug(디버그모드 진입) : F5
step over(한줄씩 실행) : F10 -> 한줄단위로 실행
step intp(함수 들어가기) : F11
resume(다음 브레이크 포인트까지 실행) : F5
run to cursor(커서위치로 실행) : crtl + F10
terminate(디버그모드 종료) : shift  + F5

조사식 창 열기 : ctrl + alt + w
(잠시후 1누르기 -> 디버그 모드에서만 가능)


-------------------------------------------------------

알고리즘 성능측정

- 시간복잡도(빅-오 표기법)

- 시간 복잡도 함수 중에서 가장 큰 영향력을 주는 n에 대한 항만을 표시

- 계수 생략

 ->n개의 데이터를 입력 받아 저장한 후 각 데이터에 1씩 증가시킨 후 각 데이터를 화면에 출력하는 알고리즘의 시간복잡도는? = O(n)


- 암기
시간복잡도 nlongn -> 밑이  10이아닌 2;
기본적인 sort (오름, 내림)/ vect (배열)
빅O표기법 사용

- 시간복잡도 /공간복잡도?

'Algorithm > C++' 카테고리의 다른 글

[21.08.03] dat활용 counting sort와 vector사용법  (0) 2021.08.03
[21.08.02] 재귀함수2/ 백트래킹  (0) 2021.08.02
[21.07.30] 재귀함수, 트리, DAT  (0) 2021.08.01
[21.07.29]스택, 큐  (0) 2021.08.01
[21.07.28] 문자열 파싱하기  (0) 2021.08.01

+ Recent posts