본문 바로가기

기타[etc]/알고리즘

(26)
[알고리즘] DP-숫자 삼각형 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 #include#include#include#include using namespace std; int dp [1001][1001];int tr [1001][1001]; /* a == 0과 마지막줄은 max없음 dp[n] = board[n][a] + max(dp[n-1][a], dp[n-1][a-1])*/ int _max(int a, int b){ return a > b ? a : b;} int main(){ int n; cin >> n; for(int i =1 ; i tr[i][a]; }..
[알고리즘] 124나라의 숫자 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263#include#include#include#includeusing namespace std; /*8 / 3 = 2 ... 22 / 3 = 0 ... 29 / 3 = 3 ... 03 / 3 = 1 ... 010 /3 = 3 ... 13 / 3 = 1 ... 0 2 111 / 3= 3 ... 22 / 3 = 0 ... 23* 3 + 1* 312 / 3 =3 .. 0 33 / 3 =1 .. 0 313 / 3 = 4 ... 14 / 3 = 1 ... 11 / 3 = 0 ... 1*/string chang..
[알고리즘] 진법 변환 123456789101112131415161718192021222324252627282930313233#include#include#include#include using namespace std; int main(){ string n; long long m; long long ans = 0; int cnt = 0; cin >> n ; cin >> m ; for(int i = n.length()-1 ; i != -1 ; i--) { int tmp = 0; if(n[i] - '0'
[알고리즘] 소수의 개수 찾기 - 에라토스테네스의 채 n부터 m까지 소수의 갯수를 구하는데 빠르게 구하는 방법2부터 각 범위까지 배수를 지워 가는 형식 123456789101112131415161718192021222324252627282930313233343536373839404142#include#include#include using namespace std;bool pn[1000001] = {0}; int main(){ int n, m ; std::fill_n(pn, 1000001, true); cin >> n >> m; if(n 1000000) return -1; if (m 1000000) return -1; for(int i = 2; i*i
[알고리즘] DP- 땅따먹기 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172#include#includeusing namespace std; vector dp(10001, vector(4,0)); int hopscotch(vector board){ // 함수를 완성하세요. // if (x != z) // d[n][x] = max(d[n-1][z]) + board[n][z] // 점화식[?] int answer = 0; int col = 4; int row = board.size(); int tmp = 0; for(int i = 0 ; i
[알고리즘] DP-가장 큰 정사각형 찾기 문제 해결 전략DP 파악DP는 작은 문제로 큰 문제 해결작은 문제의 답을 memory에 적재12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667#include#include#include#includeusing namespace std; //x,y에서의 최대 변의 길이를 저장하는 변수vector dp(10001, vector(10001,0)); int _min(int a, int b, int c){ a = a
[백준] 동적기획법 - 피보나치 0과 1 세기 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455#include #include #include using namespace std; int a,b;int fibonacci(int n, int m); int mem[10001][4] = {0}; int main(void){ mem[0][0] = 1; mem[1][0] = 0; int n,c; cin >> n; while(n--){ cin >> c ; if( 0 > c || c > 40){ return 1; } cout
[알고리즘] 숫자의 표현 123456789101112131415161718192021222324252627282930313233343536373839/*수학을 공부하던 민지는 재미있는 사실을 발견하였습니다.그 사실은 바로 연속된 자연수의 합으로 어떤 숫자를 표현하는 방법이 여러 가지라는 것입니다.예를 들어, 15를 표현하는 방법은(1+2+3+4+5)(4+5+6)(7+8)(15)로 총 4가지가 존재합니다. 숫자를 입력받아 연속된 수로 표현하는 방법을 반환하는 expressions 함수를 만들어 민지를 도와주세요.예를 들어 15가 입력된다면 4를 반환해 주면 됩니다.*/ #includeusing namespace std;int expressions(int testCase){ int answer = 0; for (int i = 1 ;..