Skip to content

Instantly share code, notes, and snippets.

@jordi-petit
Last active April 26, 2018 07:27
Show Gist options
  • Save jordi-petit/a5ee597a6d4703e677377bdadde59ac0 to your computer and use it in GitHub Desktop.
Save jordi-petit/a5ee597a6d4703e677377bdadde59ac0 to your computer and use it in GitHub Desktop.
AP1 2017-10-06
// Comptar el nombre de lletres A en un text.
// => Recorregut
#include <iostream>
using namespace std;
int main() {
int n = 0;
char c;
while (cin >> c) {
if (c == 'A') n = n + 1;
}
cout << n << endl;
}
// Trobar si hi ha alguna lletra A en un text.
// => Cerca
#include <iostream>
using namespace std;
int main() {
bool trobat = false;
char c;
while (not trobat and cin >> c) {
trobat = c == 'a';
}
cout << (trobat ? "SI" : "NO") << endl;
}
// Retorna el nombre de dígits 3 en la representació decimal de x.
// Precondició: x >= 0.
// => Recorregut
int compta_nombres3(int x) {
int n = 0;
while (x > 0) {
if (x%10 == 3) n = n + 1;
x = x / 10;
}
return n;
}
// Indica si el nombre de dígits és a la representació decimal de x.
// Precondició: x >= 0.
// => Cerca
bool hi_ha_nombre3(int x) {
bool trobat = true;
while (not trobat and x > 0) {
if (x%10 == 3) trobat = true;
else x = x / 10;
}
return trobat;
}
// Aquí, millor sense el boolèa!:
bool hi_ha_nombre3(int x) {
while (x > 0) {
if (x%10 == 3) return true;
x = x / 10;
}
return false;
}
// Trobar si hi ha un pic més alt que la Pica d'Estats (P90260)
// => Cerca
#include <iostream>
using namespace std;
// definim constants (es solen escriure en MAJÚSCULES) per no tenir valors màgics al mig del codi
const int ALCADA_PICA = 3143;
const int ULTIM_ELEMENT = 0;
bool pic_guapo(int x, int y, int z) {
return x < y and y > z and y > ALCADA_PICA;
}
int main() {
bool trobat = false;
int a, b, c;
cin >> a >> b >> c;
while (not trobat and c != ULTIM_ELEMENT) {
if (pic_guapo(a, b, c)) {
trobat = true;
} else {
a = b;
b = c;
cin >> c;
}
}
cout << (trobat ? "SI" : "NO") << endl;
}
# Traducció P90260
from jutge import read
# Algun llest es va deixar de posar constants en Python! 😢
# La convenció és que les variables en MAJÚSCULES són constants
# i no s'haurien de tocar (però no està prohibit).
#
# Com a mínim, les variables en Python poden tenir accents, en C++ no.
ALÇADA_PICA = 3143
ÚLTIM_ELEMENT = 0
def pic_guapo(x, y, z):
return x < y and y > z and y > ALÇADA_PICA
def main():
trobat = False
a, b, c = read(int, int, int)
while not trobat and c != ÚLTIM_ELEMENT:
if pic_guapo(a, b, c):
trobat = True
else:
a, b, c = b, c, read(int)
print("SI" if trobat else "NO") # operador condicional en Python com el ?: de C++.
main()
@danaecanillas
Copy link

Gràcies!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment