🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
C++如何跳出多层循环 虽然说语言是互通的,各种计算机语言的基本逻辑结构是类似的,但不同的语言之间还是有一些差别的。如循环中的break,在java中可以后面带标志:**break [flag]**(flag为要结束的循环层数),但在C++中没有这个标志。 那C++中如何跳出多重循环呢? # 以问题为例: <table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="568" valign="top" style="width:500pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"><strong>问题主题:抽签</strong></span><span style="font-size:14pt; font-family:宋体"/></p></td></tr><tr><td width="568" valign="top" style="width:426.1pt; padding:0pt 5.4pt; border-left-width:0.5pt; border-style:none solid solid; border-left-color:rgb(0,0,0); border-right-width:0.5pt; border-right-color:rgb(0,0,0); border-bottom-width:0.5pt; border-bottom-color:rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"><strong>问题描述:</strong></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">将写有数字的<span style="font-family:Times New Roman">numOfCard</span><span style="font-family:宋体">个卡片放入口袋中,从口袋中抽取</span><span style="font-family:Times New Roman">4</span><span style="font-family:宋体">次卡片,每次记录卡片的数字后将其放回口袋中。设卡片上写的数字为</span><span style="font-family:Times New Roman">k</span></span><span style="font-size:14pt; font-family:宋体; vertical-align:sub">1</span><span style="font-size:14pt; font-family:宋体">、<span style="font-family:Times New Roman">k</span></span><span style="font-size:14pt; font-family:宋体; vertical-align:sub">2</span><span style="font-size:14pt; font-family:宋体">、<span style="font-family:Times New Roman">k</span></span><span style="font-size:14pt; font-family:宋体; vertical-align:sub">3</span><span style="font-size:14pt; font-family:宋体">...k</span><span style="font-size:14pt; font-family:宋体; vertical-align:sub">n</span><span style="font-size:14pt; font-family:宋体">,</span><span style="font-size:14pt; font-family:宋体">如果这<span style="font-family:Times New Roman">4</span><span style="font-family:宋体">个数字的和为</span><span style="font-family:Times New Roman">sum</span><span style="font-family:宋体">,则输出</span></span><span style="font-size:14pt">”</span><span style="font-size:14pt; font-family:宋体">Yes</span><span style="font-size:14pt">”</span><span style="font-size:14pt; font-family:宋体">,否则输出</span><span style="font-size:14pt">”</span><span style="font-size:14pt; font-family:宋体">No</span><span style="font-size:14pt">”</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"><strong>限制条件:</strong></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">1&lt;=numOfCard&lt;=50</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">1&lt;=sum&lt;=10</span><span style="font-size:14pt; font-family:宋体; vertical-align:super">8</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">1&lt;=k</span><span style="font-size:14pt; font-family:宋体; vertical-align:sub">i</span><span style="font-size:14pt; font-family:宋体">&lt;=10</span><span style="font-size:14pt; font-family:宋体; vertical-align:super">8</span><span style="font-size:14pt; font-family:宋体; vertical-align:super"/></p></td></tr><tr><td width="568" valign="top" style="width:426.1pt; padding:0pt 5.4pt; border-left-width:0.5pt; border-style:none solid solid; border-left-color:rgb(0,0,0); border-right-width:0.5pt; border-right-color:rgb(0,0,0); border-bottom-width:0.5pt; border-bottom-color:rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"><strong>样例<span style="font-family:Times New Roman">1</span><span style="font-family:宋体">:</span></strong></span><span style="font-size:14pt; font-family:宋体"><strong/></span></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">输入</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">numOfCard = 3</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">Sum = 10</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">K = {1, 3, 5}</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">输出</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">Yes</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"><strong>样例<span style="font-family:Times New Roman">2</span><span style="font-family:宋体">:</span></strong></span><span style="font-size:14pt; font-family:宋体"><strong/></span></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">输入</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">numOfCard = 3</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">Sum = 9</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">K = {1, 3, 5}</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">输出</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'; text-indent:28pt"><span style="font-size:14pt; font-family:宋体">No</span><span style="font-size:14pt; font-family:宋体"/></p></td></tr></tbody></table> # 可能的解决方法 你可能会将程序写成这样: <table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="568" valign="top" style="width:500pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">void  drawLots() {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   //<span style="font-family:宋体">从标准输入读入</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int numOfCard, sum;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int k[MAX_N];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cout&lt;&lt;"<span style="font-family:宋体">输入</span><span style="font-family:Vijaya">numOfCard</span><span style="font-family:宋体">和</span><span style="font-family:Vijaya">sum"&lt;&lt;endl;</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cin&gt;&gt;numOfCard&gt;&gt;sum; </span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cout&lt;&lt;"<span style="font-family:宋体">请输入这</span><span style="font-family:Vijaya">sum</span><span style="font-family:宋体">张卡片的数字</span><span style="font-family:Vijaya">"&lt;&lt;endl;</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   for(int i=0; i&lt;numOfCard; i++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">cin&gt;&gt;k[i];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   bool result = false;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int _sum = 0;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   for(int a = 0; a &lt; numOfCard; a ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">      for(int b = 0; b &lt; numOfCard; b ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">          for(int c = 0; c &lt; numOfCard; c++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              for(int d = 0; d &lt; numOfCard; d ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              </span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">_sum = k[a] + k[b] + k[c] + k[d];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                  if(_sum == sum) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">                    result = true;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                    break;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                  }</span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"> </span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">          }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">      }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   if(result){</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   </span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">cout&lt;&lt;"Yes"&lt;&lt;endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   } else</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">    cout&lt;&lt;"No"&lt;&lt;endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">}</span><span style="font-size:14pt; font-family:宋体"/></p></td></tr></tbody></table> # 出现的问题 **但是上面的break并没有结束循环(即没有跳出多层循环),而只是跳出了本层循环。**如果你不明白为什么会这样,可以参考我之前写的一篇文章:[再探java基础——break和continue的用法](http://blog.csdn.net/luoweifu/article/details/10756017) 或者,你可以将程序稍微改动一下来验证: <table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="568" valign="top" style="width:500pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">void  drawLots() {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   //<span style="font-family:宋体">从标准输入读入</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int numOfCard, sum;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int k[MAX_N];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cout&lt;&lt;"<span style="font-family:宋体">输入</span><span style="font-family:Vijaya">numOfCard</span><span style="font-family:宋体">和</span><span style="font-family:Vijaya">sum"&lt;&lt;endl;</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cin&gt;&gt;numOfCard&gt;&gt;sum; </span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cout&lt;&lt;"<span style="font-family:宋体">请输入这</span><span style="font-family:Vijaya">sum</span><span style="font-family:宋体">张卡片的数字</span><span style="font-family:Vijaya">"&lt;&lt;endl;</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   for(int i=0; i&lt;numOfCard; i++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">cin&gt;&gt;k[i];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   bool result = false;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int _sum = 0;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   for(int a = 0; a &lt; numOfCard; a ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">      for(int b = 0; b &lt; numOfCard; b ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">          for(int c = 0; c &lt; numOfCard; c++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              for(int d = 0; d &lt; numOfCard; d ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              </span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">_sum = k[a] + k[b] + k[c] + k[d];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                  if(_sum == sum) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">                    result = true;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                    break;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                  }</span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"> </span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">          }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">      }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cout &lt;&lt; "_sum:" &lt;&lt; _sum &lt;&lt; "  " &lt;&lt; "sum:" &lt;&lt; sum &lt;&lt; endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   if(result){</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   </span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">cout&lt;&lt;"Yes"&lt;&lt;endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   } else</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">    cout&lt;&lt;"No"&lt;&lt;endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">}</span><span style="font-size:14pt; font-family:宋体"/></p></td></tr></tbody></table> 输入**样例1**,结果为: _sum:20  sum:10 Yes 原因:_sum:20是因为**break并没有结束循环(即没有跳出多层循环),而只是跳出了本层循环**,运行到循环最后一次时 k[a] == k[b] == k[c] == k[d] == 5; 那么,如何让程序跳出多重循环呢? # 我的解题思路是: **1. **java中有break [flat]的用法,可以解决这个问题,试了一下,但发现C++里不行,会报错,可能C++里没有这个用法;**2. **C/C++有个强制跳转的语法goto;**3. **加判断标志,不满足条件时逐层终止 # 我的解决方法: ### 一、使用goto <table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="568" valign="top" style="width:500pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">void  drawLots() {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   //<span style="font-family:宋体">从标准输入读入</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int numOfCard, sum;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int k[MAX_N];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cout&lt;&lt;"<span style="font-family:宋体">输入</span><span style="font-family:Vijaya">numOfCard</span><span style="font-family:宋体">和</span><span style="font-family:Vijaya">sum"&lt;&lt;endl;</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cin&gt;&gt;numOfCard&gt;&gt;sum; </span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cout&lt;&lt;"<span style="font-family:宋体">请输入这</span><span style="font-family:Vijaya">sum</span><span style="font-family:宋体">张卡片的数字</span><span style="font-family:Vijaya">"&lt;&lt;endl;</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   for(int i=0; i&lt;numOfCard; i++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">cin&gt;&gt;k[i];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   bool result = false;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int _sum = 0;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   for(int a = 0; a &lt; numOfCard; a ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">      for(int b = 0; b &lt; numOfCard; b ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">          for(int c = 0; c &lt; numOfCard; c++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              for(int d = 0; d &lt; numOfCard; d ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              </span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">_sum = k[a] + k[b] + k[c] + k[d];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                  if(_sum == sum) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">                    result = true;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                    goto breakLoop;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                  }</span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"> </span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">          }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">      }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   breakLoop: cout &lt;&lt; "_sum:" &lt;&lt; _sum &lt;&lt; "  " &lt;&lt; "sum:" &lt;&lt; sum &lt;&lt; endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   if(result){</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   </span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">cout&lt;&lt;"Yes"&lt;&lt;endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   } else</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">    cout&lt;&lt;"No"&lt;&lt;endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">}</span><span style="font-size:14pt; font-family:宋体"/></p></td></tr></tbody></table> 输入样例1. 结果: _sum:10  sum:10 Yes ### 二、加判断标志 <table border="1" style="font-family:Simsun; border-collapse:collapse; padding:0pt 5.4pt"><tbody><tr><td width="568" valign="top" style="width:500pt; padding:0pt 5.4pt; border:0.5pt solid rgb(0,0,0)"><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">void  drawLots() {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   //<span style="font-family:宋体">从标准输入读入</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int numOfCard, sum;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int k[MAX_N];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cout&lt;&lt;"<span style="font-family:宋体">输入</span><span style="font-family:Vijaya">numOfCard</span><span style="font-family:宋体">和</span><span style="font-family:Vijaya">sum"&lt;&lt;endl;</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cin&gt;&gt;numOfCard&gt;&gt;sum; </span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cout&lt;&lt;"<span style="font-family:宋体">请输入这</span><span style="font-family:Vijaya">sum</span><span style="font-family:宋体">张卡片的数字</span><span style="font-family:Vijaya">"&lt;&lt;endl;</span></span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   for(int i=0; i&lt;numOfCard; i++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">cin&gt;&gt;k[i];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   bool result = false;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   bool isBreakLoop = true;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   int _sum = 0;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   for(int a = 0; a &lt; numOfCard &amp;&amp; isBreakLoop; a ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">      for(int b = 0; b &lt; numOfCard &amp;&amp; isBreakLoop; b ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">          for(int c = 0; c &lt; numOfCard &amp;&amp; isBreakLoop; c++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              for(int d = 0; d &lt; numOfCard &amp;&amp; isBreakLoop; d ++) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              </span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">_sum = k[a] + k[b] + k[c] + k[d];</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                  if(_sum == sum) {</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"><span style="font-family:宋体; font-size:19px; text-align:justify">                      </span>result = true;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"><span style="font-family:宋体; font-size:19px; text-align:justify">                      </span>isBreakLoop = false;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">                  }</span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体"> </span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">              }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">          }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">      }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   }</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   cout &lt;&lt; "_sum:" &lt;&lt; _sum &lt;&lt; "  " &lt;&lt; "sum:" &lt;&lt; sum &lt;&lt; endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   if(result){</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   </span><span style="font-size:14pt; font-family:宋体"/><span style="font-size:14pt; font-family:宋体">cout&lt;&lt;"Yes"&lt;&lt;endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">   } else</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">    cout&lt;&lt;"No"&lt;&lt;endl;</span><span style="font-size:14pt; font-family:宋体"/></p><p class="p0" style="margin:0pt; text-align:justify; font-size:14pt; font-family:'Times New Roman'"><span style="font-size:14pt; font-family:宋体">}</span><span style="font-size:14pt; font-family:宋体"/></p></td></tr></tbody></table> 输入样例1. 结果: _sum:10  sum:10 Yes ### 说明: 本人还是建议采用方法二,因为方法二更符合结构化的程序设计,使代码更整洁,可读性更强!我还是尽量避免使用goto。 欢迎加入"C/C++梦之队" 学习群:226157456