14 -> 18

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
2.5 kB
4
Indexable
Never
#include <stdio.h>
#include <math.h>

void readFile(int a[], int &n, int &m);
void bai14(int a[], int n);
void bai15(int a[], int n);
void bai16(int a[], int n, int m);
void bai17(int a[], int n);
void bai18(int a[], int n);

int main() {
	int a[1000], n, m;
	
	readFile(a, n, m);
	
	bai14(a, n);
	bai15(a, n);
	bai16(a, n, m);
	bai17(a, n);
	bai18(a, n);
		
	return 0;
}

void readFile(int a[], int &n, int &m) {
    FILE *f;
    f = fopen("num.inp", "rt");
    fscanf(f, "%d", &n);
    fscanf(f, "%d", &m);
    for(int i = 0; i < n; i++)
    	fscanf(f, "%d", &a[i]);
    fclose(f);
}

void bai14(int a[], int n) {
	int s = 0;
	for (int i = 0; i < n; i++)
		s += a[i];
	float avg = s * 1.0 / n;
	FILE *f;
	f = fopen("num.out", "wt");
	fprintf(f, "%.1f\n", avg);
	fclose(f);
}

int nguyenTo(int x) {
	if (x < 2)
		return 0;
	if (x == 2)
		return 1;
	for (int i = 2; i <= sqrt(x); i++)
		if (x % i == 0)
			return 0;
	return 1;
}

int chinhPhuong(int x) {
	int a = (int) sqrt(x);
	return a * a == x;
}

int hoanChinh(int x) {
	int s = 0;
    for(int i = 1; i <= x/2; i++){
        if(x % i == 0) 
            s += i;
    }
    return s == x;
}

void bai15(int a[], int n) {
	int ngTo = 0, chPhuong = 0, hChinh = 0;
	for (int i = 0; i < n; i++) {
		if (nguyenTo(a[i]))
			ngTo++;
		if (chinhPhuong(a[i]))
			chPhuong++;
		if (hoanChinh(a[i]))
			hChinh++;
	}
	FILE *f;
	f = fopen("num.out", "a");
	fprintf(f, "%d %d %d\n", ngTo, chPhuong, hChinh);
	fclose(f);
}

void bai16(int a[], int n, int m) {
	int count = 0;
	
	for(int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++)
			if (a[i] + a[j] == m)
				count++;
	}
	FILE *f;
	f = fopen("num.out", "a");
	fprintf(f, "%d\n", count);
	fclose(f);
}

void bai17(int a[], int n) {
	int count = 0, max = 0;
	for(int i = 0; i < n - 1; i++) {
		if (a[i] <= a[i + 1]) {
			count++;
		} else {
			if (count > max) {
				max = count;
				count = 1;
			}
		}
	}
	if (count > max) {
		max = count;
	}
	FILE *f;
	f = fopen("num.out", "a");
	fprintf(f, "%d\n", max);
	fclose(f);
}

void bai18(int a[], int n) {
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			if(a[i] > a[j]) {
				int t = a[i];
				a[i] = a[j];
				a[j] = t;
			}
		}
	}
	FILE *f;
	f = fopen("num.out", "a");
	for (int i = 0; i < n; i++)
		fprintf(f, "%d ", a[i]);
	fclose(f);
}