从阵列中识别组

时间:2014-04-27 22:42:14

标签: arrays sorting counting basic

好的,首先我在BASIC写这个,因为它是我最了解的语言。

该程序假设有6个骰子正在滚动,它贯穿于[1] [1] [1] [1] [1] [1]到[6] [6] [6] [6]的所有可能组合。 ] [6] [6]。不要担心" b"和" r"在数字旁边。

我希望能够做到的(希望如此),而不必做一个复杂的IF语句就是贯穿每一个" roll"并选出诸如成对和三元组之类的组......等等,但我也想区分,如果说三重计算 - 一对不是。


IF A=B AND A<>C AND A<>D AND A<>E AND A<>F THEN PAIR=PAIR+1
IF A<>B AND A=C AND A<>D ...

这样做会花费很长时间并且需要(如果我已经正确计算)15个IF语句只是为了表明是否有1对,在我开始三元组和四元组等之后......

这是我的代码 - 这是一项正在进行的工作,因此是奇数编号。


10 REM DEFINE DICE TYPE X
20 DIM X$(6)
30 X$(1) = "1r"
40 X$(2) = "2b"
50 X$(3) = "3r"
60 X$(4) = "4b"
70 X$(5) = "5r"
80 X$(6) = "6b"
90
100 REM DEFINE DICE TYPE Y
110 DIM Y$(6)
120 Y$(1) = "1b"
130 Y$(2) = "2r"
140 Y$(3) = "3b"
150 Y$(4) = "4r"
160 Y$(5) = "5b"
170 Y$(6) = "6r"
180
190
200 REM SET DICE FACES AND ROLL COUNTER
210 LET A = 1
220 LET B = 1
230 LET C = 1
240 LET D = 1
250 LET E = 1
260 LET F = 1
270 LET R = 0
280
281
282 REM DEFINE HANDS
283 LET P = 0
284 LET PP = 0
285 LET PPP = 0
286 LET T = 0
287 LET TP = 0
288 LET TT = 0
289 LET Q = 0
290 LET QP = 0
291 LET QU = 0
292 LET S = 0
293
300 REM ROLL THE DICE
310 LET R = R + 1
320 IF A = B AND A = C AND A = D AND A = E AND A = F THEN LET S = S + 1
1300 PRINT X$(F), X$(E), X$(D), Y$(C), Y$(B), Y$(A), "ROLL "; R
1330 PRINT "SEXTUPLETS: "; S
1340
1350 REM INDEX THE ROLLS
1360 LET A = A + 1
1370 IF A < 7 GOTO 300
1380 LET A = 1
1390
1400 LET B = B + 1
1410 IF B < 7 GOTO 300
1420 LET B = 1
1430
1440
1450 LET C = C + 1
1460 IF C < 7 GOTO 300
1470 LET C = 1
1480
1490
1500 LET D = D + 1
1510 IF D < 7 GOTO 300
1520 LET D = 1
1530
1540
1550 LET E = E + 1
1560 IF E < 7 GOTO 300
1570 LET E = 1
1580
1590
1600 LET F = F + 1
1610 IF F < 7 GOTO 300
1620
1630
1640
1650 END

如果您没有BASIC编译器(我正在使用QB64),请在此处将代码弹出到Qloud:http://www.qb64.net/并按下运行

3 个答案:

答案 0 :(得分:0)

这不完全是你想要的,但它显示了它在Python中的样子。

从(行号)基本来看,Python看起来很奇怪。但它是最容易学习的现代语言之一,它非常强大(使用它的标准库和其他库)。这将是一段旅程,但如果您对编程有任何兴趣或需要,那么非常值得。

在Python程序中,缩进很重要,所以要小心移动东西。

获取python:https://www.python.org/ Python 3.4.0可能最适合你。文档:https://docs.python.org/3/

# A hash tag indicates a comment to the end of the line.

def increment(dice):    # define a function (aka, subroutine)
  dice[0] += 1
  if dice[0] > 6:
    dice[0] = 1
    dice[1] += 1
    if dice[1] > 6:
      dice[1] = 1
      dice[2] += 1
      if dice[2] > 6:
        dice[2] = 1
        dice[3] += 1
        if dice[3] > 6:
          dice[3] = 1
          dice[4] += 1
          if dice[4] > 6:
            dice[4] = 1
            dice[5] += 1
            if dice[5] > 6:
              dice[5] = 1
              return False   # return False when done
  return True

def count_pips(dice):
  counts = [0, 0, 0, 0, 0, 0]
  for x in dice:
    counts[x-1] += 1    # x-1 because of 0-based indices
  return counts

def analyze_counts(counts):
  stats = []  # will hold number of doubles, number of triples, etc.
  for i in range(2, 7):
    stats.append(counts.count(i))
  return stats

# Note that list indices are 0-based
# so indices will go from 0 to 5 for 6 elements.
dice = [1, 1, 1, 1, 1, 1]
counts = count_pips(dice)
stats = analyze_counts(counts)
print dice, stats
while increment(dice):
  counts = count_pips(dice)
  stats = analyze_counts(counts)
  print(dice, stats)

如果你能更准确地描述你想要做的事情,我可以稍微解决一下。

代码的一些解释:

dice是一个列表,其元素像里程表(值1到6)一样通过1,1,1,1,1,1到6,6,6,6的所有组合递增, 6,6。 (但你知道的!)

counts是一个列表,其中包含dice中的1,2等的计数。

stats是一个列表,其中填充了count中的2,3等的数字。列表具有与它们相关联的功能,例如count(其巧合地命名类似于我们的变量counts)。 count函数返回给定列表中具有给定值的元素数。

另请注意以下行:

for i in range(2, 7):

是一个循环,其中i取值从2到6(比第二个数字少一个,这可能看起来有点奇怪,但这就是它的工作方式)。

答案 1 :(得分:0)

在使用Python代码之后,我还设法让它在BASIC中工作。

这是我的新程序(编辑 - 现在这是完整的程序,如果有人有兴趣,不仅要计算卷数,还要计算颜色分组) - 如果你想测试这个塞进Qloud at { {3}}准备好等一下 - 不过不长。

这是我的新代码:

  10    start_time = TIMER

  20    OUT &H3C8, 0 'Set COLOR 0 to black

  30    OUT &H3C9, 1 'R%

  40    OUT &H3C9, 1 'G%

  50    OUT &H3C9, 1 'B%

  60    OUT &H3C8, 1 'set COLOR 1 to red

  70    OUT &H3C9, 63 'R%

  80    OUT &H3C9, 1 'G%

  90    OUT &H3C9, 1 'B%

 100    OUT &H3C8, 2 'set COLOR 2 to white

 110    OUT &H3C9, 63 'R%

 120    OUT &H3C9, 63 'G%

 130    OUT &H3C9, 63 'B%

 140    OUT &H3C8, 3 'set COLOR 3 to dark green

 150    OUT &H3C9, 1 'R%

 160    OUT &H3C9, 19 'G%

 170    OUT &H3C9, 1 'B%

 180    COLOR 2, 3 'Set forground (text) colour to white and background colour to dark green

 190    CLS

 200    DIM face(6)

 210    DIM count_spots(6)

 220    DIM colour(6)

 230    DIM r(6)

 240    DIM b(6)

 250    DIM sr(6)

 260    DIM sb(6)

 270    roll = 0

 280    one_pair = 0: xx = 0: xy = 0

 290    two_pair = 0: xx_xx = 0: xx_yy = 0: xx_xy = 0: xy_xy = 0

 300    three_pair = 0: xx_xx_xy = 0: xx_yy_xy = 0: xy_xy_xy = 0

 310    one_trip = 0: xxx = 0: xxy = 0

 320    one_trip_one_pair = 0: xxx_xx = 0: xxx_yy = 0: xxy_xx = 0: xxy_yy = 0: xxy_xy = 0

 330    two_trips = 0: xxx_xxx = 0: xxx_yyy = 0: xxx_xxy = 0: xxy_xxy = 0

 340    one_quad = 0: xxxy = 0: xxyy = 0

 350    one_quad_one_pair = 0: xxxy_xx = 0: xxxy_yy = 0: xxxy_xy = 0: xxyy_xx = 0: xxyy_xy = 0

 360    one_quint = 0

 370    one_sextuple = 0

 380    one_straight = 0: xxxxxx = 0: xxxxyy = 0: xxxyyy = 0

 390    FOR die = 1 TO 6

 400        face(die) = 1

 410    NEXT die

 420    PRINT "  NO. SHOWING ON  |  ROLL | NO. OF GROUPS OF | COLOUR OF FACE"

 430    PRINT "  THE DICE FACE   |  NO.  | 6  5  4  3  2  1 | RED=1 BLACK=0"

 440    PRINT "------------------+-------+------------------+-----------------"

 450    FOR position = 1 TO 6

 460        colour(position) = (face(position) + ((position - 1) \ 3)) MOD 2

 470        count_spots(position) = 0

 480        group(position) = 0

 490        r(position) = 0

 500        b(position) = 0

 510        sr(position) = 0

 520        sb(position) = 0

 530    NEXT position

 540    g = 0

 550    sg = 0

 560    FOR dice = 1 TO 6

 570        tally_spots = 0

 580        tally_red = 0

 590        tally_black = 0

 600        FOR spots = 1 TO 6

 610            IF face(spots) = dice THEN

 620                tally_spots = tally_spots + 1

 630                IF colour(spots) = 1 THEN

 640                    tally_red = tally_red + 1

 650                ELSEIF colour(spots) = 0 THEN

 660                    tally_black = tally_black + 1

 670                END IF

 680            END IF

 690        NEXT spots

 700        IF tally_spots > 1 THEN

 710            count_spots(tally_spots) = count_spots(tally_spots) + 1

 720            g = g + 1

 730            r(g) = r(g) + tally_red

 740            b(g) = b(g) + tally_black

 750        ELSEIF tally_spots = 1 THEN

 760            count_spots(tally_spots) = count_spots(tally_spots) + 1

 770            sg = sg + 1

 780            sr(sg) = sr(sg) + tally_red

 790            sb(sg) = sb(sg) + tally_black

 800        END IF

 810    NEXT dice

 820    IF count_spots(2) = 1 AND count_spots(3) = 0 AND count_spots(4) = 0 THEN

 830        one_pair = one_pair + 1

 840        IF r(1) = 2 OR b(1) = 2 THEN

 850            xx = xx + 1

 860        ELSE

 870            xy = xy + 1

 880        END IF

 890    ELSEIF count_spots(2) = 2 THEN

 900        two_pair = two_pair + 1

 910        IF r(1) + r(2) = 4 OR b(1) + b(2) = 4 THEN

 920            xx_xx = xx_xx + 1

 930        ELSEIF r(1) + r(2) = 3 OR b(1) + b(2) = 3 THEN

 940            xx_xy = xx_xy + 1

 950        ELSEIF r(1) = 1 AND r(2) = 1 AND b(1) = 1 AND b(2) = 1 THEN

 960            xy_xy = xy_xy + 1

 970        ELSE

 980            xx_yy = xx_yy + 1

 990        END IF

