Z ciekawości zrobiłem dwie alternatywne (krótsze) wersje w C++:
- C z klasami (test.cpp)
#include <iostream>
int pot3[100001],sum;
main() {
int x;
for(int i = 11, a = 1; i; i--, a *= 3) pot3[a] = 1;
while(std::cin >> x) sum += pot3[x];
std::cout << sum << std::endl;
}
- bardziej C++11 (test2.cpp)
#include <iostream>
#include <unordered_set>
main() {
std::unordered_set<int> pot3;
int x, sum = 0;
for(int i = 11, a = 1; i; i--, a *= 3) pot3.insert(a);
while(std::cin >> x) sum += pot3.count(x);
std::cout << sum << std::endl;
}
Wynik testów (wersja pajtonowa z https://4programmers.net/Forum/1682688):
python -m compileall . && time python test.pyc <liczby.txt
Listing . ...
Compiling ./test.py ...
18
real 0m0,014s
user 0m0,009s
sys 0m0,004s
piotr@piotr-Prec-M47:~/progs/cpp/tests/matura2019$ g++ test.cpp && time ./a.out <liczby.txt
18
real 0m0,002s
user 0m0,002s
sys 0m0,000s
piotr@piotr-Prec-M47:~/progs/cpp/tests/matura2019$ g++ test2.cpp && time ./a.out <liczby.txt
18
real 0m0,002s
user 0m0,002s
sys 0m0,000s