Created
September 26, 2017 12:34
-
-
Save jordi-petit/5e6754240fe367830da3698c6e5620c4 to your computer and use it in GitHub Desktop.
AP1 2017-09-26
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Suma d'una seqüència d'enters | |
// (recordeu que cal picar Control+D per finalitzar l'entrada al terminal) | |
#include <iostream> | |
using namespace std; | |
int main() { | |
int s = 0; | |
int x; | |
while (cin >> x) { | |
s = s + x; | |
} | |
cout << s << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Suma d'una seqüència de reals | |
// (recordeu que cal picar Control+D per finalitzar l'entrada al terminal) | |
#include <iostream> | |
using namespace std; | |
int main() { | |
double s = 0; | |
double x; | |
while (cin >> x) { | |
s = s + x; | |
} | |
cout << s << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mitjana d'una seqüència (no buida!) de reals | |
// (recordeu que cal picar Control+D per finalitzar l'entrada al terminal) | |
#include <iostream> | |
using namespace std; | |
int main() { | |
double s = 0; | |
int n = 0; | |
double x; | |
while (cin >> x) { | |
s = s + x; | |
n = n + 1; | |
} | |
cout << s/n << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Nombre d'elements successius repetits en una seqüència d'enters. | |
#include <iostream> | |
using namespace std; | |
int main() { | |
int r = 0; | |
int a; | |
if (cin >> a) { | |
int b; | |
while (cin >> b) { | |
if (b == a) r = r + 1; | |
a = b; | |
} | |
} | |
cout << r << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Per a cada tres nombres en una seqüencia, dir si són o no ternes pitagòriques | |
#include <iostream> | |
using namespace std; | |
int sqr(int x) { | |
return x * x; | |
} | |
bool es_terna_pitagorica(int a, int b, int c) { | |
return sqr(a) * sqr(b) == sqr(c); | |
} | |
int main() { | |
int a, b, c; | |
while (cin >> a >> b >> c) { | |
if (es_terna_pitagorica(a, b, c)) cout << "SI" << endl; | |
else cout << "NO" << endl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Escriure les arrels quadrades amb 6 digíts de precisió | |
#include <iostream> | |
#include <cmath> // usem la llibreria de funcions matemàtiques | |
using namespace std; | |
int main() { | |
// aquestes dues línies "màgiques" formaten els reals amb 6 dígits: | |
cout.setf(ios::fixed); | |
cout.precision(6); | |
double x; | |
while (cin >> x) { | |
cout << sqrt(x) << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment