1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <iostream> #include <conio.h> #define LEFT 75 #define RIGHT 77 #define UP 72 #define DOWN 80 #define ESC 27 using namespace std; void main(){ int *arr,input,temp, zero_location, key[2]; cout<<"입력:"; cin>>input; arr=(int*)malloc((input+1)*4); cout<<"변경전: "; for(int j=0;j<input;j++) { arr[j] = j; cout<<arr[j]<<" "; } for (int j=0; j<input; j++){ int rnd1 = rand() % input; int rnd2 = rand() % input; temp = arr[rnd1]; arr[rnd1] = arr[rnd2]; arr[rnd2] = temp; } cout<<"\n변경후: "; for(int j=0;j<input;j++) cout<<arr[j]<<" "; cout<<"\n"; for (int j=0; j<input; j++){ if (arr[j] == 0) { zero_location = j; } } cout<<"\n"; while(1){ key[0]=getch(); key[1]=getch(); switch(key[1]) { case LEFT: { if (zero_location == 0) { temp = arr[zero_location]; arr[zero_location] = arr[(input-1)]; arr[(input-1)] = temp; for(int j=0;j<input;j++) cout<<arr[j]<<" "; zero_location=(input-1); break; } temp = arr[zero_location]; arr[zero_location] = arr[zero_location-1]; arr[zero_location-1] = temp; for(int j=0;j<input;j++) cout<<arr[j]<<" "; zero_location--; break; } case RIGHT: { if (zero_location-input == -1) { temp = arr[0]; arr[0] = arr[(input-1)]; arr[(input-1)] = temp; zero_location=0; for(int j=0;j<input;j++) cout<<arr[j]<<" "; break; } temp = arr[zero_location]; arr[zero_location] = arr[zero_location+1]; arr[zero_location+1] = temp; for(int j=0;j<input;j++) cout<<arr[j]<<" "; zero_location++; break; } case 27: { free(arr); exit(1); } } cout<<"\n"; } } | cs |
'C++ > 알고리즘' 카테고리의 다른 글
2017 카카오 신입 공채 1차 코딩 테스트 1번 문제 비밀 지도 - C++ (0) | 2019.12.16 |
---|---|
for문을 사용하여 1!,2!,3!,4! .... 10!을 구하라 (0) | 2019.01.04 |
c++ malloc 동적배열 난수 발생 후 섞기 (0) | 2016.04.07 |
c++ 알고리즘 소수 출력 (0) | 2016.04.06 |
c++ 포인터 call by reference 콜 바이 레퍼런스 (0) | 2016.04.06 |