{"id":119,"date":"2026-03-29T12:36:01","date_gmt":"2026-03-29T04:36:01","guid":{"rendered":"https:\/\/www.becksai.cn\/?p=119"},"modified":"2026-03-29T12:58:50","modified_gmt":"2026-03-29T04:58:50","slug":"%e9%ab%98%e7%a8%8b%e4%bd%9c%e4%b8%9a%e7%95%99%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/www.becksai.cn\/?p=119","title":{"rendered":"\u9ad8\u7a0b\u4f5c\u4e1a\u7559\u6863"},"content":{"rendered":"<p>#### 9.23\uff08\u6d17\u724c\u548c\u53d1\u724c\uff09<br \/>\n\u7f16\u5199\u4e00\u4e2a\u6d17\u724c\u548c\u53d1\u724c\u7684\u7a0b\u5e8f\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u5305\u542b\u7c7b Card\u3001\u7c7b DeckOfCards \u548c\u4e00\u4e2a\u9a71\u52a8\u7a0b\u5e8f\u3002<br \/>\n###### \u7c7b Card \u6709\uff1a<br \/>\n* a) int \u578b\u7684\u6570\u636e\u6210\u5458 face \u548c suit\u3002<br \/>\n* b) \u63a5\u6536\u4e24\u4e2a int \u578b\u7684\u4ee3\u8868\u9762\u503c\u548c\u82b1\u8272\u7684\u6784\u9020\u51fd\u6570\uff0c\u7528\u4e8e\u521d\u59cb\u5316\u6570\u636e\u6210\u5458\u3002<br \/>\n* c) \u4e24\u4e2a string \u7c7b\u578b\u7684 static \u6570\u7ec4\uff0c\u4ee3\u8868\u9762\u503c\u548c\u82b1\u8272\u3002<br \/>\n* d) \u4e00\u4e2a toString \u51fd\u6570\uff0c\u8fd4\u56de\u5f62\u5f0f\u4e3a \u201cface of suit\u201d \u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4f7f\u7528 + \u8fd0\u7b97\u7b26\u8fde\u63a5\u5b57\u7b26\u4e32\u3002<br \/>\n###### \u7c7b DeckOfCards \u6709\uff1a<br \/>\n* a) \u4e00\u4e2a\u540d\u4e3a deck \u7684 Card \u7c7b\u578b\u7684 vector\uff0c\u7528\u6765\u5b58\u50a8 Card\u3002<br \/>\n* b) \u4ee3\u8868\u4e0b\u4e00\u4e2a\u5c06\u8981\u5904\u7406\u7684\u724c\u7684\u6574\u578b\u503c currentCard\u3002<br \/>\n* c) \u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u7528\u4e8e\u521d\u59cb\u5316 deck \u4e2d\u7684 Card\u3002\u6784\u9020\u51fd\u6570\u4f7f\u7528 vector \u7684 push_back \u51fd\u6570\u5c06\u4ea7\u751f\u7684\u724c\u6dfb\u52a0\u5230 vector \u672b\u5c3e\uff0c\u8be5\u8fc7\u7a0b\u9700\u5bf9 deck \u4e2d\u7684 52 \u5f20\u724c\u90fd\u6267\u884c\u4e00\u904d\u3002<br \/>\n* d) \u51fd\u6570 shuffle \u7528\u4e8e\u6d17\u724c\u3002\u6d17\u724c\u7b97\u6cd5\u5728 vector \u4e2d\u53cd\u590d\u6267\u884c\uff1a\u5bf9\u6bcf\u5f20\u724c\uff0c\u968f\u673a\u9009\u53d6\u53e6\u4e00\u5f20\u724c\u5e76\u4ea4\u6362\u4e24\u8005\u4f4d\u7f6e\u3002<br \/>\n* e) dealCard \u51fd\u6570\u8fd4\u56de\u4e0b\u4e00\u5f20\u724c\u3002<br \/>\n* f) moreCards \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a bool \u503c\uff0c\u4ee3\u8868\u662f\u5426\u8fd8\u6709\u724c\u8981\u5904\u7406\u3002<br \/>\n\u9a71\u52a8\u7a0b\u5e8f\u4ea7\u751f DeckOfCards \u5bf9\u8c61\uff0c\u6d17\u724c\uff0c\u7136\u540e\u53d1\u724c\u3002<br \/>\n#### 9.24\uff08\u6d17\u724c\u548c\u53d1\u724c\uff09<br \/>\n\u4fee\u6539\u7ec3\u4e60\u9898 9.23 \u4e2d\u7684\u7a0b\u5e8f\uff0c\u4f7f\u53d1\u724c\u51fd\u6570\u53d1\u4e00\u624b 5 \u5f20\u724c\uff0c\u7136\u540e\u7f16\u5199\u51fd\u6570\u5b8c\u6210\u4e0b\u5217\u4efb\u52a1\uff1a<br \/>\n* a) \u786e\u5b9a\u624b\u4e0a\u662f\u5426\u6709\u4e00\u526f\u5bf9\u5b50\u3002<br \/>\n* b) \u786e\u5b9a\u624b\u4e0a\u662f\u5426\u6709\u4e24\u526f\u5bf9\u5b50\u3002<br \/>\n* c) \u786e\u5b9a\u624b\u4e0a\u662f\u5426\u6709 3 \u5f20\u540c\u53f7\u724c\uff08\u5982 3 \u5f20 J\uff09\u3002<br \/>\n* d) \u786e\u5b9a\u624b\u4e0a\u662f\u5426\u6709 4 \u5f20\u540c\u53f7\u724c\uff08\u5982 4 \u5f20 A\uff09\u3002<br \/>\n* e) \u786e\u5b9a\u624b\u4e0a\u662f\u5426\u6709\u540c\u82b1\uff08\u5373 5 \u5f20\u724c\u82b1\u8272\u76f8\u540c\uff09\u3002<br \/>\n* f) \u786e\u5b9a\u662f\u5426\u6709\u540c\u987a\uff08\u5373 5 \u5f20\u9762\u503c\u8fde\u7eed\u7684\u724c\uff09\u3002<\/p>\n<p>```cpp<br \/>\n#include <iostream><br \/>\n#include <string><br \/>\n#include <vector><br \/>\n#include <algorithm><br \/>\n#include <cstdlib><br \/>\n#include <ctime><\/p>\n<p>using namespace std;<\/p>\n<p>class Card<br \/>\n{<br \/>\npublic:<br \/>\n    int face;<br \/>\n    int suit;<br \/>\n    static string faces[];<br \/>\n    static string suits[];<\/p>\n<p>private:<\/p>\n<p>public:<br \/>\n    Card(int f, int s)<br \/>\n    {<br \/>\n        face = f;<br \/>\n        suit = s;<br \/>\n    }<\/p>\n<p>    string toString()<br \/>\n    {<br \/>\n        return faces[face] + \"of\" + suits[suit];<br \/>\n    }<\/p>\n<p>private:<br \/>\n};<\/p>\n<p>string Card::faces[] = { \"Ace\", \"Deuce\", \"Three\", \"Four\", \"Five\", \"Six\",<br \/>\n                        \"Seven\", \"Eight\", \"Nine\", \"Ten\", \"Jack\", \"Queen\", \"King\" };<br \/>\nstring Card::suits[] = { \"Hearts\", \"Diamonds\", \"Clubs\", \"Spades\" };<\/p>\n<p>class DeckOfCards<br \/>\n{<br \/>\npublic:<\/p>\n<p>private:<br \/>\n    vector<Card> deck;<br \/>\n    int currentCard;<\/p>\n<p>public:<br \/>\n    DeckOfCards()<br \/>\n    {<br \/>\n        currentCard = 0;<br \/>\n        for (int suit = 0; suit < 4; ++suit)\n        {\n            for (int face = 0; face < 13; ++face)\n            {\n                deck.push_back(Card(face, suit));\n            }\n        }\n    }\n\n    void shuffle()\n    {\n        for (size_t i = 0; i < deck.size(); ++i)\n        {\n            int j = rand() % deck.size();\n            swap(deck[i], deck[j]);\n        }\n\n    }\n    \n\n    Card dealCard()\n    {\n        return deck[currentCard++];\n    }\n\n    bool moreCards() { return currentCard <= 51; }\n};\n\nint numofPairs(const vector<Card>& d)<br \/>\n{<br \/>\n    int cnt = 0;<br \/>\n    int count[13] = { 0 };<br \/>\n    for (int i = 0; i < 5; i++)\n    {\n        count[d[i].face]++;\n    }\n    for (int i = 0; i < 13; i++)\n    {\n        if (count[i] == 2)\n            cnt++;\n    }\n    return cnt;\n}\n\nint numofTriples(const vector<Card>& d)<br \/>\n{<br \/>\n    int cnt = 0;<br \/>\n    int count[13] = { 0 };<br \/>\n    for (int i = 0; i < 5; i++)\n    {\n        count[d[i].face]++;\n    }\n    for (int i = 0; i < 13; i++)\n    {\n        if (count[i] == 3)\n            cnt++;\n    }\n    return cnt;\n}\n\nint numofFours(const vector<Card>& d)<br \/>\n{<br \/>\n    int cnt = 0;<br \/>\n    int count[13] = { 0 };<br \/>\n    for (int i = 0; i < 5; i++)\n    {\n        count[d[i].face]++;\n    }\n    for (int i = 0; i < 13; i++)\n    {\n        if (count[i] == 4)\n            cnt++;\n    }\n    return cnt;\n}\n\nint numofSameColor(const vector<Card>& d)<br \/>\n{<br \/>\n    int cnt = 0;<br \/>\n    int count[4] = { 0 };<br \/>\n    for (int i = 0; i < 5; i++)\n    {\n        count[d[i].suit]++;\n    }\n    for (int i = 0; i < 4; i++)\n    {\n        if (count[i] == 5)\n            cnt++;\n    }\n    return cnt;\n}\n\nbool isStraight(const vector<Card>& d)<br \/>\n{<br \/>\n    int faces[5];<br \/>\n    for (int i = 0; i < 5; i++)\n    {\n        faces[i] = d[i].face;\n    }\n    sort(faces, faces + 5);\n    for (int i = 1; i < 5; i++)\n    {\n        if (faces[i] - faces[i - 1] != 1)\n            return false;\n\n    }\n    return true;\n        \n\n}\n\nint main()\n{\n    srand(time(0));\n\n    DeckOfCards myDeck;\n    myDeck.shuffle();\n\n    vector<Card> myHand;<br \/>\n    for (int i = 0; i < 5; i++)\n    {\n        myHand.push_back(myDeck.dealCard());\n    }\n    for (int i = 0; i < 5; i++)\n    {\n        cout << myHand[i].toString() << endl;\n\n    }\n    cout << \"\u624b\u4e0a\u6709\"<<numofPairs(myHand)<<\"\u526f\u5bf9\u5b50\" << endl;\n    if (numofTriples(myHand))\n        cout << \"\u624b\u4e0a\u6709\u4e09\u5f20\u540c\u53f7\u724c\" << endl;\n    else\n        cout << \"\u624b\u4e0a\u6ca1\u6709\u4e09\u5f20\u540c\u53f7\u724c\" << endl;\n    if (numofFours(myHand))\n        cout << \"\u624b\u4e0a\u6709\u56db\u5f20\u540c\u53f7\u724c\" << endl;\n    else\n        cout << \"\u624b\u4e0a\u6ca1\u6709\u56db\u5f20\u540c\u53f7\u724c\" << endl;\n    if (numofSameColor(myHand))\n        cout << \"\u624b\u4e0a\u6709\u540c\u82b1\" << endl;\n    else\n        cout << \"\u624b\u4e0a\u6ca1\u6709\u540c\u82b1\" << endl;\n    if (isStraight(myHand))\n        cout << \"\u624b\u4e0a\u6709\u987a\u5b50\" << endl;\n    else\n        cout << \"\u624b\u4e0a\u6ca1\u6709\u987a\u5b50\" << endl;\n\n}\n```\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#### 9.23\uff08\u6d17\u724c\u548c\u53d1\u724c\uff09 \u7f16\u5199\u4e00\u4e2a\u6d17\u724c\u548c\u53d1\u724c\u7684\u7a0b\u5e8f\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u5305\u542b\u7c7b Card\u3001\u7c7b DeckOfCards \u548c\u4e00\u4e2a\u9a71\u52a8\u7a0b\u5e8f\u3002 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[7],"tags":[],"class_list":["post-119","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.becksai.cn\/index.php?rest_route=\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.becksai.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.becksai.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.becksai.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.becksai.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=119"}],"version-history":[{"count":5,"href":"https:\/\/www.becksai.cn\/index.php?rest_route=\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":123,"href":"https:\/\/www.becksai.cn\/index.php?rest_route=\/wp\/v2\/posts\/119\/revisions\/123"}],"wp:attachment":[{"href":"https:\/\/www.becksai.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.becksai.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.becksai.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}