목록Algorithm (151)
자이의 프로그래밍
입력을 받은 뒤 정렬하여 제일 앞에 작은 블록입력을 받은 뒤 정렬하여 제일 앞에 적은 상자의 갯수, 제일 뒤에 많은 상자의 갯수가 오게 하였다. 주어진 횟수만큼 큰 블록을 빼고 작은 블록을 더한 뒤 정렬하는 과정을 반복했고, 그 과정중에 정렬이 완료되었으면 반복문을 빠져나갔다. #include #include #include using namespace std; int main(){ vector v; for(int T=1; T>n; for(int i=0; i>a; v.push_back(a); } sort(v.begin(), v.end()); for(int i=0; iv[0]){ v[99]--; v[0]++; } else break; sort(v.begin(), v.end()); } cout
100*100의 배열을 입력받고 한 행, 한 열씩 계속 더해가면서 최댓값을 찾았다. 대각선의 경우엔 예외로 따로 처리해주었다. #include #include #include #include #include using namespace std; int arr[110][110]; int main(){ for(int T=1; T>tc; for(int i=0; iarr[i][j]; } } int maximum=-2147000000; for(int i=0; imaximum)maximum=colsum; } int sum=0; for(int i=0; imaximum)maximum=sum; sum=0; for(int i=0; imaximum)maximum=sum; cout
찾아야 할 문자열과 탐색해야 할 문자열을 입력받고 tmp라는 문자형 배열을 선언하여 해당 배열에 탐색해야 할 문자열을 순차적으로 대입해주었다. 이후 strcmp라는 함수를 이용하여 같다면 cnt를 하나씩 더해 최종 갯수를 구해주었다. #include #include #include #include #include using namespace std; char finding[1010]; char seek[1010]; int main(){ for(int T=1; T>tc; cin>>finding>>seek; int cnt=0; for(int i=0; i
a값을 입력받은 뒤 a값이 0이 아니면 b값도 입력받아야 하므로 b를 입력받았다. a가 0이면 현재의 속도를 유지하므로 최종 달린 길이에 현재의 속도만큼을 더해주었다. (1초이므로) a가 1이면 입력받은 속도만큼 가속한뒤 더해주었다. a가 2이면 현재 속도보다 감속해야 할 속도가 더 큰 경우 0으로 속도를 맞춰주었고 그렇지 않은 경우에는 현재 속도를 입력받은 속도만큼 감속해 더해주었다. #include #include #include #include #include using namespace std; int main(){ int test_case; cin>>test_case; // for(int T=1; T>n; int nowspeed=0; int len=0; for(int j=0; j>a; if(a!..
2로 나눠지는 한 2로 계속 나누고 3으로 나눠지는 한 3으로 계속 나누는 과정을 반복한 뒤에 각각을 출력했다. #include using namespace std; int main(int argc, char** argv) { int test_case; int T; cin>>T; for(test_case = 1; test_case >a; while(a%2==0){ a=a/2; twonum++; } while(a%3==0){ a=a/3; threenum++; } while(a%5==0){ a=a/5; fivenum++; } while(a%7==0){ a=a/7; sevennum++; } while(a%11==0){ a=a/11; elevennum++; } cout
cnt라는 변수를 사용해서 10번씩 카운팅해주었다. num이라는 변수는 해당 문자가 출력되어야할 횟수이다. num만큼 출력하면서 cnt를 더하며 cnt가 10이 된다면 줄 바꿈을 해주었다. cnt를 초기화하지 않아 다음 문자열에서도 값을 유지한채 계속 사용하게끔 했다. #include #include #include #include #include using namespace std; int main(){ int test_case; cin>>test_case; for(int T=1; T>n; vector v; v.clear(); for(int i=0; i>ch>>num; v.push_back(make_pair(ch, num)); } int cnt=0; cout
매 달의 날짜수를 day라는 배열에 저장해주었다. 입력받는 두 개의 달이 다르다면 day 배열에서 먼저 있는 달을 빼주면서 날짜를 계산했고 먼저 있는 달을 하나씩 더하면서 두 개의 달이 같은 달이 될때까지 반복했다. 같은 달이 된다면 두 개의 값을 빼서 최종 sum이라는 값에 더해주었다. #include using namespace std; int day[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main(){ int test_case; cin>>test_case; for(int T=1; T>m1>>d1>>m2>>d2; int sum=0; while(m1
BFS로 해결했다.. 사실 그럴 필요는 없었을 것 같은데 풀 당시에 떠오르는 생각이 그것밖에 없었다. 네 방향을 지정해준뒤 진행을 하다가 범위를 벗어나거나, 이미 달팽이가 방문한 칸을 만난다면 방향을 전환해주었다. 방향을 전환한 뒤에 만난 점을 아직 달팽이가 만나지 않았다면 체크배열에 검사해주고 큐에 push 해주었다. #include #include #include using namespace std; int ch[11][11]; int dx[]={0, 1, 0, -1}; int dy[]={1, 0, -1, 0}; int main(){ int test_case; cin>>test_case; for(int T=1; T>n; memset(ch, 0, sizeof(ch)); int dir=0; queue q..