-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bai47_DichPhai.cpp
60 lines (44 loc) · 930 Bytes
/
Bai47_DichPhai.cpp
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
#include <iostream>
#include<fstream>
using namespace std;
void readFile(const char* fileName, size_t& n, int* arr, size_t& d) {
fstream ifs(fileName);
if (ifs.fail()) {
cout << "Loi";
}
ifs >> n;
for (int i = 0; i < n; i++) {
ifs >> arr[i];
}
ifs >> d;
ifs.close();
}
void ouputFile(const char* fileName, int n, int* arr) {
ofstream ofs(fileName);
for (int i = 0; i < n; i++) {
ofs << arr[i] << " ";
}
ofs.close();
}
void dichPhai(int n, int* arr, int d) {
int i = n - 1;
int temp = arr[n - 1];
while (1) {
int k = i - d;
if (k < 0)
k += n;
if (k == n - 1)
break;
arr[i] = arr[k];
i = k;
}
arr[i] = temp;
}
int main()
{
size_t n, d;
int* arr = new int[100];
readFile("C:\\Users\\ASUS\\Desktop\\CodePTIT\\41..50\\Bai47_DichPhai\\Data.txt", n, arr, d);
dichPhai(n, arr, d);
ouputFile("C:\\Users\\ASUS\\Desktop\\CodePTIT\\41..50\\Bai47_DichPhai\\Result.txt", n, arr);
}