본문 바로가기
반응형

알고리즘8

SW Expert Academy(2058. 자릿수 더하기) C++ #include using namespace std; int main(int argc, char** argv) { int N; int sum = 0; int value = 1000; cin>>N; for( ; value > 0 ;) { sum += N / value; N %= value; value /= 10; } printf("%d", sum ); return 0;//정상종료시 반드시 0을 리턴해야합니다. } 1. 자리수가 최대 4자리 수라 value를 1000으로 고정했다. 2021. 8. 10.
C++ iomanip 헤더, 소수점 자리 출력 fixed #include 를 포함시키면 setprecision(자리수)를 사용할 수 있다. 또한 fixed를 사용하여 원하고자 하는 자리수 까지 출력할 수 있다. #include #include using namespace std; int main(){ double a = 1.8888888888; //소수점 3번째 자리에서 반올림 cout 2021. 7. 2.
Programmers 체육복문제 (Greedy) 코딩테스트 연습으로 푼 문제인데 처음이라그런지 푸는데 굉장히 오래걸렸다. 처음 시도로는 몇몇 테스트 케이스가 계속 오류가 떠서 결국 검색해보았는데 다른사람들의 풀이 방식만을 보고 혼자 코딩해보아서 좀 지져분하다. #include #include using namespace std; int solution(int n, vector lost, vector reserve) { int answer = 0; //학생들의 체육복 유무를 확인하기 위한 배열 생성 기본값은 0 // -1 : 체육복이 없음, 0 : 체육복 있음, 1 : 체육복 2벌 vector check(n); //체육복을 가지고 있지 않으면 -1로 세팅 for(int i = 0; i < lost.size(); i++){ check[lost[i]-1]-.. 2020. 9. 24.
QuickSort(퀵정렬), C, C++ Pivot을 기준으로 Pivot보다 작은 수는 왼쪽에, Pivot보다 큰 수는 오른쪽에 배치하여 정렬하는 기법이다. 이름처럼 매우 빠른 속도로 정렬한다. #include #include #define max 10 void QuickSort(int*, int, int); int Partition(int*, int, int); void QuickSort(int* a, int start, int end){ int index; if(start < end){ index = Partition(a, start, end); QuickSort(a, start, index-1); QuickSort(a, index+1, end); } else{ return ; } } int Partition(int* a, int start.. 2020. 1. 14.
반응형