Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <math.h>
- #define MAX 10
- #define loop(x, a, b) for (int x = a; x < b; ++x)
- bool isPrime(int n)
- {
- if (n == 0 || n == 1)
- return false;
- for (int i = 2; i <= sqrt(n); ++i)
- if (n % i == 0)
- return false;
- return true;
- }
- int main()
- {
- int arr[MAX];
- int N;
- do
- {
- scanf("%d", &N);
- } while (N < 3 || N > 10);
- loop(i, 0, N)
- scanf("%d", &arr[i]);
- // Câu 2:
- int even = 0;
- loop(i, 0, N) if (arr[i] > 0 && !(arr[i] & 1))++ even;
- printf("%d\n", even);
- // Câu 3:
- int first_prime = -1;
- loop(i, 0, N)
- {
- if (isPrime(arr[i]))
- {
- first_prime = arr[i];
- break;
- }
- }
- if (first_prime != -1)
- printf("%d\n", first_prime);
- // Câu 4:
- int sum = 0;
- loop(i, 0, N)
- sum += (i + 1) * arr[i];
- printf("%d\n", sum);
- // Câu 5:
- bool found;
- int index = 0;
- int check[MAX];
- loop(i, 0, N)
- {
- found = false;
- loop(j, 0, index)
- {
- if (arr[i] == check[j])
- {
- found = true;
- break;
- }
- }
- if (!found)
- {
- check[index] = arr[i];
- ++index;
- }
- }
- loop(i, 0, index)
- printf("%d ", check[i]);
- printf("\n");
- // Câu 6:
- sum = abs(arr[0] + arr[1]);
- int first = 0;
- int second = 1;
- loop(i, 0, N)
- {
- loop(j, i + 1, N)
- {
- if (abs(arr[i] + arr[j]) < sum)
- {
- sum = abs(arr[i] + arr[j]);
- first = i;
- second = j;
- }
- }
- }
- printf("%d %d\n", arr[first], arr[second]);
- // Câu 7:
- int max = arr[0];
- int min_1 = arr[0];
- int min_2 = arr[0];
- int min_count = 0;
- loop(i, 0, N)
- {
- max = (max < arr[i]) ? arr[i] : max;
- min_1 = (min_1 > arr[i]) ? arr[i] : min_1;
- }
- loop(i, 0, N)
- {
- if (arr[i] < min_2 && arr[i] > min_1)
- min_2 = arr[i];
- if (arr[i] == min_1)
- {
- ++min_count;
- if (min_count == 2)
- {
- min_2 = min_1;
- break;
- }
- }
- }
- if (max > (min_1 + min_2))
- printf("YES");
- else
- printf("NO");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement