Skip to content

Instantly share code, notes, and snippets.

@jordi-petit
Last active April 26, 2018 07:28
Show Gist options
  • Save jordi-petit/f9b2e35d3ac1edd2273fab651e621136 to your computer and use it in GitHub Desktop.
Save jordi-petit/f9b2e35d3ac1edd2273fab651e621136 to your computer and use it in GitHub Desktop.
AP1 2017-10-25
// Dir si un text és palíndrom
#include <iostream>
#include <string>
using namespace std;
bool palindrom(const string& s) { // string s també estaria bé
int esq = 0;
int dre = s.size() - 1;
while (esq < dre) {
if (s[esq] != s[dre]) {
return false;
}
++esq;
--dre;
}
return true;
}
int main() {
string s;
cin >> s;
cout << palindrom(s) << endl;
}
#include <iostream> // pel cin/cout
#include <vector> // pels vectors
#include <algorithm> // pel swap
using namespace std;
// Acció que revessa un vector.
void revessa(vector<int>& v) {
int esq = 0;
int dre = v.size() - 1;
while (esq < dre) {
swap(v[esq], v[dre]);
++esq;
--dre;
}
}
int main() {
vector<int> v = {2, 5, 7, 2, 5, 8, 5};
revessa(v);
for (int x : v) cout << v << endl;
}
// Segment nul més llarg
#include <iostream>
#include <vector>
using namespace std;
// Prec: v està ordenat creixenment.
// Post: - (i,j) conté els índexos del segment nul més llarg de v.
// - Quan el segment és buit, i > j.
void segment_nul_mes_llarg (const vector<int>& v, int& i, int& j) {
int s = 0;
for (int x : v) s += x;
i = 0;
j = v.size() - 1;
while (s != 0) {
if (s > 0) {
s -= v[j];
--j;
} else {
s -= v[i];
++i;
}
}
}
int main() {
vector<int> v = {7, -3, -2, 5, 13};
int i, j;
segment_nul_mes_llarg(v, i, j);
if (i > j) {
cout << "no n'hi ha" << endl;
} else {
cout << i << "," << j << endl;
}
}
@margaritageleta
Copy link

Gràcies!!

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