Haureu d’implementar una funció que rebrà un vector ordenat creixentment de notes d’un examen. Cada nota serà un double entre 0 i 10. Conceptualment, cadascuna d’aquestes notes correspon a una categoria:
L’objectiu de la funció és calcular el nombre de notes de cada categoria.
Aquesta és la capçalera:
// Pre: Sigui n el tamany v.size(). // v està ordenat creixentment, és a dir, per a tot i de {0..n-2}, v[i]<=v[i+1]. // Els valors de v estan entre 0 i 10, és a dir, per a tot i de {0..n-1}, 0<=v[i]<=10. // Post: numA conté el nombre de valors x de v que cumpleixen 9<=x<=10. // numB conté el nombre de valors x de v que cumpleixen 7<=x<9. // numC conté el nombre de valors x de v que cumpleixen 5<=x<7. // numD conté el nombre de valors x de v que cumpleixen 0<=x<5. void classify(const vector<double> &v, int &numA, int &numB, int &numC, int &numD);
Per tal de resoldre l’exercici eficientment podeu, i és raonable, crear i utilitzar convenientment una funció auxiliar.
Observació Només cal enviar el procediment demanat; el programa principal serà ignorat.
Observació
Avaluació sobre 10 punts:
Entenem com a solució ràpida una que és correcta, de cost logarítmic i capaç de superar els jocs de proves públics i privats. Entenem com a solució lenta una que no és ràpida, però és correcta i capaç de superar els jocs de proves públics.
classify([0, 1, 1, 2, 3, 5, 5, 5, 9, 9], numA, numB, numC, numD) => numA = 2, numB = 0, numC = 3 , numD = 5 classify([0, 0, 0, 1, 2, 3, 4, 4, 6, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 10], numA, numB, numC, numD) => numA = 4, numB = 7, numC = 1 , numD = 8 classify([0.7, 0.9, 1.3, 2.2, 2.6, 2.6, 2.9, 3.0, 3.3, 4.0, 4.7, 6.0, 6.1, 6.4, 7.0, 7.2, 7.6, 7.9, 9.5, 9.9], numA, numB, numC, numD) => numA = 2, numB = 4, numC = 3 , numD = 11 classify([0.0, 0.1, 0.1, 0.1, 0.1, 0.2, 0.4, 0.4, 0.5, 0.8, 1.1, 1.3, 1.6, 2.5, 4.2, 4.2, 4.5, 5.2, 5.4, 6.3, 7.1, 7.2, 7.2, 8.0, 8.0, 8.1, 8.1, 8.3, 8.4, 8.4, 8.6, 8.6, 8.8, 9.0, 9.0, 9.0, 9.1, 9.7, 9.8, 10.0], numA, numB, numC, numD) => numA = 7, numB = 13, numC = 3 , numD = 17 classify([0.00, 0.01, 0.01, 0.02, 0.05, 0.10, 0.11, 0.11, 0.12, 0.12, 0.15, 0.33, 0.33, 0.43, 0.51, 0.62, 0.66, 0.83, 0.90, 1.01, 1.07, 1.61, 1.83, 2.00, 2.05, 2.32, 2.79, 3.09, 3.72, 3.73, 3.98, 4.01, 4.01, 4.36, 4.42, 4.43, 4.50, 5.00, 5.02, 5.02, 5.04, 5.05, 5.07, 5.17, 5.23, 5.25, 5.35, 5.46, 5.46, 5.46, 5.47, 5.59, 5.62, 5.73, 5.76, 5.80, 5.83, 5.87, 5.88, 5.91, 5.93, 6.22, 6.31, 6.32, 6.44, 6.54, 6.56, 6.58, 6.66, 7.14, 7.41, 7.44, 7.84, 8.16, 8.23, 8.93, 8.97, 9.40, 9.61, 9.88], numA, numB, numC, numD) => numA = 3, numB = 8, numC = 32 , numD = 37 classify([0.04, 0.08, 0.37, 0.89, 2.98, 6.90, 7.88, 8.01, 8.01, 8.12, 8.16, 8.39, 8.63, 9.03, 9.04, 9.22, 9.43, 9.57, 9.87], numA, numB, numC, numD) => numA = 6, numB = 7, numC = 1 , numD = 5 classify([0, 1, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 8, 8], numA, numB, numC, numD) => numA = 0, numB = 2, numC = 5 , numD = 8 classify([0.00, 0.05, 0.39, 2.93, 3.67, 9.64, 9.86, 9.86, 9.87, 9.87, 9.89], numA, numB, numC, numD) => numA = 6, numB = 0, numC = 0 , numD = 5 classify([0, 0, 0, 0, 1, 2, 2, 6, 9, 9, 9, 9, 9, 9, 10], numA, numB, numC, numD) => numA = 7, numB = 0, numC = 1 , numD = 7 classify([2.3690, 2.4945, 3.0053, 7.5277], numA, numB, numC, numD) => numA = 0, numB = 1, numC = 0 , numD = 3 classify([0.2041, 0.6053, 1.2083, 1.5769, 1.8987, 2.3352, 3.3062, 4.0464, 4.1776, 4.2180, 4.3149, 4.4148, 4.7549, 4.8261, 5.3559, 5.5700, 7.1105, 7.2104, 9.4366], numA, numB, numC, numD) => numA = 1, numB = 2, numC = 2 , numD = 14 classify([0.0, 0.1, 0.5, 1.1, 1.3, 1.4, 1.6, 2.0, 2.0, 2.0, 2.0, 3.3, 4.5, 5.1, 6.0, 6.9, 8.2], numA, numB, numC, numD) => numA = 0, numB = 1, numC = 3 , numD = 13 classify([0.0108, 0.0550, 0.0675, 4.8777, 5.3075], numA, numB, numC, numD) => numA = 0, numB = 0, numC = 1 , numD = 4 classify([0.065, 0.087, 0.089, 0.183, 0.446, 0.502, 0.971, 3.623, 3.835, 5.462, 5.614, 8.363, 9.090, 9.873, 9.875, 9.875, 9.906, 9.906, 9.923, 9.926], numA, numB, numC, numD) => numA = 8, numB = 1, numC = 2 , numD = 9 classify([0.46, 0.50, 0.61, 1.12, 2.93, 4.12, 4.19, 5.26, 5.89, 6.11, 6.43], numA, numB, numC, numD) => numA = 0, numB = 0, numC = 4 , numD = 7 classify([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 5, 5, 5, 6], numA, numB, numC, numD) => numA = 0, numB = 0, numC = 4 , numD = 13 classify([0.05, 0.50, 0.57, 0.84, 1.18, 1.19, 1.53, 2.80, 3.27, 4.17, 5.98, 7.09, 8.61, 8.65, 9.34], numA, numB, numC, numD) => numA = 1, numB = 3, numC = 1 , numD = 10 classify([1, 1, 3, 3, 3, 4, 5, 5, 7, 7, 8, 8, 10], numA, numB, numC, numD) => numA = 1, numB = 4, numC = 2 , numD = 6 classify([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 4, 6, 7, 8, 9], numA, numB, numC, numD) => numA = 1, numB = 2, numC = 1 , numD = 16 classify([3.8308, 8.0787, 8.3485, 8.9226, 9.7602], numA, numB, numC, numD) => numA = 1, numB = 3, numC = 0 , numD = 1 classify([0.1414, 0.2473, 0.2554, 1.0339, 1.0394, 1.1048, 1.2947, 1.5050, 4.2030], numA, numB, numC, numD) => numA = 0, numB = 0, numC = 0 , numD = 9 classify([0.27, 0.93, 4.64, 5.77, 7.59, 8.56, 8.57, 8.64, 8.97, 9.35, 9.39, 9.50], numA, numB, numC, numD) => numA = 3, numB = 5, numC = 1 , numD = 3 classify([0.0220, 0.0287, 0.0555, 0.1169, 0.2923, 0.9300, 2.3382, 3.9107, 4.1080, 4.1227, 5.1325, 5.1486, 6.2229, 6.7110, 6.8687, 7.7458, 8.2366, 8.5744], numA, numB, numC, numD) => numA = 0, numB = 3, numC = 5 , numD = 10 classify([0.0, 0.1, 0.4, 1.3, 1.8, 2.3, 3.5, 4.2, 5.5, 5.8, 7.4, 8.4], numA, numB, numC, numD) => numA = 0, numB = 2, numC = 2 , numD = 8 classify([0.978, 1.065, 1.819, 2.840, 4.501, 5.589, 7.295, 7.332, 8.355, 8.764, 9.390], numA, numB, numC, numD) => numA = 1, numB = 4, numC = 1 , numD = 5