1000    ELSEIF count_spots(2) = 3 THEN

1010        three_pair = three_pair + 1

1020        IF r(1) + r(2) + r(3) = 5 OR b(1) + b(2) + b(3) = 5 THEN

1030            xx_xx_xy = xx_xx_xy + 1

1040        ELSEIF r(1) = 1 AND r(2) = 1 AND r(3) = 1 AND b(1) = 1 AND b(2) = 1 AND b(3) = 1 THEN

1050            xy_xy_xy = xy_xy_xy + 1

1060        ELSE

1070            xx_yy_xy = xx_yy_xy + 1

1080        END IF

1090    ELSEIF count_spots(3) = 1 AND count_spots(2) = 0 THEN

1100        one_trip = one_trip + 1

1110        IF r(1) = 3 OR b(1) = 3 THEN

1120            xxx = xxx + 1

1130        ELSE

1140            xxy = xxy + 1

1150        END IF

1160    ELSEIF count_spots(3) = 1 AND count_spots(2) = 1 THEN

1170        one_trip_one_pair = one_trip_one_pair + 1

1180        IF r(1) + r(2) = 5 OR b(1) + b(2) = 5 THEN

1190            xxx_xx = xxx_xx + 1

1200        ELSEIF r(1) + b(2) = 5 OR r(2) + b(1) = 5 THEN

1210            xxx_yy = xxx_yy + 1

1220        ELSEIF (r(1) = 2 AND b(1) = 1 AND r(2) = 2) OR (r(1) = 1 AND b(1) = 2 AND b(2) = 2) OR (r(1) = 2 AND r(2) = 2 AND b(2) = 1) OR (b(1) = 2 AND r(2) = 1 AND b(2) = 2) THEN

1230            xxy_xx = xxy_xx + 1

1240        ELSEIF (r(1) = 2 AND b(1) = 1 AND b(2) = 2) OR (r(1) = 1 AND b(1) = 2 AND r(2) = 2) OR (r(1) = 2 AND r(2) = 1 AND b(2) = 2) OR (b(1) = 2 AND r(2) = 2 AND b(2) = 1) THEN

1250            xxy_yy = xxy_yy + 1

