繁体   English   中英

Java用条件计算计数

[英]Java calculate the count with condition

我有以下数据,我想知道哪种最佳的数据结构来计算以下各项的计数:

在此处输入图片说明

注意:

  • 我为以下所有规则创建表:a ==> z,b ==> z和c ==> z

  • 对于每个规则,例如上图中的规则(a ==> z):我为所有组合创建表:b = 1时a ==> z,c = 1时a ==> z,a ==当b,且c = 1时> z

  • 例如,当我计数时,有多少次(A = 1,B = 1和Z = 1)在一起,我必须排除次数(A = 1,B = 1,C = 1和Z = 1 )聚集在一起,在另一个病房中,我想计算一下(A = 1,B = 1,C = 0,和Z = 1)聚集在一起的次数

一种实现方法是使用前缀树(尝试),在本文中对此进行描述: http : //www.sciencedirect.com/science/article/pii/S095070511400152X#

但我不想使用任何树作为数据结构。

更新,针对那些问我是否在努力的人:

是的,我做了我的努力,但我一直在寻找更好的主意,谢谢大家。 特别感谢@isaac,非常感谢您的帮助。

我将数据重新排列为这种格式(变量,数组列表对(变量出现的记录号,记录的长度)。记录的长度=变量的值多少次= 1

例:

A, comes in the records ( (7,1) ,(8,2) , (9,2) , (10,3), (11,3), (12,3) ,(13,4) )
B, comes in the records ( (4,1) , (5,2) ,(6,3), (11,3), (12,3) ,(13,4))
C, comes in the records ( (2,1), (3,2), (5,2) ,(6,3), (9,2) , (10,3), (12,3) ,(13,4) )
Z, comes in the records ( (1,1) , (3,2), (6,3), (8,2), (10,3), (11,3), (13,4) )

然后:

  • 我将计算每个变量单独出现多少次
  • Afrequency = 1,Bfrequency = 1,Cfrequency = 1以及Zfrequency = 1
  • 然后,我根据记录的长度执行记录列表之间的交集

例如:变量(A,B,Z)之间的交点位于记录(11和13)中, 但是

记录13的长度>变量数。 因此 ,我不会计算

结果:

The count number of (A=1, B=1, Z=1) = 1. Let call this result R1
From this, I can figure out the others values:
The count number of (A=1, B=1, Z=0) =  R1 -  Afrequency  =1-1=0
The count number of (A=0, B=1, Z=1) = R1 -  Zfrequency = 1-1 =0
The count number of (A=0, B=1, Z=0) = Bfrequency =1

我不会研究您正在谈论的这个前缀树,也不会去阅读您给出的论文。 但是从我对问题的理解中,您需要计算指定的给定状态的条件数,我可以给您一些想法。

我提出了一个方法count(Boolean a, Boolean b, Boolean c, Boolean z)

只有一堆ifelse if来比较每个可能的状态与a,b,c和z。 如果测试状态等于您的输入,则递增计数器,然后您可以返回该值。 对于您的示例,您将调用count(true,true,null,true);

请注意,您不能使用基本类型布尔值,因为它不能包含null。

编辑:这是我的实现。 为简单起见,只有2位状态。 可能的状态是(A,B)0,0; 0,1; 1,0; 1,1。 我尝试计算多少A = 1;

public static void main(String[] args) {
    boolean[][] possibleStates = {{false,false},{false,true},{true,false},{true,true}};
    int result = count(true,null,possibleStates);
    System.out.println(result);
}
public static int count(Boolean a,Boolean b,boolean[][] possibleStates) {
    int counter = 0;
    Boolean oldA = a;
    Boolean oldB = b;
    for(boolean[] state : possibleStates) {
        a = oldA;
        b = oldB;
        if(a == null) 
            a = state[0];
        if(b == null) 
            b = state[1];
        if(state[0] == a && state[1] == b)
            counter++;
    }
    return counter;
}

它输出2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM