1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to count combinations?

Discussion in 'C#' started by trovaerdig, Feb 27, 2008.

  1. #1
    Hi there.
    I have been working on the following code:
    (I didn't know if there was a tag for asp, so i just used the one there is for php).
    <%
    response.buffer=false
    
    %>
    <html><body>
    <%
    
     dim land(30)
     dim antal(30)
     dim used(30)
    dim rest(30)
    dim i
    
    rest(0) = 345
    land(0)="D"               
    antal(0)=29    
    used(0)=0
    rest(1) = 316
    land(1)="F"               
    antal(1)=29    
    used(1)=0
    rest(2) = 287
    land(2)="GB"        
    antal( 2)=29    
    used(2)=0
    rest(3) = 258
    land(3)="I"               
    antal(3)=29    
    used(3)=0
    rest(4) = 229
    land(4)="E"               
    antal(4)=27    
    used(4)=0
    rest(5) = 202
    land(5)="PL"                 
    antal(5)=27    
    used(5)=0
    rest(6) = 175
    land(6)="RO"               
    antal(6)=14    
    used(6)=0
    rest(7) = 161
    land(7)="NL"           
    antal(7)=13    
    used(7)=0
    rest(8) = 148
    land(8)="GR"            
    antal(8)=12    
    used(8)=0
    rest(9) = 136
    land(9)="P"              
    antal(9)=12    
    used(9)=0
    rest(10) = 124
    land(10)="B"              
    antal(10)=12    
    used(10)=0
    rest(11) = 112
    land(11)="CZ"              
    antal(11)=12    
    used(11)=0
    rest(12) = 100
    land(12)="H"                
    antal(12)=12    
    used(12)=0
    rest(13) = 88
    land(13)="S"              
    antal(13)=10    
    used(13)=0
    rest(14) = 78
    land(14)="A"                
    antal(14)=10    
    used(14)=0
    rest(15) = 68
    land(15)="BG"            
    antal(15)=10    
    used(15)=0
    rest(16) = 58
    land(16)="DK"              
    antal(16)=7     
    used(16)=0
    rest(17) = 51
    land(17)="SK"            
    antal(17)=7     
    used(17)=0
    rest(18) = 44
    land(18)="FIN"              
    antal(18)=7     
    used(18)=0
    rest(19) = 37
    land(19)="IRL"                
    antal(19)=7     
    used(19)=0
    rest(20) = 30
    land(20)="LT"              
    antal(20)=7     
    used(20)=0
    rest(21) = 23
    land(21)="LV"              
    antal(21)=4     
    used(21)=0
    rest(22) = 19
    land(22)="SLO"            
    antal(22)=4     
    used(22)=0
    rest(23) = 15
    land(23)="EST"              
    antal(23)=4     
    used(23)=0
    rest(24) = 11
    land(24)="CY"                
    antal(24)=4     
    used(24)=0
    rest(25) = 7
    land(25)="L"            
    antal(25)=4     
    used(25)=0
    rest(26) = 3
    land(26)="M"                
    antal(26)=3 
    used(26)=0
    
    dim count 
    dim overTwenty
    overTwenty = 0
    function mytry(byref n,byref sum)
    dim i 
    dim str
    dim x
    
    x = 0
    
    
      if (sum=255)  then
        count = count+1
     '   response.write("<br>#" & count & ": ")
        
        for i=0 to n-1
          if (used(i)=1) then
            str = str & land(i) & "(" & antal(i) & ") "
        x = x+1
    '    response.write(land(i) & "(" & antal(i) & ") ")
          end if
        next
        if (x>=20) then
            response.write("<br><strong><font COLOR='green'>+</font></strong>&nbsp;#" & count & ": " & str & " = " & sum &"")
            overTwenty = overTwenty + 1
        else
        response.write("<br><strong><font COLOR='red'>x</font></strong>&nbsp;#" & count & ": " & str & " = " & sum &"")
        end if
      else
        if (n<27) then
     '    dim newnum
      '   newNum = n+1
          if (sum  + rest(n) > 254) then
            if (sum + antal(n) < 256) then
              used(n)=1
              count = mytry(n+1,sum+antal(n))
            end if    
            used(n)=0
            count = mytry(n+1,sum)
          end if
        end if
      end if
      mytry = count
    end function
    
    
    
    response.write("<i>Bemærk: Programmet følger ikke korrekt matematisk notation, dette har noget at gøre med måden hvorpå programmet behandler sine data. Således er et '+' imellem hvert land underforstået, samtidigt med at forkortelsen foran tallet er tiltænkt som en hjælp til lettere at forstå udregningen.</i><br><br>Udregning påbegyndes/" )
    i = mytry(0,0)
    
    response.write("<br>/Udregning tilendebragt<br><br><strong>Resultater:</strong>")
    response.write("<br>Kombinationer hvori det dobbelt kvalificerede flertal overholdes: "& overTwenty &"")
    
    %>
    </body>
    </html>  
    
    PHP:
    (Don't mind the Danish in the code).
    What it does is that at finds all combanations of the countries that gives exactly 255. Afterwards it finds all combinations in which there are 20 or more countries represented, and count them in the end.
    What I need to do is to count how many combinations in which there are 20 or more countries represented, that has both the country DK and the country F in them.
    I can't quite figure out how to do this, so i am asking you for help.

    Thx Eirik-
     
    trovaerdig, Feb 27, 2008 IP