From 4bc82188482e63ec52acb71985039d3d33378a7a Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Wed, 28 Mar 2018 16:50:18 +0700 Subject: Revise 09/TP-HN-2014 --- 09/TP-HN-2014/README.md | 2 +- 09/TP-HN-2014/cau1.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 09/TP-HN-2014/cau2.cpp | 32 +++++++++++++++++++++++++++++++ 09/TP-HN-2014/cau3.c | 40 ++++++++++++++++++++++++++++++++++++++ 09/TP-HN-2014/cau4.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 09/TP-HN-2014/cau1.cpp create mode 100644 09/TP-HN-2014/cau2.cpp create mode 100644 09/TP-HN-2014/cau3.c create mode 100644 09/TP-HN-2014/cau4.c (limited to '09') diff --git a/09/TP-HN-2014/README.md b/09/TP-HN-2014/README.md index 8a21e84..5d15789 100644 --- a/09/TP-HN-2014/README.md +++ b/09/TP-HN-2014/README.md @@ -50,7 +50,7 @@ Hai số x và y trên cùng một dòng, cách nhau một dấu cách. ## Câu 2: Đua Robot -Trong cuộc đua tốc độc có n Robot tham gia được đánh số từ 1 đến n. Đường đua +Trong cuộc đua tốc độ có n Robot tham gia được đánh số từ 1 đến n. Đường đua có độ dài d (mét). Robot thứ i (1 ≤ i ≤ n) có vận tốc đua không đổi là vi (mét/phút). Các Robot xuất phát theo thứ tự từ 1 đến n và cách nhau 1 phút. Robot i gọi là vượt Robot j (1 ≤ j ≤ n) nếu i xuất phát sau j và diff --git a/09/TP-HN-2014/cau1.cpp b/09/TP-HN-2014/cau1.cpp new file mode 100644 index 0000000..274cef6 --- /dev/null +++ b/09/TP-HN-2014/cau1.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +long +gcd(long x, long y) +{ + long z; + while (y) + { + z = x; + x = y; + y = z % x; + } + + return x; +} + +int +main() +{ + ifstream infile; + long a, b, c, d; + infile.open("CAU1.INP"); + infile >> a >> b >> c >> d; + infile.close(); + + long y = b * d / gcd(b, d); + long x = a * y / b - c * y / d; + if (!x) + y = 1; + else + { + a = gcd(x, y); + x /= a; + y /= a; + if (y < 0) + { + x *= -1; + y *= -1; + } + } + + ofstream outfile; + outfile.open("CAU1.OUT"); + outfile << x << ' ' << y << endl; + outfile.close(); + + return 0; +} diff --git a/09/TP-HN-2014/cau2.cpp b/09/TP-HN-2014/cau2.cpp new file mode 100644 index 0000000..8c87168 --- /dev/null +++ b/09/TP-HN-2014/cau2.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +using namespace std; + +int +main() +{ + ifstream infile; + infile.open("CAU2.INP"); + short n, i, j; + long d, count = 0; + infile >> n >> d; + short* v = (short*) malloc(n * sizeof(short)); + for (i = 0; i < n; i++) + { + infile >> v[i]; + for (j = 0; j < i; j++) + if (d * (v[i] - v[j]) > v[i] * v[j] * (i - j)) + count++; + } + infile.close(); + free(v); + + ofstream outfile; + outfile.open("CAU2.OUT"); + outfile << count << endl; + outfile.close(); + + return 0; +} diff --git a/09/TP-HN-2014/cau3.c b/09/TP-HN-2014/cau3.c new file mode 100644 index 0000000..cbb2ce4 --- /dev/null +++ b/09/TP-HN-2014/cau3.c @@ -0,0 +1,40 @@ +#include +#include + +char ispalin(char *s, unsigned char len) +{ + unsigned char i, j; + for (i = 0; i < (len + 1) / 2; i++) + if (s[i] != s[len - i - 1]) + return 0; + return 1; +} + +unsigned char maxpalin(char s[256]) +{ + unsigned char i, j, k, len = strlen(s); + for (i = len - 1; i; i--) + for (j = 0; j + i <= len; j++) + if (ispalin(s + j, i)) + return i; +} + +int main() +{ + FILE *f = fopen("CAU3.INP", "r"); + char s[256]; + fscanf(f, "%s\n", s); + fclose(f); + + unsigned char i, count[128] = {}; + for (i = 0; i < strlen(s); count[s[i++]]++); + for (i = 1; i < 128; i++) + if (count[i]) + (*count)++; + + f = fopen("CAU3.OUT", "w"); + fprintf(f, "%hhd\n%hhd\n", *count, maxpalin(s)); + fclose(f); + + return 0; +} diff --git a/09/TP-HN-2014/cau4.c b/09/TP-HN-2014/cau4.c new file mode 100644 index 0000000..49e3df6 --- /dev/null +++ b/09/TP-HN-2014/cau4.c @@ -0,0 +1,48 @@ +#include +#include + +int cmp(const void *x, const void *y) +{ + return *(int *) x - *(int *) y; +} + +int main() +{ + FILE *f = fopen("CAU4.INP", "r"); + int n, d, k; + fscanf(f, "%d %d", &n, &d); + int *a = (int *) malloc(n * sizeof(int)); + for (k = 0; k < n; k++) + fscanf(f, "%d", a + k); + k = a[--d]; + fclose(f); + + qsort(a, n, sizeof(int), cmp); + int t = 0; + for (int i = 1; i < n; i++) + t += abs(a[i] - a[i - 1]); + int idx = (int *) bsearch(&k, a, n, sizeof(int), cmp) - a; + if ((idx - d) * (idx - n + d + 1)) { + int t0, t1; + if (idx < d) { + t0 = (t - abs(a[n - d + idx] - a[n - d + idx - 1]) + + abs(a[n - d + idx] - *a)); + d = n - d - 1; + t1 = (t - abs(a[idx - d] - a[idx - d - 1]) + + abs(a[n - 1] - a[idx - d - 1])); + } else { + t0 = (t - abs(a[idx - d] - a[idx - d - 1]) + + abs(a[n - 1] - a[idx - d - 1])); + d = n - d - 1; + t1 = (t - abs(a[n - d + idx] - a[n - d + idx - 1]) + + abs(a[n - d + idx] - *a)); + } + t = (t0 < t1) ? t0 : t1; + } + + f = fopen("CAU4.OUT", "w"); + fprintf(f, "%d\n", t); + fclose(f); + + return 0; +} -- cgit 1.4.1