杭电判断能不能连着连吃不一样的糖

来源:互联网 发布:程序员之路 周金桥 编辑:程序博客网 时间:2024/04/28 14:38

Problem DescriptionHOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一种,这样;可是Gardon不知道是否存在一种吃糖果的顺序使得他能把所有糖果都吃完?请你写个程序帮忙计算一下。Input第一行有一个整数T,接下来T组数据,每组数据占2行,第一行是一个整数N(0<N<=1000000),第二行是N个数,表示N种糖果的数目Mi(0<Mi<=1000000)。Output对于每组数据,输出一行,包含一个"Yes"或者"No"。Sample Input234 1 155 4 3 2 1Sample OutputHintHintPlease use function scanf #include <iostream>#include<cstdio>using namespace std;int main(){    int T,n,sum,max,temp;    scanf("%d",&T);    while(T--)    {        sum=temp=max=0;        scanf("%d",&n);        while(n--)        {            scanf("%d",&temp);            if(max<temp)                max=temp;            sum+=temp;        }        if(sum-max>max)            printf("Yes\n");        else            printf("No\n");    }    return 0;}<img src="http://img.blog.csdn.net/20150129100742250?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTcxNjEyMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />只要满足总共的糖数减去数目最多的糖数大于数目最多的糖数即可输出是<div class="article_title" style="margin: 5px 0px; font-size: 20px; line-height: 30px; font-family: 'Microsoft YaHei';"><span class="ico ico_type_Original" style="display: inline-block; width: 19px; height: 19px; margin: 0px 2px 0px 0px; background-image: url(http://static.blog.csdn.net/images/ico_Original.gif); vertical-align: middle; background-position: 0px 0px; background-repeat: no-repeat no-repeat;"></span> <h1 style="margin: 0px; padding: 0px; display: inline; font-weight: normal; font-size: 20px; vertical-align: middle;"><span class="link_title"><a target=_blank href="http://blog.csdn.net/mdx20072419/article/details/8543388" style="color: rgb(0, 0, 0); text-decoration: none;">scanf()函数用法小结</a></span></h1></div><div class="article_manage" style="padding: 5px 0px; color: rgb(153, 153, 153); line-height: 24px; font-family: Arial; text-align: right;"><span class="link_categories" style="margin: 0px 5px; float: left;">分类: <a target=_blank href="http://blog.csdn.net/mdx20072419/article/category/1191440" style="color: rgb(51, 102, 153); text-decoration: none;">linux基础</a> <a target=_blank href="http://blog.csdn.net/mdx20072419/article/category/1190659" style="color: rgb(51, 102, 153); text-decoration: none;">C语言</a></span><span class="link_postdate" style="margin: 0px 5px 0px 0px;">2013-01-25 20:46</span> <span class="link_view" title="阅读次数" style="margin: 0px 5px; padding: 0px 0px 0px 14px; background-image: url(http://static.blog.csdn.net/images/ico_view.png); background-position: 0% 50%; background-repeat: no-repeat no-repeat;">1652人阅读</span> <span class="link_comments" title="评论次数" style="margin: 0px 5px; padding: 0px 0px 0px 14px; background-image: url(http://static.blog.csdn.net/images/ico_comm.png); background-position: 0% 50%; background-repeat: no-repeat no-repeat;"><a target=_blank href="http://blog.csdn.net/mdx20072419/article/details/8543388#comments" style="color: rgb(51, 102, 153); text-decoration: none;">评论</a>(0)</span> <span class="link_collect" style="margin: 0px 5px;"><a target=_blank title="收藏" style="color: rgb(51, 102, 153);">收藏</a></span> <span class="link_report" style="margin: 0px 5px;"><a target=_blank href="http://blog.csdn.net/mdx20072419/article/details/8543388#report" title="举报" style="color: rgb(51, 102, 153); text-decoration: none;">举报</a></span></div><div id="article_content" class="article_content" style="margin: 20px 0px 0px; font-size: 14px; line-height: 26px; font-family: Arial; color: rgb(51, 51, 51);"><p><span style="color: rgb(85, 85, 85);">scanf()</span><span style="color: rgb(85, 85, 85);">函数是格式化输入函数,它从标准输入设备(</span>键盘) 读取输入的信息。</p><p><span style="color: rgb(85, 85, 85);">其调用格式为:      scanf("<</span>格式化字符串>",<地址表>);</p><p><span style="color: rgb(85, 85, 85);">格式化字符串包括以下三类不同的字符;</span></p><p><span style="color: rgb(85, 85, 85);">1</span><span style="color: rgb(85, 85, 85);">、 </span>格式化说明符:</p><p><span style="color: rgb(85, 85, 85);">格式化说明符与printf()</span>函数中的格式说明符基本相同。但和printf()函数中格式字符串的用法有一些小区别。我们来看下面这个表。</p><p><span style="color: rgb(85, 85, 85);">格式字符                                           </span>说明%d                                              从键盘输入十进制整数%o                                             从键盘输入八进制整数%x                                             从键盘输入十六进制整数%c                                             从键盘输入一个字符%s                                              从键盘输入一个字符串%f                                             从键盘输入一个实数%e                                             与%f的作用相同</p><p><span style="color: rgb(85, 85, 85);">附加格式说明字符表                                         </span></p><p><span style="color: rgb(85, 85, 85);">字符                                           </span>说明L                                            输入"长"数据H                                           输入"短"数据M                                           指定输入数据所占宽度*                                           空读一个数据</p><p><span style="color: rgb(85, 85, 85);">2</span><span style="color: rgb(85, 85, 85);">、   </span>空白字符: 空白字符会使scanf()函数在读操作中略去输入中的一个或多个空白字符。</p><p><span style="color: rgb(85, 85, 85);">3</span><span style="color: rgb(85, 85, 85);">、   </span>非空白字符: 一个非空白字符会使scanf()函数在读入时剔除掉与这个非空白字符相同的字符。</p><p><span style="color: rgb(85, 85, 85);">地址表是需要读入的所有变量的地址,而不是变量本身。这与printf()</span>函数完全不同,要特别注意。各个变量的地址之间同","分开。</p><p><span style="color: rgb(85, 85, 85);"> </span></p><p><span style="color: rgb(85, 85, 85);">  </span><span style="color: rgb(85, 85, 85);">首先我们来看一下格式化说明符“%d</span>”,从键盘输入一个十进制整数,当多个输入时,采用一个或多个空格做为分隔符,看下面的例子:</p><p><span style="color: rgb(85, 85, 85);"> <img src="http://img.my.csdn.net/uploads/201301/25/1359118158_2049.jpg" alt="" style="border: none; max-width: 100%;" /></span></p><p><span style="color: rgb(85, 85, 85);"></span></p><p>运行结果如下:</p><p><span style="color: rgb(85, 85, 85);"><img src="http://img.my.csdn.net/uploads/201301/25/1359118178_7237.jpg" alt="" style="border: none; max-width: 100%;" /></span></p><p><span style="color: rgb(85, 85, 85);"></span></p><p>分析如下:</p><p>  scanf格式化输入中有多个空格和’\n’,但是我们采用的是%d格式化输入,所以空格和’\n’等转义字符是没有读入的,这里有一个”%*3d”,意思是跳过输入的前三个整型数字,从第四个数字开始读入,所以我们输入“432432432”,输出的却是432432,而不是432432432.</p><p>有同学就要问了,为什么输入“432432432”后还有输入一个字母’d’呢?其实这个地方输入任何一个非空字节都是可以的。因为scanf必须从缓冲区读入一个非空字符再回车才会刷新缓冲区,这里scanf已经把“432432432”读入变量,缓冲区里就为空了,所以必须输入一个非空字节,这样按回车才会结束scanf输入。</p><p> </p><p>好,下面我们来看”%c”,还是先写一个例子:</p><img src="http://img.my.csdn.net/uploads/201301/25/1359118205_9898.jpg" alt="" style="border: none; max-width: 100%;" /><p><span style="color: rgb(85, 85, 85);"></span></p><p>运行结果如下:</p><img src="http://img.my.csdn.net/uploads/201301/25/1359118227_5531.jpg" alt="" style="border: none; max-width: 100%;" /><p><span style="color: rgb(85, 85, 85);"></span></p><p>分析:</p><p>  这里一目了然,采用“%c”输入时,scanf不仅会读入非空字符,遇到空字符他也会读入。</p><p>再看一个例子:</p><p><span style="color: rgb(85, 85, 85);">#include <stdio.h></span></p><p><span style="color: rgb(85, 85, 85);">void main()</span></p><p><span style="color: rgb(85, 85, 85);">{</span></p><p><span style="color: rgb(85, 85, 85);">       char a,b;</span></p><p><span style="color: rgb(85, 85, 85);">       printf("input charactera,b\n");</span></p><p><span style="color: rgb(85, 85, 85);">       scanf("%c %c",&a,&b);/*</span><span style="color: rgb(85, 85, 85);">注意两个%c</span>之间有个空格*/</p><p><span style="color: rgb(85, 85, 85);">       printf("\n%c%c\n",a,b);</span></p><p><span style="color: rgb(85, 85, 85);">}</span></p><p><span style="color: rgb(85, 85, 85);">本例表示scanf</span>格式控制串"%c %c"之间有空格时, 输入的数据之间可以有空格间隔。这时候我们应该这样输入’a’空格’b’。</p><p><span style="color: rgb(85, 85, 85);"> </span></p><p><span style="color: rgb(85, 85, 85);">下面看一下”%s”,</span>遇到空格换行等转义字符不读入,</p><img src="http://img.my.csdn.net/uploads/201301/25/1359118257_8341.jpg" alt="" style="border: none; max-width: 100%;" /><p><span style="color: rgb(85, 85, 85);"></span></p><p>分析:</p><p>  这里一目了然,采用“%c”输入时,scanf不仅会读入非空字符,遇到空字符他也会读入。</p><p>再看一个例子:</p><p><span style="color: rgb(85, 85, 85);">#include <stdio.h></span></p><p><span style="color: rgb(85, 85, 85);">void main()</span></p><p><span style="color: rgb(85, 85, 85);">{</span></p><p><span style="color: rgb(85, 85, 85);">       char a,b;</span></p><p><span style="color: rgb(85, 85, 85);">       printf("input charactera,b\n");</span></p><p><span style="color: rgb(85, 85, 85);">       scanf("%c %c",&a,&b);/*</span><span style="color: rgb(85, 85, 85);">注意两个%c</span>之间有个空格*/</p><p><span style="color: rgb(85, 85, 85);">       printf("\n%c%c\n",a,b);</span></p><p><span style="color: rgb(85, 85, 85);">}</span></p><p><span style="color: rgb(85, 85, 85);">本例表示scanf</span>格式控制串"%c %c"之间有空格时, 输入的数据之间可以有空格间隔。这时候我们应该这样输入’a’空格’b’。</p><p><span style="color: rgb(85, 85, 85);"> </span></p><p><span style="color: rgb(85, 85, 85);">下面看一下”%s”,</span>遇到空格换行等转义字符不读入,</p><img src="http://img.my.csdn.net/uploads/201301/25/1359118303_2831.jpg" alt="" style="border: none; max-width: 100%;" /><p><span style="color: rgb(85, 85, 85);"></span></p><p>运行结果如下:</p><p><img src="http://img.my.csdn.net/uploads/201301/25/1359118330_9059.jpg" alt="" style="border: none; max-width: 100%;" /></p><div></div></div>

0 0
原创粉丝点击