Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. package ejercicios1entrega;
  2.  
  3. import java.util.List;
  4. import java.util.stream.Collectors;
  5.  
  6. import us.lsi.common.Streams2;
  7.  
  8. public class Ejercicio5 {
  9.  
  10. public static List<int[]> leeFichero5(String nombreFich){
  11. return Streams2.fromFile(nombreFich)
  12. .map(linea->LineaArray(linea))
  13. .collect(Collectors.toList());
  14. }
  15.  
  16.  
  17. private static int[] LineaArray(String s){
  18. String[] sar = s.split(",");
  19. int[] ares = new int[sar.length];
  20. for(int i=0;i<sar.length;i++) {
  21. ares[i] = Integer.parseInt(sar[i]);
  22. }
  23. return ares;
  24. }
  25.  
  26.  
  27. public static int problema5(int[] ar) {
  28. int n = ar.length;
  29. return problema5aux(ar,0,n,0);
  30. }
  31.  
  32. private static int problema5aux(int[] ar, int i, int j,int areares) {
  33. if(j-i!=0) {
  34. int menor= problema5aux2(ar, i, j);
  35. int vmenor= ar[menor];
  36. int area= (j-i)*vmenor;
  37. if(area>areares) {
  38. areares=area;
  39. }
  40. if(j-i!=1) {
  41. if(menor!=i) {
  42. areares =problema5aux(ar,i,menor,areares);
  43. }
  44. if(menor!=j-1) {
  45. areares =problema5aux(ar,menor+1,j,areares);
  46. }
  47. }
  48. }
  49. return areares;
  50. }
  51.  
  52. private static int problema5aux2(int[]ar, int i, int j) {
  53. int n=0;
  54. int nres=0;
  55. for(int r=i;r<j;r++) {
  56. n = ar[r];
  57. if(r==i) {
  58. nres=r;
  59. }else {
  60. if(n<ar[nres]) {
  61. nres=r;
  62. }
  63. }
  64. }
  65. return nres;
  66. }
  67.  
  68. public static void main(String[] args) {
  69. List<int[]> lsres = leeFichero5("ficheros/PI2Ej5DatosEntrada.txt");
  70. List<String> laux = Streams2.fromFile("ficheros/PI2Ej5DatosEntrada.txt")
  71. .collect(Collectors.toList());
  72. for(int i = 0;i<lsres.size();i++) {
  73. System.out.println("Histograma: " + "[" + laux.get(i) + "]");
  74. System.out.println("Área máxima: " + problema5(lsres.get(i)));
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement