Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

// duyệt tất cả các phần tử có trong mảng rồi so sánh khoảng cách giữa tụi nó với m, tìm cái

khoảng
cách // nhỏ nhất

#include<stdio.h>
#include<math.h>

const int N = 10000;


const int inf = 1'000'000'000;

int main() {
int n, m, idx = 0;
double ans;
double a[N];

scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lf", &a[i]);

int mi = inf;
for (int i = 0; i < n; i++) {
if (abs(a[i] - m) < mi) {
mi = abs(a[i] - m);
ans = a[i];
idx = i;
}
}

printf("%.2lf at index %d nearest to %d", ans, idx, m);


return 0;
}

// test 1 sai, đề thì ghi lung tung


#include<stdio.h>
#include<ctype.h>
#include<string.h>
const int N = 1000;

int searchChar(char s[N], char c) {


int res = -1;
for (int i = 0; i < strlen(s); i++)
if (s[i] == c) {
res = i;
break;
}
return res;
}

int main() {
char s[100];
char c;
scanf("%s", s);
getchar();
scanf("%c", &c);

for (int i = 0; i < strlen(s); i++)


s[i] = tolower(s[i]);
c = tolower(c);

int tmp = searchChar(s, c);


if (tmp != -1)
printf("Found %c at %d", c, tmp);
else printf("Not found!");
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

const int N = 500;

int search(int n, char s[N][N], char t[N]) {


int res = -1;
for (int i = 0; i < n; i++)
if (strcmp(s[i], t) == 0) {
res = i;
break;
}
return res;
}

int main() {
int n, k;
char s[N][N], t[N];
scanf("%d%d", &n, &k);

for (int i = 0; i < n; i++)


scanf("%s", s[i]);

scanf("%s", t);
int tmp = search(n, s, t);
if (tmp != -1)
printf("Found %s at %d", t, tmp);
else printf("Not found!");

// nay gio quen dung gets, puts, tu nhien lai dung scanf :v
#include<stdio.h>
#include<string.h>

const int N = 1005;


void insertChar(int k, char s[N], char c) {
int len = strlen(s);
if (k >= 0 && k <= len) {
for (int i = len; i >= k; i--)
s[i + 1] = s[i];
s[k] = c;
}
}

int main() {
char s[N], c;
int idx;
gets(s);

scanf("%c", &c);
scanf("%d", &idx);

insertChar(idx, s, c);

puts(s);
}
#include<stdio.h>
#include<string.h>

const int N = 1000;

void insertString(int *n, int m, char s[][N], int k, char t[]) {


if (k >= 0 && k <= *n) {
for (int i = *n - 1; i >= k; i--)
for (int j = 0; j < m; j++) {
s[i + 1][j] = s[i][j];
}
for (int i = 0; i < m; i++)
s[k][i] = t[i];
(*n)++;
}
}

int main() {
int n, m, idx;
char s[N][N], t[N];

scanf("%d%d", &n, &m);


for (int i = 0; i < n; i++)
scanf("%s", s[i]);
getchar();
scanf("%d", &idx);
scanf("%s", t);

insertString(&n, m, s, idx, t);

for (int i = 0; i < n; i++)


printf("%s ", s[i]);

return 0;

#include<stdio.h>

const int maxN = 1000;


void insert(int *n, int m, double a[], double b[], int c[]) {
int j = *n - 1, tmp = m;
for (int i = m - 1; i >= 0; i--) {
while (j >= c[i]) {
a[j + tmp] = a[j];
j--;
}
--tmp;
}
tmp = m - 1;
for (int i = m - 1; i >= 0; i--) {
a[c[i] + tmp]= b[i];
--tmp;
}
*n = (*n) + m;
}

int main() {
int n;
double a[maxN], b[maxN];
int c[maxN];
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lf", a + i);
int m;
scanf("%d", &m);
for (int i = 0; i < m; i++)
scanf("%lf%d", &b[i], &c[i]);
insert(&n, m, a, b, c);
for (int i = 0; i < n; i++)
printf("%.2lf ", a[i]);

return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

const int maxN = 1000;

void Solve(int *n, int m, char s[][12], char t[][12]) {


for (int i = *n - 1; i >= 0; i--) {
int tmp = (s[i][0] -'0') * 10 + s[i][1] - '0';
if (tmp == i) continue;
for (int j = 0; j < 10; j++)
s[tmp][j] = s[i][j];
}
for (int i = 0; i < m; i++) {
int tmp = (t[i][0] -'0') * 10 + t[i][1] - '0';
for (int j = 0; j < 10; j++)
s[tmp][j] = t[i][j];
}
*n = *n + m;
}

int main() {
int n, m;
char s[maxN][12], t[maxN][12];
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%s", s[i]);
scanf("%d", &m);
for (int i = 0; i < m; i++)
scanf("%s", t[i]);
Solve(&n, m, s, t);
printf("\n");
for (int i = 0; i < n; i++)
printf("%s\n", s[i]);

return 0;
}
#include<stdio.h>
#include<string.h>

const int maxN = 1005;

void deleteChar(int pos, char s[]) {


int n = strlen(s);
for (int i = pos; i < n - 1; i++)
s[i] = s[i + 1];
s[n - 1] = '\n';
}

int main() {
char s[maxN];
scanf("%s", s);

int pos;
scanf("%d", &pos);

deleteChar(pos, s);
printf("%s", s);
}
#include<stdio.h>

const int maxN = 1004;


int pos[maxN];
int cnt = 0;
void find(int n, double x, double y, double a[], double b[]) {
for (int i = 0; i < n; i++)
if (a[i] == x && b[i] == y)
pos[cnt++] = i;
}

void Delete(int *n, int k, double a[], double b[]) {


for (int i = k; i < *n - 1; i++) {
a[i + 1] = a[i];
b[i + 1] = b[i];
}
--*n;
}

int main() {
int n;
double a[maxN], b[maxN];
double x, y;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lf%lf", a + i, b + i);
scanf("%lf%lf", &x, &y);

find(n, x, y, a, b);
if (!cnt)
printf("Can not delete %lf %lf from this array!", x, y);
else {
for (int i = 0; i <= cnt; i++)
Delete(&n, pos[i], a, b);
for (int i = 0; i < n; i++)
printf("%lf %lf\n", a[i], b[i]);
printf("Delete %lf %lf at ", x, y);
for (int i = 0; i < cnt - 1; i++)
printf("%d, ", pos[i]);
printf("%d", pos[cnt - 1]);
}
}
// lazy boy :v

You might also like