본문 바로가기

Programming

랜덤 함수를 이용한 함수, 배열을 출력하는 함수 // 랜덤 함수를 이용하여 int 배열을 0~max-1 의 값으로 무작위로 채우는 함수static void initRandom(int list[], int n, int max = 100) {for (int i = 0; i < n; i++)list[i] = rand() % max;} // 배열을 화면에 보기 좋게 출력하는 함수. 디폴트 매개변수 사용static void printArray(int arr[], int n, const char *str = "Array") {printf("%s = ", str);for (int i = 0; i < n; i++)printf("%3d", arr[i]);printf("\n");} 더보기
선택정렬 알고리즘을 이용해 int 배열을 오름차순으로 정렬하는 함수 //두 정수를 교환하는 함수 : inline 함수. 매개변수로 레퍼런스형 사용.inline void swap(int& x, int& y) {int t = x;x = y;y = t;} //선택정렬 알고리즘을 이용해 int 배열을 오름차순으로 정렬하는 함수void selectionSort(int A[], int n) {for (int i = 0; i < n - 1; i++) {// n-1번만 반복int least = i;for (int j = i + 1; j < n; j++)//최솟값 탐색if (A[j] < A[least]) least = j;swap(A[i], A[least]);}} 더보기
WGraphFloyd.h : Floyd 알고리즘의 최단 경로 탐색 기능이 추가된 그래프 #pragma once#ifndef ___WGraphFloyd#define ___WGraphFloyd//WGraphFloyd.h : Floyd 알고리즘의 최단 경로 탐색 기능이 추가된 그래프#include "WGraph.h"class WGraphFloyd : public WGraph{int A[MAX_VTXS][MAX_VTXS];// 최단 경로 거리public:void ShortestPathFloyd() {for (int i = 0; i < size; i++)for (int j = 0; j < size; j++)A[i][j] = getEdge(i, j); for (int k = 0; k < size; k++) {for (int i = 0; i < size; i++)for (int j = 0; j < si.. 더보기
[C++] 백준 1057번 : 토너먼트 https://www.acmicpc.net/problem/1057 #include #include using namespace std; int main() { int n;int m1, m2;cin >> n >> m1 >> m2; n--;m1--;m2--; int tmp = 0;while (1) {if (n >= 2){n /= 2;tmp++;}elsebreak;} int round = tmp + 1;int result = 0, flag = 0;while (flag != 1) {(m1 / (int)pow(2, tmp)) == (m2 / (int)pow(2, tmp)) ? result++ : flag = 1;tmp--;}round -= result; cout 더보기
WGraphDijkstra.h : Dijkstra알고리즘의 최단 경로 탐색 기능이 추가된 그래프 #pragma once#ifndef ___WGraphDijkstra#define ___WGraphDijkstra//WGraphDijkstra.h : Dijkstra알고리즘의 최단 경로 탐색 기능이 추가된 그래프#include "WGraph.h" class WGraphDijkstra : public WGraph {int dist[MAX_VTXS];// 시작노드로부터의 최단경로 거리bool found[MAX_VTXS];// 방문한 정점 표시public://방문하지 않은 정점들 중에서 최단경로 거리가 가장 작은 정점을 찾아 반환int chooseVertex() {int min = INF;int minpos = -1;for(int i=0;i 더보기
윤년 답안 - 함수로 분할 #include int isLeap(int year);//윤년인지 구분해주는 함수int yearDay(int year, int month, int day);//그 해의 몇번째 날인지 계산하는 함수int totalDay(int year, int month, int day);//1900년 부터 총 날짜를 계산하는 함수int Week(int total);//요일을 반환하는 함수int main() {int year, month, day;//년 월 일을 입력받을 함수char week[7][5] = { {"일"}, {"월"}, {"화"}, {"수"}, {"목"}, {"금"}, {"토"} };//요일을 초기화char leap[2][3] = { {"평"}, {"윤"}};int total = 0;//년 월 일 입력pri.. 더보기
[C++] 백준 1076번 : 저항 https://www.acmicpc.net/problem/1076 #include #include #include using namespace std; long long detector(string a); int main() {string str1;string str2;string str3;cin >> str1;cin >> str2;cin >> str3;if (detector(str1) != -1 && detector(str2) != -1 && detector(str3) != -1){long long n = (detector(str1) * 10 + detector(str2))*pow(10, detector(str3));cout 더보기
[C++] 백준 1037번 : 약수 https://www.acmicpc.net/problem/1037 #include #include #include using namespace std; int main() {int tc,tmp;vector n;cin >> tc; for (int i = 0; i > tmp;n.push_back(tmp);}sort(n.begin(), n.end());cout 더보기