1260        ELSEIF ((r(1) = 2 AND b(1) = 1) OR (r(1) = 1 AND b(1) = 2)) AND (r(2) = 1 AND b(2) = 1) OR (r(1) = 1 AND b(1) = 1) AND ((r(2) = 2 AND b(2) = 1) OR (r(2) = 1 AND b(2) = 2)) THEN

1270            xxy_xy = xxy_xy + 1

1280        END IF

1290    ELSEIF count_spots(3) = 2 THEN

1300        two_trips = two_trips + 1

1310        IF r(1) + r(2) = 6 OR b(1) + b(2) = 6 THEN

1320            xxx_xxx = xxx_xxx + 1

1330        ELSEIF r(1) + b(2) = 6 OR r(2) + b(1) = 6 THEN

1340            xxx_yyy = xxx_yyy + 1

1350        ELSEIF r(1) < 3 AND r(2) < 3 AND b(1) < 3 AND b(2) < 3 THEN

1360            xxy_xyy = xxy_xyy + 1

1370        END IF

1380    ELSEIF count_spots(4) = 1 AND count_spots(2) = 0 THEN

1390        one_quad = one_quad + 1

1400        IF r(1) = 3 OR b(1) = 3 THEN

1410            xxxy = xxxy + 1

1420        ELSEIF r(1) = 2 AND b(1) = 2 THEN

1430            xxyy = xxyy + 1

1440        END IF

1450    ELSEIF count_spots(4) = 1 AND count_spots(2) = 1 THEN

1460        one_quad_one_pair = one_quad_one_pair + 1

1470        IF (r(1) = 3 AND r(2) = 2) OR (r(1) = 2 AND r(2) = 3) OR (b(1) = 3 AND b(2) = 2) OR (b(1) = 2 AND b(2) = 3) THEN

1480            xxxy_xx = xxxy_xx + 1

1490        ELSEIF (r(1) = 3 AND b(2) = 2) OR (r(1) = 2 AND b(2) = 3) OR (b(1) = 3 AND r(2) = 2) OR (b(1) = 2 AND r(2) = 3) THEN

1500            xxxy_yy = xxxy_yy + 1

1510        ELSEIF ((r(1) = 3 AND b(1) = 1) OR (r(1) = 1 AND b(1) = 3)) AND (r(2) = 1 AND b(2) = 1) OR ((r(2) = 3 AND b(2) = 1) OR (r(2) = 1 AND b(2) = 3)) AND (r(1) = 1 AND b(1) = 1) THEN

1520        xxxy_xy = xxxy_xy + 1

1530        ELSEIF (r(1) = 1 AND r(2) = 2) OR (b(1) = 2 AND b(2) = 2) THEN

1540            xxyy_xx = xxyy_xx + 1

1550        ELSEIF (r(1) = 2 AND b(1) = 2 AND r(2) = 1 AND b(2) = 1) OR (r(1) = 1 AND b(1) = 1 AND r(2) = 2 AND b(2) = 2) THEN

1560            xxyy_xy = xxyy_xy + 1

1570        END IF

1580    ELSEIF count_spots(5) = 1 THEN

1590        one_quint = one_quint + 1

1600    ELSEIF count_spots(6) = 1 THEN

1610        one_sextuple = one_sextuple + 1

1620    ELSEIF count_spots(1) = 6 THEN

1630        one_straight = one_straight + 1

1640        IF sr(1) + sr(2) + sr(3) + sr(4) + sr(5) + sr(6) = 6 OR sb(1) + sb(2) + sb(3) + sb(4) + sb(5) + sb(6) = 6 THEN

1650            xxxxxx = xxxxxx + 1

1660        ELSEIF sr(1) + sr(2) + sr(3) + sr(4) + sr(5) + sr(6) = 4 OR sb(1) + sb(2) + sb(3) + sb(4) + sb(5) + sb(6) = 4 THEN

1670            xxxxyy = xxxxyy + 1

1680        END IF

1690    END IF

1700    LET roll = roll + 1

1710    FOR throw_spots = 1 TO 6

1720        COLOR colour(throw_spots): PRINT face(throw_spots);

1730    NEXT throw_spots

1740    COLOR 2: PRINT "|";

1750    IF roll < 10 THEN

1760        PRINT "    "; roll;

1770    ELSEIF roll < 100 THEN

1780        PRINT "   "; roll;

1790    ELSEIF roll < 1000 THEN

1800        PRINT "  "; roll;

1810    ELSEIF roll < 10000 THEN

1820        PRINT " "; roll;

1830    ELSE

1840        PRINT roll;

1850    END IF

1860    PRINT "|"; count_spots(6); count_spots(5); count_spots(4); count_spots(3); count_spots(2); count_spots(1); "|";

1870    FOR throw_colour = 1 TO 6

1880        COLOR colour(throw_colour): PRINT colour(throw_colour);

1890    NEXT throw_colour

1900    COLOR 2: PRINT " "

1910    face(6) = face(6) + 1

1920    IF face(6) < 7 THEN

1930        GOTO 450

1940    ELSE

1950        face(6) = 1

1960        face(5) = face(5) + 1

1970        IF face(5) < 7 THEN

1980            GOTO 450

1990        ELSE

2000            face(5) = 1

2010            face(4) = face(4) + 1

2020            IF face(4) < 7 THEN

2030                GOTO 450

2040            ELSE

2050                face(4) = 1

2060                face(3) = face(3) + 1

2070                IF face(3) < 7 THEN

2080                    GOTO 450

2090                ELSE

2100                    face(3) = 1

2110                    face(2) = face(2) + 1

2120                    IF face(2) < 7 THEN

2130                        GOTO 450

2140                    ELSE

2150                        face(2) = 1

2160                        face(1) = face(1) + 1

2170                        IF face(1) < 7 THEN

2180                            GOTO 450

2190                        ELSE

2200                            face(1) = 1

2210                        END IF

2220                    END IF

2230                END IF

2240            END IF

2250        END IF

2260    END IF

2270    PRINT "TOTAL NUMBER OF HANDS CONTAINING"

2280    PRINT "1 PAIR:", one_pair; "- XX:"; xx; "- XY:"; xy

2290    PRINT "2 PAIRS:", two_pair; "- XX XX:"; xx_xx; "- XX YY:"; xx_yy; "- XX XY:"; xx_xy; "- XY XY:"; xy_xy

2300    PRINT "3 PAIRS:", " "; three_pair; "- XX XX XY:"; xx_xx_xy; "- XX YY XY:"; xx_yy_xy; "- XY XY XY:"; xy_xy_xy

2310    PRINT "1 TRIP:", " "; one_trip; "- XXX:"; xxx; "- XXY:"; xxy

2320    PRINT "1 TRIP/1 PAIR:"; " "; one_trip_one_pair; "- XXX XX:"; xxx_xx; "- XXX YY:"; xxx_yy; "- XXY XX:"; xxy_xx

2330    PRINT " ", "         "; "XXY YY:"; xxy_yy; "- XXY XY:"; xxy_xy

2340    PRINT "2 TRIPS:", "  "; two_trips; "- XXX XXX:"; xxx_xxx; "- XXX YYY"; xxx_yyy; "- XXY XYY:"; xxy_xyy

2350    PRINT "1 QUAD:", " "; one_quad; "- XXXY:"; xxxy; "- XXYY"; xxyy

2360    PRINT "1 QUAD/1 PAIR:"; "  "; one_quad_one_pair; "- XXXY XX:"; xxxy_xx; "- XXXY YY:"; xxxy_yy; "- XXYY_XX:"; xxyy_xx; "- XXYY XY:"; xxyy_xy

2370    PRINT "1 QUINT:", "  "; one_quint; "- XXXYY:"; one_quint

2380    PRINT "1 SEXTUPLE:", "    "; one_sextuple; "- XXXYYY:"; one_sextuple

2390    PRINT "1 STRAIGHT:", "  "; one_straight; "- XXXXXX:"; xxxxxx; "- XXXXYY:"; xxxxyy

2400    end_time = TIMER

2410    PRINT "This program took "; end_time - start_time; " seconds to execute"

2420    FOR reset_COLOR = 1 TO 256

2430        OUT &H3C7, reset_COLOR

2440        R% = INP(&H3C9)

2450        G% = INP(&H3C9)

2460        B% = INP(&H3C9)

2470    NEXT reset_COLOR

2480    END

答案 2 :(得分:0)

此代码片段计算6个模具中的4个或更多的对,三元组和卷数:

DIM dice(1 TO 6) AS INTEGER
RANDOMIZE TIMER
FOR l = 1 TO 6
    dice(l) = INT(RND * 6 + 1)
    PRINT dice(l);
NEXT
PRINT
FOR l = 1 TO 6
    c = 0
    FOR m = 1 TO 6
        IF dice(m) = l THEN
            c = c + 1
        END IF
    NEXT
    IF c >= 2 THEN
        PRINT "there are"; c; "dice of value"; l
    END IF
NEXT
END