Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Mínim comú múltiple

#include <iostream>
using namespace std;

int mcd(int a, int b) {


while (b > 0) {
int r = a%b;
a = b;
b = r;
}
return a;
}
int mcm(int a, int b) {
return (a/mcd(a,b))*b;
}
int main() {
int n;
while (cin >> n) {
if (n >= 1) {
int m, mcm_total;
mcm_total = 1;
for (int i = n; i > 0; --i) {
cin >> m;
mcm_total = mcm( mcm_total, m);
}
cout << mcm_total << endl;
}
}

}
Capicues
#include <iostream>
using namespace std;

int invierte (int n) {


int i = 0;
while (n > 0) {
i = i*10 + n%10;
n = n/10;
}
return i;
}

bool es_capicua(int n){


int i = invierte (n);
if (n == i) return true;
else return false;

}
int main() {

int n;
while (cin >> n)
cout << es_capicua(n) << endl;
}
Canvis de base
#include <iostream>
using namespace std;

void base_2(int n){


if (n > 0){
base_2(n/2);
cout << n%2;
}
}
void base_8(int n){
if (n > 0){
base_8(n/8);
cout << n%8;
}
}
void base_16(int n){
if ( n > 0){
if (n <= 9) cout << n;
else {
base_16(n/16);
if ( n%16 == 10) cout << 'A';
else if ( n%16 == 11) cout << "B";
else if ( n%16 == 12) cout << "C";
else if ( n%16 == 13) cout << "D";
else if ( n%16 == 14) cout << "E";
else if ( n%16 == 15) cout << "F";
else cout << n%16;
}
}
}
int main() {
int n;
while (cin >> n) {
if (n == 0) cout << n << " = " << n << ", " << n << ", " << n <<
endl;
else {
cout << n << " = ";
base_2(n);
cout << ", ";
base_8(n);
cout << ", ";
base_16(n);
cout <<endl;
}
}
}
Barres (1)
#include <iostream>
using namespace std;
void n_ast(int n) {
if (n == 0) cout << endl;
else {
cout << '*';
n_ast(n-1);
}
}
void bar(int n) {
if (n == 1) cout << '*' << endl;
else {
bar(n-1);
n_ast(n);
bar(n-1);
}
}
int main() {
int n;
cin >> n;
bar(n);
}
Girant una llista de paraules (1)
#include <iostream>
using namespace std;
void giran() {
string s;
while (cin >> s) {
giran();
cout << s << endl;
}
}
int main() {
giran();
}

Girant una llista de paraules (3)


Paraules consecutives repetides
#include <iostream>
#include <string>
using namespace std;

int main() {
string mare;
string fill;
int cont = 1;
cin >> mare;
int contmax = 0;
while (cin >> fill) {
if (mare == fill) ++cont;
else {
if (cont > contmax) contmax = cont;
cont = 0;
}
}
if (cont > contmax) contmax = cont;
cout << contmax << endl;
}

Parells creixents
#include <iostream>
using namespace std;

int main() {
int n;
int cont = 0;
int ant, post;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> ant;
if (ant != 0) {
while ( cin >> post and post != 0) {
if (post > ant) ++cont;
ant = post;
}
cout << cont << endl;
cont = 0;
}
else cout << 0 << endl;
}
}
Codi Cèsar
#include <iostream>
using namespace std;

char codificat(char c, int k) {


c = 'A' + (c - 'a' + k) % 26;
return c;
}
int main() {
int k;
char c;
while (cin >> k) {
while (cin >> c and c != '.') {
if (c == '-') cout << "-";
else if (c == '_') cout << " ";
else if (c == ',') cout << ",";
else if (c >= 'a' and c <= 'z') cout << codificat(c,k);
else cout << c;
}
cout << endl;
}
}

Primalitat
#include <iostream>
#include <cmath>
using namespace std;

bool es_primer(int n) {
if (n == 0 or n == 1) return false;
for (int i = 2; i <= sqrt(n); ++i) {
if (n%i == 0) return false;
}
return true;
}

void escriu_es_primer(int n) {
cout << n;
if (not es_primer(n)) cout << " no";
cout << " es primer" << endl;
}

int main() {
int n, x; //n:# nombres de la sequencia
//x:nombre a tractar
cin >> n;
while (n > 0) {
cin >> x;
escriu_es_primer(x);
--n;
}
}
Dígits centrals
#include <iostream>
using namespace std;

int nombre_digits(int n) {
int c = 1;
while (n > 10) {
n /= 10;
++c;
}
return c;
}

int digit_central(int n, int c) {


for (int i = 0; i < c/2; ++i) {
n/=10;
}
return n%=10;
}

int main() {
int n, p, A, B;
cin >> n;
n *= 2;
bool guanyaA, guanyaB;
guanyaA = guanyaB = false;
bool primer_nombre = true;
while ((not guanyaA or not guanyaB) and n > 0) {
cin >> A;
int c = nombre_digits(A);
if (c%2 != 0 and primer_nombre) {
p = digit_central(A, c);
primer_nombre = false;
}
if (c%2 == 0 or (digit_central(A, c) != p)) guanyaB = true;
cin >> B;
c = nombre_digits(B);
if (not guanyaB) {
if (c%2 == 0 or (digit_central(B, c) != p)) guanyaA = true;
}
--n;
}
while (n > 0) {
cin >> A >> B;
--n;
}
if (guanyaA) cout << 'A' << endl;
else if (guanyaB) cout << 'B' << endl;
else cout << '=' << endl;
}
Parèntesis
#include <iostream>
using namespace std;

int main() {
char sym;
int paren = 0;
bool mal = false;
while (cin >> sym and not mal) {
if (sym == '(') ++paren;
else --paren;
if (paren < 0) mal = true;
}
if (paren != 0) mal = true;
if (mal) cout << "no" << endl;
else cout << "si" << endl;
}

Nombres equilibrats
#include <iostream>
using namespace std;

bool es_equilibrat(int n) {
int numimp = 0, numpar = 0;
while (n > 0) {
numimp += n%10;
n /= 10;
numpar += n%10;
n /= 10;
}
if (numimp == numpar) return true;
return false;
}

int main() {
int n;
cin >> n;
if (es_equilibrat(n)) cout << "si" << endl;
else cout << "no" << endl;
}
Línies ordenades (1)
#include <iostream>
using namespace std;

int main() {
int x;
bool creixent = false;
int num_lin = 0;
while (not creixent and cin >> x) {
string s;
cin >> s;
creixent = true;
for (int i = 1; i < x; ++i){
string s2;
cin >> s2;
creixent = creixent and (s <= s2);
s = s2;
}
++num_lin;
}
if (creixent) cout << "La primera linia ordenada creixentment
es la " << num_lin << "." << endl;
else cout << "No hi ha cap linia ordenada creixentment." <<
endl;
}

Sumes màximes
#include <iostream>
using namespace std;

int main() {
int x;
while (cin >> x){
int suma_esq = 0, suma_dre = 0, suma_total = 0;
for (int i = 0; i < x; ++i){
int y;
cin >> y;
suma_total += y;

if (suma_total > suma_esq) suma_esq = suma_total;


else if (suma_total < suma_dre) suma_dre = suma_total;
}
cout << suma_esq << " " << suma_total - suma_dre << endl;
}
}
Poderos
#include <iostream>
using namespace std;

bool es_poderos(int n) {
bool passat = false;
for (int i = 2; i <= n; ++i) {
if (n%i == 0) {
passat = true;
if ((n/i)%i == 0) {
while (n%i == 0) n /= i;
}
else return false;
}
}
if (passat) return true;
else return false;

int main() {
int n;
while (cin >> n) {
cout << "1";
for (int i = 2; i <= n; ++i) {
if (es_poderos(i)) cout << "," << i;
}
cout << endl;
}
}

Multiples de X
#include <iostream>
using namespace std;

int main() {
int n;
int seq;
cin >> n;
int cont = 0;
while (cin >> seq) {
if (seq%n == 0) ++cont;
}
cout << cont << endl;
}
Paraules entre guions
#include <iostream>
using namespace std;

int main() {
int c = 0;
int m = 0;
int l = 0;
char p;
int cont = 0;
while (cin >> p and p != '.') {
if (p == '-' and cont != 0) {
if (cont < 5) ++c;
else if ( cont >= 5 and cont <= 9) ++m;
else ++l;
cont = 0;
}
else if (p != '-') {
++cont;
}
}
if (cont != 0) {
if (cont < 5) ++c;
else if ( cont >= 5 and cont <= 9) ++m;
else ++l;
}
cout << c << "," << m << "," << l << endl;
}

Intercalacio
#include <iostream>
using namespace std;

int intercalacio (int x,int y) {


int s = 0;
if (x == 0) return (y - y%10)*10 + y;
if ( y == 0) return x*10;
s = (x%10)*10 + y%10;
return intercalacio (x/10, y/10)*100 + s;
}

int main() {
int x,y;
while (cin >> x >> y) {
cout << intercalacio (x,y) << endl;
}
}
Cubs
#include <iostream>
using namespace std;

int main() {
int a, b;
while (cin >> a >> b){
cout << "suma dels cubs entre "<< a << " i " << b << ": ";
int sumacub = 0;
for (int i = a; i <= b; ++i){
sumacub += i*i*i;
}
cout << sumacub << endl;
}
}

You might also like