Skip to content

Instantly share code, notes, and snippets.

@rafalw
Last active March 28, 2022 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafalw/13ff3c74e6e5edd79e10cf4570df0e7a to your computer and use it in GitHub Desktop.
Save rafalw/13ff3c74e6e5edd79e10cf4570df0e7a to your computer and use it in GitHub Desktop.
Zadania maturalne (informatyka rozszerzona). Maj 2019, zadanie 4. C++11/C++17
Dane do zadania znajdują się pod adresem:
https://arkusze.pl/maturalne/informatyka-2019-maj-matura-rozszerzona-zalaczniki.zip
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <numeric> // -std=c++17
// W <numeric> jest m. in. NWD (GCD)
#include <cmath>
using namespace std;
vector<int> liczby;
void wczytaj_liczby(vector<int>& tablica) {
ifstream plik_we("liczby.txt");
string sliczba = "";
while(getline(plik_we, sliczba)) {
tablica.push_back(stoi(sliczba));
}
// Dobry nawyk:
plik_we.close();
}
bool czy_pot_3(int liczba) {
int wykl = 0;
int pot = 1;
while (pot < liczba) {
wykl++;
pot = (int)pow(3, wykl);
}
if (liczba == pot) {
return true;
}
return false;
}
int main() {
wczytaj_liczby(liczby);
int ile_pot = 0;
for (int a : liczby) {
if (czy_pot_3(a)) {
ile_pot++;
}
}
cout << "Liczba potęg trójki wynosi: " << ile_pot << endl;
return 0;
}
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <numeric> // -std=c++17
// W <numeric> jest m. in. NWD (GCD)
#include <cmath>
using namespace std;
vector<string> liczby;
// Dla niewielkich n <0, 9> możemy liczyć silnię rekurencyjnie
int silnia(int n) {
return (n == 1 || n == 0) ? 1 : n * silnia(n - 1);
}
int suma_silni(string& liczba) {
int suma = 0;
for (int i = 0; i < liczba.length(); i++) {
suma += silnia(liczba[i] - 48);
}
return suma;
}
void wczytaj_liczby(vector<string>& tablica) {
ifstream plik_we("liczby.txt");
string sliczba = "";
while(getline(plik_we, sliczba)) {
tablica.push_back(sliczba);
}
// Dobry nawyk:
plik_we.close();
}
int main() {
wczytaj_liczby(liczby);
for (string l : liczby) {
int liczba = stoi(l);
if (suma_silni(l) == liczba) {
cout << liczba << endl;
}
}
return 0;
}
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <numeric> // -std=c++17
// W <numeric> jest m. in. NWD (GCD)
using namespace std;
vector<int> liczby;
// NWD rekurencyjnie - eksperyment
// – oczywiście gdyby nie można było kompilować z przełącznikiem -std=c++17
// int gcd(int a, int b) {
// if (b == 0) {
// return a;
// }
// return gcd(b, a % b);
// }
void wczytaj_liczby(vector<int>& tablica) {
ifstream plik_we("liczby.txt");
string sliczba = "";
while(getline(plik_we, sliczba)) {
tablica.push_back(stoi(sliczba));
}
// Dobry nawyk:
plik_we.close();
}
int main() {
int dl_max = 0;
int pierw_max = 0;
int nwd_max = 0;
int ciag = 1; // wartość wynika z faktu, że najkrótszy ciąg liczb ma długość 1 ;P
int pierwsza = 0;
int tmp = 0;
int nwd = 0;
wczytaj_liczby(liczby);
for (int i = 0; i < liczby.size(); i++) {
tmp = pierwsza = liczby[i];
ciag = 1;
for (int j = i + 1; j < liczby.size(); j++) {
nwd = gcd(tmp, liczby[j]);
if (nwd == 1) {
tmp = pierwsza = liczby[j];
ciag = 1;
} else {
tmp = nwd;
ciag++;
}
if (ciag > dl_max) {
dl_max = ciag;
pierw_max = pierwsza;
nwd_max = tmp;
}
}
}
cout << "Długość ciągu: " << dl_max << " Pierwsza liczba: " << pierw_max << " NWD: " << nwd_max << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment