C語言數組三

定義和初始化數組

 問題

刪除數組中的某個元素,並將後續元素頂上來。

步驟

實現此案例需要按照如下步驟進行。

步驟一:定義和初始化數組

代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int num<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入要刪除的元素:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>num<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> num<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li></ol>

定義一個整型數組array,包含10個元素。並使用循環對元素賦值的方式完成對數組array的初始化。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int num<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入要刪除的元素:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>num<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>

首先,定義一個整型變量num,用於存儲輸入的一個要刪除的元素。

然後,使用函數printf提示輸入的一個要刪除的元素。

最後,使用函數scanf輸入的要刪除的元素到變量num中。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> num<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>

設置一個循環,逐個元素array[i]與輸入的要刪除的元素num對比,找到要刪除的元素在數組中的位置後退出循環。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li></ol>

將找到的要刪除元素的位置的下一個元素賦值到其中,實現刪除的功能。設置循環,依次賦值,實現將後續元素頂上來的功能。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>

設置一個循環,逐個打印數組中剩餘的元素,驗證刪除效果。

 完整代碼

本案例的完整代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int num<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入要刪除的元素:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>num<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> num<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

數組的長度

 問題

刪除數組中的某個元素,並將後續元素頂上來。

 步驟

實現此案例需要按照如下步驟進行。

步驟一:數組的長度

代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span style="color: rgb(51, 204, 0);"><span class="sh_function">sizeof</span><span class="sh_symbol" style="font-weight: bold;">(</span>array<span class="sh_symbol" style="font-weight: bold;">)</span><span class="sh_normal"> </span><span class="sh_symbol" style="font-weight: bold;">/</span> <span class="sh_function">sizeof</span><span class="sh_symbol" style="font-weight: bold;">(</span>array<span class="sh_symbol" style="font-weight: bold;">[</span><span class="sh_number" style="font-weight: bold;">0</span><span class="sh_symbol" style="font-weight: bold;">])</span></span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_normal"> </span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">/</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li></ol>

定義一個整型數組array,包含10個元素。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_normal"> </span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">/</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li></ol>

使用循環對元素賦值的方式完成對數組array的初始化。數組的長度是使用數組所佔的字節數sizeof(array)除以數組第一個元素所佔的字節數sizeof(array[0])得到的。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_normal"> </span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">/</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>

設置循環逐個輸出數組的每一個元素。

完整代碼

本案例的完整代碼如下所示:

<ol class="snippet-num" style="color: rgb(54, 46, 43); line-height: 25.2px; margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_normal"> </span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">/</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_normal"> </span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">/</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol><div style="color: rgb(54, 46, 43); line-height: 25.2px;"><span style="color: rgb(255, 0, 128);"><strong>
</strong></span></div><div style="color: rgb(54, 46, 43); line-height: 25.2px;"><span style="color: rgb(255, 0, 128);"><strong>
</strong></span></div><div style="color: rgb(54, 46, 43); line-height: 25.2px;"><span style="color: rgb(255, 0, 128);"><strong></strong></span><h2 style="margin: 16.3px 0px 0px; padding: 0px; font-size: 20px; font-family: 微軟雅黑, 'Times New Roman';"><a target=_blank name="t8" style="color: rgb(106, 57, 6);"></a>1 可變長數組的使用</h2><h3 style="margin: 10px 0px; padding: 0px; font-size: 18px; font-family: 微軟雅黑, 'Times New Roman';"><a target=_blank name="t9" style="color: rgb(106, 57, 6);"></a>1.1 問題</h3><p style="margin: 3px; padding-right: 2px; padding-left: 2px; text-indent: 21pt; font-size: 15px; font-family: 微軟雅黑, 'Times New Roman';">定義一個變量n,然後用n定義和使用可變長數組。</p><h3 style="margin: 10px 0px; padding: 0px; font-size: 18px; font-family: 微軟雅黑, 'Times New Roman';"><a target=_blank name="t10" style="color: rgb(106, 57, 6);"></a>1.2 步驟</h3><p style="margin: 3px; padding-right: 2px; padding-left: 2px; text-indent: 21pt; font-size: 15px; font-family: 微軟雅黑, 'Times New Roman';">實現此案例需要按照如下步驟進行。</p><p class="number" style="margin: 8px 3px; padding-right: 2px; padding-left: 2px; text-indent: 21pt; font-size: 15px; font-weight: bold; font-family: 微軟雅黑, 'Times New Roman';">步驟一:可變長數組的使用</p><p style="margin: 3px; padding-right: 2px; padding-left: 2px; text-indent: 21pt; font-size: 15px; font-family: 微軟雅黑, 'Times New Roman';">代碼如下所示:</p><div class="snippet-container" style="font-family: 微軟雅黑, 'Times New Roman';"><div class="sh_acid snippet-wrap" style="padding: 0px; margin: 0px; border: 0px none; position: relative; background: none;"><div class="snippet-menu sh_sourceCode" style="position: absolute; top: 10px; right: 15px; font-size: 0.9em; z-index: 1; background-color: transparent;"></div><pre class="code sh_javascript snippet-formatted sh_sourceCode" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-left: 40px; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; background-color: rgb(238, 238, 238);"><ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入數組長度:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入數組長度:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>

首先,定義一個整型變量n,用於存儲輸入的數組長度。

然後,使用函數printf提示輸入數組長度。

最後,使用函數scanf輸入一個數組長度到變量n中。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li></ol>

定義一個整型數組,數組的長度由整型變量n通過函數scanf動態輸入決定。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>

將數組array中的元素逐個賦初值。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>

逐個輸出數組array中的元素。

1.3 完整代碼

本案例的完整代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入數組長度:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

2 可變長數組的使用(續1)

2.1 問題

輸入某班同學的考試成績,並保存在數組中,然後計算總分、平均分、最高分和最低分。注意: 需要先輸入學生人數,使用可變長數組。

2.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:可變長數組的使用(續1)

代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入班級學生人數:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int score<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"第%d個成績:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>score<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+=</span> score<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"總分:%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> sum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"平均分:%f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_normal">sum </span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">/</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入班級學生人數:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>

首先,定義一個整型變量n,用於存儲輸入的學生人數。

然後,使用函數printf提示輸入學生人數。

最後,使用函數scanf輸入一個學生人數到變量n中。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int score<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li></ol>

定義一個整型數組,數組的長度由整型變量n通過函數scanf動態輸入決定。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"第%d個成績:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>score<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

設置循環,逐個輸入每個學生的成績。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>

定義整型變量sum,並初始化爲0,用於存儲班級學生總成績。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+=</span> score<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li></ol>

設置循環,對每個學生的成績進行累加,計算總成績。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"總分:%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> sum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"平均分:%f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_normal">sum </span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">/</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>

使用函數printf輸出班級學生總成績和平均分。

2.3 完整代碼

本案例的完整代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入班級學生人數:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int score<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"第%d個成績:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>score<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+=</span> score<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"總分:%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> sum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"平均分:%f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_normal">sum </span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">/</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

3 二維數組的使用

3.1 問題

聲明和初始化一個二維數組,並打印所有元素的值。

3.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:二維數組的使用

代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">11</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">20</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">21</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">22</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">23</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">30</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">31</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">32</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">33</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">11</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">20</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">21</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">22</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">23</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">30</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">31</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">32</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">33</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>

定義了一個二維數組array,該數組的第一維長度爲3,第二位長度爲4,共有12個數組元素。定義完成之後,對數組array進行了初始化。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

設置雙重循環,打印輸出二維數組內容,外循環用於控制數組的第一維,內層循環用於控制數組的第二維。

3.3 完整代碼

本案例的完整代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">11</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">20</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">21</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">22</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">23</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">30</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">31</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">32</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">33</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

4 二維數組的使用(續1)

4.1 問題

定義兩個3*3的二維數組,用第一個的行乘以第二個的列,得到新的二維數組並打印。

4.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:二維數組的使用(續1)

代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">6</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">7</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">8</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">11</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">14</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">15</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">16</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">17</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">18</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*=</span> array1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">6</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">7</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">8</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">11</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">14</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">15</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">16</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">17</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">18</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>

定義了兩個二維數組array和array1,數組的第一維長度爲3,第二位長度也爲3,共有9個數組元素。定義完成之後,對數組array和array1進行了初始化。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*=</span> array1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li></ol>

設置雙重循環,用數組array的行乘以數組array1的列,外循環分別用於控制數組array的第一維和數組array1的第二維,內層循環分別用於控制數組array的第二維和數組array1的第一維。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

設置雙重循環,打印輸出二維數組array的內容,外循環用於控制數組的第一維,內層循環用於控制數組的第二維。

4.3 完整代碼

本案例的完整代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">6</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">7</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">8</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int array1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">11</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">14</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">15</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">16</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">17</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">18</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*=</span> array1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">][</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

5 二維數組的使用(續2)

5.1 問題

實現隨機發牌(不帶大王和小王)。

程序負責發一副標準紙牌,每張標準紙牌都在一個花色(梅花C、方塊D、紅桃H或黑桃S)和一個等級(2、3、4、5、6、7、8、9、T、J、Q、K、A) 。程序需要用戶輸入手裏應該握有幾張牌。

5.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:定義數組。

數組suit,用於保存撲克牌的花色S、H、D、C。

數組rank,用於保存撲克牌的13張牌A、2、3、4、5、6、7、8、9、T、J、Q、K。

代碼如下:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>time<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'S'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'H'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'F'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'C'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'A'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'2'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'3'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'4'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'5'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'6'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'7'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'8'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'9'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'T'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'J'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'Q'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'K'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

步驟二:從控制檯輸入你要的張數。

需要定義一個變量count,用於保存輸入的張數。

代碼如下:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>time<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'S'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'H'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'F'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'C'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'A'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'2'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'3'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'4'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'5'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'6'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'7'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'8'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'9'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'T'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'J'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'Q'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'K'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入你要的張數:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

步驟三:隨機生成一張牌。

需要定義一個變量s,用於保存隨機生成的一種花色。

需要定義一個變量r,用於保存隨機生成的一種牌型。

代碼如下:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>time<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'S'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'H'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'F'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'C'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'A'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'2'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'3'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'4'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'5'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'6'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'7'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'8'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'9'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'T'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'J'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'Q'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'K'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入你要的張數:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">srand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">((</span>unsigned<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        int s <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">rand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">%</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        int r <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">rand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">%</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%c%c "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">],</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

步驟四:構建循環,生成你要的張數。

代碼如下:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>time<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'S'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'H'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'F'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'C'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'A'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'2'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'3'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'4'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'5'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'6'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'7'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'8'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'9'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'T'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'J'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'Q'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'K'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入你要的張數:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">srand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">((</span>unsigned<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        int s <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">rand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">%</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        int r <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">rand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">%</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%c%c "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">],</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

步驟五:濾掉重複的牌。

代碼如下:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>time<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'S'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'H'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'F'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'C'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'A'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'2'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'3'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'4'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'5'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'6'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'7'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'8'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'9'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'T'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'J'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'Q'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'K'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入你要的張數:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char oldS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char oldR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">srand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">((</span>unsigned<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        int s <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">rand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">%</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        int r <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">rand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">%</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> k <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>oldS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&&</span> oldR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">])</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">                i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">--;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">                <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> k<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%c%c "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">],</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            oldS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            oldR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char oldS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char oldR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li></ol>

定義兩個數組用於保存已生成的花色和牌型。

上述代碼中,以下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>oldS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&&</span> oldR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">])</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">                i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">--;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">                <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

在循環體內,嵌入一個內層循環,用於逐張與已經生成的牌進行對比,如果相同,則重新生成一張牌,如果不相同,則使用如下代碼:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> k<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%c%c "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">],</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            oldS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            oldR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>

則將新生成的牌保存到數組中。

5.3 完整代碼

本案例的完整代碼如下所示:

<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>time<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'S'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'H'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'F'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'C'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_string" style="color: rgb(166, 133, 0);">'A'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'2'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'3'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'4'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'5'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'6'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'7'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'8'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'9'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'T'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'J'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'Q'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'K'</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    int count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"請輸入你要的張數:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char oldS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    char oldR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">srand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">((</span>unsigned<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span><span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> count<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        int s <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">rand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">%</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">4</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        int r <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">rand</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">%</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">13</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        int j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> k <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(;</span> j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>oldS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&&</span> oldR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>j<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">])</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">                i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">--;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">                <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>j <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> k<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%c%c "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">],</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            oldS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> suit<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>s<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">            oldR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> rank<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>r<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>





發佈了27 篇原創文章 · 獲贊 19 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章