Java 爪哇语;“分形”;返回错误输出的方法

Java 爪哇语;“分形”;返回错误输出的方法,java,algorithm,Java,Algorithm,方向: 给定一个int[]x和一个百分比p(0-100),找出x的最小值元素y,使x的至少百分比p元素小于或等于y Example 1: x = {-3, -5, 2, 1}, p = 50 Method should return -3 Reason: 50% of the elements in x are less than or equal to -3: -5 and -3 Example 2: x = {7, 9, 2, -10, -6}, p = 50 Method sho

方向: 给定一个
int[]x
和一个百分比p(0-100),找出
x
的最小值元素
y
,使
x
的至少百分比
p
元素小于或等于
y

Example 1:
x = {-3, -5, 2, 1}, p = 50 
Method should return -3 
Reason: 50% of the elements in x are less than or equal to -3:  -5 and -3

Example 2:
x = {7, 9, 2, -10, -6}, p = 50 
Method should return 2 
Reason: 60 percent of the elements in x are less than or equal to 2: 2, -10 and -6 
-6 would be wrong because only 40% of the elements are less than or equal
(100% of the elements are less than or equal to 9, but that isn't the smallest value)

Example 3:
x = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,7,9,5,43,124,94}, p = 0
Method should return 1
Reason: Only 0% is needed, so in theory any number will do, but 1 is the smallest value

以下是我为该方法编写的内容:

public static int fractile(int[] x, int p)
   {
      int smallestInt = x[0];            
      for (int i = 0; i < x.length; i++) {
         int testNum = x[i];
         int percentage;
         int count = 0;
         for (int j = 0; j < x.length; j++) {
            if (x[j] <= testNum)
               count++;
         }
         percentage = (count / x.length) * 100;
         if (testNum <= smallestInt && percentage >= p)
            smallestInt = testNum;
      }
      return smallestInt;
   }
这几乎就像它抓住了第一个索引,没有看它背后的数字,但我不明白为什么

我做错了什么

排队

percentage = (count / x.length) * 100;
count和x.length都是整数。因此,将它们相除会得到一个整数值,而不是一个浮点数。百分比可能包含0或100,而不是介于两者之间的某个值。

行中

percentage = (count / x.length) * 100;

count和x.length都是整数。因此,将它们相除会得到一个整数值,而不是一个浮点数。百分比可能包含0或100,而不是介于之间的某个值。

将百分比更改为double/float,并将divide中的一个变量转换为double/float

比如:

double percentage=0.0;
...
percentage = ((double)count / x.length) * 100;
所以方程返回double/float

--

基本类型的数字提升规则

  • 如果两个值具有不同的数据类型,Java将自动将其中一个值升级为两个数据类型中的较大值

  • 如果其中一个值是整数,另一个值是浮点值,Java将自动将整数值提升为整数 浮点值的数据类型

  • 较小的数据类型,即byte、short和char,在任何时候与Java二进制算法一起使用时都会首先提升为int 运算符,即使两个操作数都不是int

  • 发生所有升级且操作数具有相同的数据类型后,生成的值将具有与其数据类型相同的数据类型 提升的操作数

  • Jeanne Boyarsky&Scott Selikoff-OCA研究指南

    将您的百分比更改为双精度/浮点数,并将divide中的一个变量也转换为双精度/浮点数

    比如:

    double percentage=0.0;
    ...
    percentage = ((double)count / x.length) * 100;
    
    所以方程返回double/float

    --

    基本类型的数字提升规则

  • 如果两个值具有不同的数据类型,Java将自动将其中一个值升级为两个数据类型中的较大值

  • 如果其中一个值是整数,另一个值是浮点值,Java将自动将整数值提升为整数 浮点值的数据类型

  • 较小的数据类型,即byte、short和char,在任何时候与Java二进制算法一起使用时都会首先提升为int 运算符,即使两个操作数都不是int

  • 发生所有升级且操作数具有相同的数据类型后,生成的值将具有与其数据类型相同的数据类型 提升的操作数

  • Jeanne Boyarsky&Scott Selikoff-OCA研究指南

    您需要的是选择算法,该算法可以按给定比例对数组进行分区

    简单快速的是


    <>和我怀疑在Java中有使用的实现,如C++ +代码> STD::Nthjava元素< /C> < /P> > P>你需要的是一种按比例分配数组的选择算法。 简单快速的是


    <>和我怀疑在Java中有使用的实现,如C++ ++代码:STD::Nthjava元素< /Calp>< /P> < P>对于从最低到最高排序的数组,你可以把长度与百分比相乘,以得到搜索元素的正确索引。更多信息

    这将使您的代码更短

     public static int fractile(int[] x, double p){   
       Arrays.sort(x);       
       int k = (int) Math.ceil(x.length*p)-1;
       return x[k];
    }
    

    对于从最低到最高排序的数组,您只需将长度乘以百分比即可获得所搜索元素的正确索引。更多信息

    这将使您的代码更短

     public static int fractile(int[] x, double p){   
       Arrays.sort(x);       
       int k = (int) Math.ceil(x.length*p)-1;
       return x[k];
    }
    
    你可以试试:

    public static int fractile( int[] x, int p )
       {
            Arrays.sort(x);
            return x[Math.max((int) Math.ceil(x.length / 100.0 * p) - 1, 0)];
       }
    
    这假定允许您操作数组。对数组进行排序几乎总是使操作更容易。除非阵列出于任何原因必须保持原样,否则这将起作用

    此代码对数组进行排序,由于初始输入“p”告诉您要查找的百分比,因此它查看数组中的近似位置并返回存储在那里的值。

    您可以尝试:

    public static int fractile( int[] x, int p )
       {
            Arrays.sort(x);
            return x[Math.max((int) Math.ceil(x.length / 100.0 * p) - 1, 0)];
       }
    
    这假设您可以操作数组。对数组进行排序几乎总是使操作更容易。除非阵列出于任何原因必须保持原样,否则这将起作用


    这段代码对数组进行排序,由于初始输入“p”告诉您要查找的百分比,它会查看数组中的近似位置并返回存储在那里的值。

    我有另一个解决该问题的实现:

    publicstaticvoidmain(字符串参数[]){
    int[]a1={-3,-5,2,1};
    int[]a2={7,9,2,-10,-6};
    int[]a3={1,2,3,4,5,6,7,8,9,10};
    int[]a4={1,2,3,4,5,6,7,8,9,1,2,3,4,5,7,9,5,43124,94};
    int[]a5={1};
    int p=50;
    系统输出打印LN(分位数(a1,p));
    }
    私有静态整数分位数(整数[]x,整数p){
    数组。排序(x);
    双值0=x.length/100.0*p;
    double value1=Math.ceil(value0);
    int value2=(int)value1;
    int value3=value2-1;
    int value4=Math.max(值3,0);
    返回x[value4];
    
    }
    我有另一个解决该问题的方法:

    publicstaticvoidmain(字符串参数[]){
    int[]a1={-3,-5,2,1};
    int[]a2={7,9,2,-10,-6};
    int[]a3={1,2,3,4,5,6,7,8,9,10};
    int[]a4={1,2,3,4,5,6,7,8,9,1,2,3,4,5,7,9,5,43124,94};
    int[]a5={1};
    int p=50;
    系统输出打印LN(分位数(a1,p));
    }
    私有静态整数分位数(整数[]x,整数p){
    数组。排序(x);
    双值0=x.length/100.0*p;
    double value1=Math.ceil(value0);
    int value2=(int)value1;
    int value3=value2-1;
    int value4=Math.max(值3,0);
    返回x[value4];
    }
    将强制转换为(int)会修复此问题,还是会产生不准确的结果?我刚刚尝试了此方法,但仍然存在问题