8.13 ** (The Location class)

r囧r小猫 2022-08-09 14:40 117阅读 0赞
  1. 问题及代码:
  2. Main.java
  3. package first;
  4. import java.util.Scanner;
  5. public class Main {
  6. public static void main(String[] args) {
  7. int row, column ;
  8. System.out
  9. .print("Enter the number of tows anf columns of the array: ");
  10. Scanner cin = new Scanner(System.in);
  11. row = cin.nextInt();
  12. column = cin.nextInt();
  13. System.out.println("Enter the array: ");
  14. double [][]array = new double[row][column];
  15. for(int i=0;i<row;++i)
  16. {
  17. for(int j=0;j<column;++j)
  18. {
  19. array[i][j]=cin.nextDouble();
  20. }
  21. }
  22. Location a=new Location(row,column,array);
  23. Point x=new Point();
  24. x=a.getmaxPoint();
  25. System.out.println("The location of the largest element is "+(int)a.max +" at ("+(int)x.getx()+","+(int)x.gety()+")");
  26. }
  27. }
  28. Location.java
  29. package first;
  30. public class Location {
  31. public int row, column;
  32. double [][]array;
  33. double max;
  34. Point maxPoint=new Point();
  35. Location (int a,int b,double a1[][])
  36. {
  37. row=a;
  38. column=b;
  39. array=a1;
  40. }
  41. Point getmaxPoint()
  42. {
  43. max=array[0][0];
  44. for(int i=0;i<row;++i)
  45. {
  46. for(int j=0;j<column;++j)
  47. {
  48. if(array[i][j]>max)
  49. {
  50. max=array[i][j];
  51. maxPoint.setPoint(i,j);
  52. }
  53. }
  54. }
  55. return maxPoint;
  56. }
  57. }
  58. Point.java
  59. package first;
  60. public class Point {
  61. double a,b;
  62. Point()
  63. {
  64. a=0;
  65. b=0;
  66. }
  67. Point(double x,double y)
  68. {
  69. a=x;
  70. b=y;
  71. }
  72. void setPoint(double x,double y)
  73. {
  74. a=x;
  75. b=y;
  76. }
  77. double getx()
  78. {
  79. return a;
  80. }
  81. double gety()
  82. {
  83. return b;
  84. }
  85. }
  86. 运行结果:

Center

发表评论

表情:
评论列表 (有 0 条评论,117人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CF813E Army Creation

    昨天想了好久……现在想想是水题啊。 首先对于一个区间,如果一个数出现的次数超过了$k$次, 那么直接选$k$个这个数就好了。然后我们算一算每一个数对答案的贡献,对于第$i$个