Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- using namespace std;
- vector<int> notPrimeInRange(int L, int R) {
- vector<int> not_primes;
- for (int i = L; i <= R; i++) {
- if (i == 1 || i == 0) {
- continue;
- }
- bool is_prime = true;
- for (int j = 2; j <= sqrt(i); ++j) {
- if (i % j == 0) {
- is_prime = false;
- break;
- }
- }
- if (!is_prime) {
- not_primes.push_back(i);
- }
- }
- return not_primes;
- }
- int main() {
- int t;
- cin >> t;
- vector<pair<int, pair<int, int>>> results; // guardar o numero impar e o par da resposta
- for (int i = 0; i < t; i++) {
- int temp;
- cin >> temp;
- if (temp % 2 == 0) {
- results.push_back({temp, {-1, -1}}); // numeros pares só recebem isso
- continue;
- }
- vector<int> not_primes = notPrimeInRange(2, temp); // lista de numeros não primos no range
- int mindist = 1000000;
- // declare found here
- bool found = false;
- pair<int, int> solution = {-1, -1}; // possível par
- for (int j = 0; j < not_primes.size(); j++) {
- for (int k = j + 1; k < not_primes.size(); k++) {
- int sum = not_primes[j] + not_primes[k]; //soma 2 a 2 dos numeros primos da lista
- if (sum == temp) {
- int distancia = abs(not_primes[j] - not_primes[k]); // caso a soma for igual, checar a distancia
- if (distancia < mindist) {
- mindist = distancia;
- solution = {not_primes[j], not_primes[k]};
- found = true; //achou a menor distancia, criou o par
- }
- }
- }
- }
- results.push_back({temp, solution}); // armazena o numero da entrada com o par de soluções
- }
- // printa tudo de uma vez
- for (auto result : results) {
- int odd_num = result.first;
- pair<int, int> pair = result.second;
- if (odd_num % 2 == 0) {
- cout << "nenhum numero par." << endl;
- }
- else {
- cout << odd_num << ": " << pair.first << " " << pair.second << endl;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment