1
0
Fork 0

Revise 09/TP-HN-2014

This commit is contained in:
Nguyễn Gia Phong 2018-03-28 16:50:18 +07:00
parent bc836411b0
commit 4bc8218848
5 changed files with 172 additions and 1 deletions

View File

@ -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à
v<sub>i</sub> (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à

51
09/TP-HN-2014/cau1.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <iostream>
#include <fstream>
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;
}

32
09/TP-HN-2014/cau2.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <stdlib.h>
#include <iostream>
#include <fstream>
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;
}

40
09/TP-HN-2014/cau3.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <string.h>
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;
}

48
09/TP-HN-2014/cau4.c Normal file
View File

@ -0,0 +1,48 @@
#include <stdlib.h>
#include <stdio.h>
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;
}