C语言数组三

来源:互联网 发布:雇一次网络水军多少钱 编辑:程序博客网 时间:2024/05/22 07:51

定义和初始化数组

 问题

删除数组中的某个元素,并将后续元素顶上来。

步骤

实现此案例需要按照如下步骤进行。

步骤一:定义和初始化数组

代码如下所示:

<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>




0 0