﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-RePorridge-随笔分类-UVA题解</title><link>http://www.cppblog.com/JaceForce/category/20664.html</link><description>Nothing change but our heart</description><language>zh-cn</language><lastBuildDate>Wed, 30 Oct 2013 07:24:43 GMT</lastBuildDate><pubDate>Wed, 30 Oct 2013 07:24:43 GMT</pubDate><ttl>60</ttl><item><title>UVA 10071 Back to High School Physics 题解</title><link>http://www.cppblog.com/JaceForce/archive/2013/09/08/203092.html</link><dc:creator>Porridge</dc:creator><author>Porridge</author><pubDate>Sun, 08 Sep 2013 13:15:00 GMT</pubDate><guid>http://www.cppblog.com/JaceForce/archive/2013/09/08/203092.html</guid><wfw:comment>http://www.cppblog.com/JaceForce/comments/203092.html</wfw:comment><comments>http://www.cppblog.com/JaceForce/archive/2013/09/08/203092.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JaceForce/comments/commentRss/203092.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JaceForce/services/trackbacks/203092.html</trackback:ping><description><![CDATA[Problem B<br />Back to High School Physics<br />Input: standard input<br />Output: standard output<br /> <br />A particle has initial velocity and constant acceleration. If its velocity after certain time is v then what will its displacement be in twice of that time?<br /> <br />Input<br />The input will contain two integers in each line. Each line makes one set of input. These two integers denote the value of v (-100 &lt;= v &lt;= 100) and t(0&lt;=t&lt;= 200) ( t means at the time the particle gains that velocity) <br /> <br />Output<br />For each line of input print a single integer in one line denoting the displacement in double of that time.<br /> <br />Sample Input<br />0 0<br />5 12<br /><br />Sample Output<br />0<br />120<br /><br />其实题目很简单，差点被误导了。<br /><br />题意：一个物体有初速度v,然后再2*t的时间内走的位移。没有加速度。<br /><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_123413" onclick="this.style.display='none'; Code_Closed_Text_123413.style.display='none'; Code_Open_Image_123413.style.display='inline'; Code_Open_Text_123413.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_123413" style="display: none" onclick="this.style.display='none'; Code_Open_Text_123413.style.display='none'; Code_Closed_Image_123413.style.display='inline'; Code_Closed_Text_123413.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_123413" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">physics.cpp</span><span id="Code_Open_Text_123413" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include&nbsp;&lt;iostream&gt;<br />#include&nbsp;&lt;cstdio&gt;<br /><span style="color: #0000FF; ">using</span>&nbsp;<span style="color: #0000FF; ">namespace</span>&nbsp;std;<br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;a,b;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(cin&gt;&gt;a&gt;&gt;b)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;a*b*2&lt;&lt;endl;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}<br /></span></div><img src ="http://www.cppblog.com/JaceForce/aggbug/203092.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JaceForce/" target="_blank">Porridge</a> 2013-09-08 21:15 <a href="http://www.cppblog.com/JaceForce/archive/2013/09/08/203092.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 457 Linear Cellular Automata 题解</title><link>http://www.cppblog.com/JaceForce/archive/2013/09/02/202961.html</link><dc:creator>Porridge</dc:creator><author>Porridge</author><pubDate>Mon, 02 Sep 2013 11:10:00 GMT</pubDate><guid>http://www.cppblog.com/JaceForce/archive/2013/09/02/202961.html</guid><wfw:comment>http://www.cppblog.com/JaceForce/comments/202961.html</wfw:comment><comments>http://www.cppblog.com/JaceForce/archive/2013/09/02/202961.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JaceForce/comments/commentRss/202961.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JaceForce/services/trackbacks/202961.html</trackback:ping><description><![CDATA[Linear Cellular Automata <br />A biologist is experimenting with DNA modification of bacterial colonies being grown in a linear array of culture dishes. By changing the DNA, he is able ``program" the bacteria to respond to the population density of the neighboring dishes. Population is measured on a four point scale (from 0 to 3). The DNA information is represented as an array DNA, indexed from 0 to 9, of population density values and is interpreted as follows:<br /><br />In any given culture dish, let K be the sum of that culture dish's density and the densities of the dish immediately to the left and the dish immediately to the right. Then, by the next day, that dish will have a population density of DNA[K].<br />The dish at the far left of the line is considered to have a left neighbor with population density 0.<br />The dish at the far right of the line is considered to have a right neighbor with population density 0.<br />Now, clearly, some DNA programs cause all the bacteria to die off (e.g., [0,0,0,0,0,0,0,0,0,0]). Others result in immediate population explosions (e.g., [3,3,3,3,3,3,3,3,3,3]). The biologist is interested in how some of the less obvious intermediate DNA programs might behave.<br /><br />Write a program to simulate the culture growth in a line of 40 dishes, assuming that dish 20 starts with a population density of 1 and all other dishes start with a population density of 0.<br /><br />Input<br /><br />The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.<br /><br />For each input set your program will read in the DNA program (10 integer values) on one line.<br /><br />Output<br /><br />For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.<br /><br />For each input set it should print the densities of the 40 dishes for each of the next 50 days. Each day's printout should occupy one line of 40 characters. Each dish is represented by a single character on that line. Zero population densities are to be printed as the character ` '. Population density 1 will be printed as the character `.'. Population density 2 will be printed as the character `x'. Population density 3 will be printed as the character `W'.<br /><br />Sample Input<br /><br />1<br /><br />0 1 2 0 1 3 3 2 3 0<br />Sample Output<br /><br />bbbbbbbbbbbbbbbbbbb.bbbbbbbbbbbbbbbbbbbb<br />bbbbbbbbbbbbbbbbbb...bbbbbbbbbbbbbbbbbbb<br />bbbbbbbbbbbbbbbbb.xbx.bbbbbbbbbbbbbbbbbb<br />bbbbbbbbbbbbbbbb.bb.bb.bbbbbbbbbbbbbbbbb<br />bbbbbbbbbbbbbbb.........bbbbbbbbbbbbbbbb<br />bbbbbbbbbbbbbb.xbbbbbbbx.bbbbbbbbbbbbbbb<br />bbbbbbbbbbbbb.bbxbbbbbxbb.bbbbbbbbbbbbbb<br />bbbbbbbbbbbb...xxxbbbxxx...bbbbbbbbbbbbb<br />bbbbbbbbbbb.xb.WW.xbx.WW.bx.bbbbbbbbbbbb<br />bbbbbbbbbb.bbb.xxWb.bWxx.bbb.bbbbbbbbbbb<br /> <br />Note: Whe show only the first ten lines of output (the total number of lines must be 50) and the spaces have been replaced with the character "b" for ease of reading. The actual output file will use the ASCII-space character, not "b".<br /><br /><br />题目理解了半天。到网上找了题意大概是这样的。<br />起初是一组只有第20个是1其他全是0的40个细菌的培养基吧<br />设为X[0]X[1]....X[39] X[0]左边X[-1]和X[39] X[40]右边的数量设置成0 假设其存在，程序中可以特殊处理一下<br />第二天的细菌的数量X2[i] = DNA[X1[i-1] ＋ X1[i] + X1[i+1]] X2 X1表示第一天、第二天的培养基<br /><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_140356" onclick="this.style.display='none'; Code_Closed_Text_140356.style.display='none'; Code_Open_Image_140356.style.display='inline'; Code_Open_Text_140356.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_140356" style="display: none" onclick="this.style.display='none'; Code_Open_Text_140356.style.display='none'; Code_Closed_Image_140356.style.display='inline'; Code_Closed_Text_140356.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_140356" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">UVA457.cpp</span><span id="Code_Open_Text_140356" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include&nbsp;&lt;iostream&gt;<br />#include&nbsp;&lt;cstdio&gt;<br />#include&nbsp;&lt;cstring&gt;<br /><span style="color: #0000FF; ">using</span>&nbsp;<span style="color: #0000FF; ">namespace</span>&nbsp;std;<br /><span style="color: #0000FF; ">char</span>&nbsp;ddish[50][41];<br /><span style="color: #0000FF; ">int</span>&nbsp;DNA[10];<br /><span style="color: #0000FF; ">int</span>&nbsp;C[300];<br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;T;<br />&nbsp;&nbsp;&nbsp;&nbsp;C['&nbsp;']&nbsp;=&nbsp;0;<br />&nbsp;&nbsp;&nbsp;&nbsp;C['.']&nbsp;=&nbsp;1;<br />&nbsp;&nbsp;&nbsp;&nbsp;C['x']&nbsp;=&nbsp;2;<br />&nbsp;&nbsp;&nbsp;&nbsp;C['W']&nbsp;=&nbsp;3;<br />&nbsp;&nbsp;&nbsp;&nbsp;C[0]&nbsp;=&nbsp;'&nbsp;';<br />&nbsp;&nbsp;&nbsp;&nbsp;C[1]&nbsp;=&nbsp;'.';<br />&nbsp;&nbsp;&nbsp;&nbsp;C[2]&nbsp;=&nbsp;'x';<br />&nbsp;&nbsp;&nbsp;&nbsp;C[3]&nbsp;=&nbsp;'W';<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;ok&nbsp;=&nbsp;1;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(scanf("%d",&amp;T);T;T--)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(ok)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ok&nbsp;=&nbsp;0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&lt;&nbsp;50;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span>&nbsp;j&nbsp;=&nbsp;0;j&nbsp;&lt;&nbsp;40;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ddish[i][j]&nbsp;=&nbsp;'&nbsp;';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ddish[i][40]&nbsp;=&nbsp;'\0';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ddish[0][19]&nbsp;=&nbsp;'.';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&lt;&nbsp;10;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d",&amp;DNA[i]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;1;i&nbsp;&lt;&nbsp;50;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ddish[i][0]&nbsp;=&nbsp;C[DNA[(C[(<span style="color: #0000FF; ">int</span>)ddish[i-1][0]]&nbsp;+&nbsp;C[(<span style="color: #0000FF; ">int</span>)ddish[i-1][1]])]];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ddish[i][39]&nbsp;=&nbsp;C[DNA[(C[(<span style="color: #0000FF; ">int</span>)ddish[i-1][39]]&nbsp;+&nbsp;C[(<span style="color: #0000FF; ">int</span>)ddish[i-1][38]])]];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span>&nbsp;j&nbsp;=&nbsp;1;j&nbsp;&lt;&nbsp;39;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ddish[i][j]&nbsp;=&nbsp;C[DNA[(C[(<span style="color: #0000FF; ">int</span>)ddish[i-1][j-1]]&nbsp;+&nbsp;C[(<span style="color: #0000FF; ">int</span>)ddish[i-1][j+1]]&nbsp;+&nbsp;C[(<span style="color: #0000FF; ">int</span>)ddish[i-1][j]])]];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>(<span style="color: #0000FF; ">int</span>&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&lt;&nbsp;50;i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%s\n",ddish[i]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}<br /></span></div><img src ="http://www.cppblog.com/JaceForce/aggbug/202961.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JaceForce/" target="_blank">Porridge</a> 2013-09-02 19:10 <a href="http://www.cppblog.com/JaceForce/archive/2013/09/02/202961.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 10055 Hashmat the Brave Warrior 题解</title><link>http://www.cppblog.com/JaceForce/archive/2013/09/01/202927.html</link><dc:creator>Porridge</dc:creator><author>Porridge</author><pubDate>Sun, 01 Sep 2013 10:35:00 GMT</pubDate><guid>http://www.cppblog.com/JaceForce/archive/2013/09/01/202927.html</guid><wfw:comment>http://www.cppblog.com/JaceForce/comments/202927.html</wfw:comment><comments>http://www.cppblog.com/JaceForce/archive/2013/09/01/202927.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JaceForce/comments/commentRss/202927.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JaceForce/services/trackbacks/202927.html</trackback:ping><description><![CDATA[A - Hashmat the Brave Warrior<br />Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld &amp; %llu<br /><br />Description<br />Hashmat is a brave warrior who with his group of young soldiers moves from one place to another to fight against his opponents. Before fighting he just calculates one thing, the difference between his soldier number and the opponent's soldier number. From this difference he decides whether to fight or not. Hashmat's soldier number is never greater than his opponent.<br /><br />Input<br />The input contains two integer numbers in every line. These two numbers in each line denotes the number of soldiers in Hashmat's army and his opponent's army or vice versa. The input numbers are not greater than 2^32. Input is terminated by End of File.<br /><br />Output<br />For each line of input, print the difference of number of soldiers between Hashmat's army and his opponent's army. Each output should be in seperate line.<br /><br />Sample Input<br />10 12<br />10 14<br />100 200<br />Sample Output<br />2<br />4<br />100<br /><br /><br />本题主要是要注意Hashmat's soldier number is never greater than his opponent.这一句。所以读入后要比较一下大小。<br /><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_143957" onclick="this.style.display='none'; Code_Closed_Text_143957.style.display='none'; Code_Open_Image_143957.style.display='inline'; Code_Open_Text_143957.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_143957" style="display: none" onclick="this.style.display='none'; Code_Open_Text_143957.style.display='none'; Code_Closed_Image_143957.style.display='inline'; Code_Closed_Text_143957.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_143957" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">UVA10055.cpp</span><span id="Code_Open_Text_143957" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include&nbsp;&lt;iostream&gt;<br />#include&nbsp;&lt;cstdio&gt;<br /><span style="color: #0000FF; ">using</span>&nbsp;<span style="color: #0000FF; ">namespace</span>&nbsp;std;<br /><br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">long</span>&nbsp;<span style="color: #0000FF; ">long</span>&nbsp;a,b,c;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(scanf("%lld%lld",&amp;a,&amp;b)!=EOF)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(a&nbsp;&gt;&nbsp;b)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;=&nbsp;a;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;=&nbsp;b;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b&nbsp;=&nbsp;c;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%lld\n",b-a);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}<br /></span></div><img src ="http://www.cppblog.com/JaceForce/aggbug/202927.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JaceForce/" target="_blank">Porridge</a> 2013-09-01 18:35 <a href="http://www.cppblog.com/JaceForce/archive/2013/09/01/202927.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UVA 694  The Collatz Sequence  题解</title><link>http://www.cppblog.com/JaceForce/archive/2013/09/01/202926.html</link><dc:creator>Porridge</dc:creator><author>Porridge</author><pubDate>Sun, 01 Sep 2013 10:23:00 GMT</pubDate><guid>http://www.cppblog.com/JaceForce/archive/2013/09/01/202926.html</guid><wfw:comment>http://www.cppblog.com/JaceForce/comments/202926.html</wfw:comment><comments>http://www.cppblog.com/JaceForce/archive/2013/09/01/202926.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JaceForce/comments/commentRss/202926.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JaceForce/services/trackbacks/202926.html</trackback:ping><description><![CDATA[&nbsp; The Collatz Sequence <br />An algorithm given by Lothar Collatz produces sequences of integers, and is described as follows:<br />Step 1:<br />Choose an arbitrary positive integer A as the first item in the sequence.<br />Step 2:<br />If A = 1 then stop.<br />Step 3:<br />If A is even, then replace A by A / 2 and go to step 2.<br />Step 4:<br />If A is odd, then replace A by 3 * A + 1 and go to step 2.<br />It has been shown that this algorithm will always stop (in step 2) for initial values of A as large as 109, but some values of A encountered in the sequence may exceed the size of an integer on many computers. In this problem we want to determine the length of the sequence that includes all values produced until either the algorithm stops (in step 2), or a value larger than some specified limit would be produced (in step 4).<br />Input <br />The input for this problem consists of multiple test cases. For each case, the input contains a single line with two positive integers, the first giving the initial value of A (for step 1) and the second giving L, the limiting value for terms in the sequence. Neither of these, A or L, is larger than 2,147,483,647 (the largest value that can be stored in a 32-bit signed integer). The initial value of A is always less than L. A line that contains two negative integers follows the last case.<br />Output <br />For each input case display the case number (sequentially numbered starting with 1), a colon, the initial value for A, the limiting value L, and the number of terms computed.<br />Sample Input <br /><br /> 3 100<br /> 34 100<br /> 75 250<br /> 27 2147483647<br /> 101 304<br /> 101 303<br /> -1 -1<br /><br />Sample Output <br /><br /><br /> Case 1: A = 3, limit = 100, number of terms = 8<br /> Case 2: A = 34, limit = 100, number of terms = 14<br /> Case 3: A = 75, limit = 250, number of terms = 3<br /> Case 4: A = 27, limit = 2147483647, number of terms = 112<br /> Case 5: A = 101, limit = 304, number of terms = 26<br /> Case 6: A = 101, limit = 303, number of terms = 1<br /><br /><br /><br />本题其实简单，前面A,L用的都是int类型然后超时了，接着我用__int64，服务器不识别。然后又用long long 类型才秒过<br /><br />题意：给你一个初始A递归：如果A==1则停止，如果A是偶数则A = A / 2 如果A是奇数 则 A = 3 * A + 1&nbsp;<br /><br />这里还有L的用处，L是限定A的大小，如果上述步骤中A超过了L也直接停止 输出 步骤数<br /><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_143456" onclick="this.style.display='none'; Code_Closed_Text_143456.style.display='none'; Code_Open_Image_143456.style.display='inline'; Code_Open_Text_143456.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_143456" style="display: none" onclick="this.style.display='none'; Code_Open_Text_143456.style.display='none'; Code_Closed_Image_143456.style.display='inline'; Code_Closed_Text_143456.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_143456" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">UVA694.cpp</span><span id="Code_Open_Text_143456" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include&nbsp;&lt;iostream&gt;<br />#include&nbsp;&lt;cstdio&gt;<br /><span style="color: #0000FF; ">using</span>&nbsp;<span style="color: #0000FF; ">namespace</span>&nbsp;std;<br /><br /><span style="color: #0000FF; ">int</span>&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">long</span>&nbsp;<span style="color: #0000FF; ">long</span>&nbsp;A,L;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;C&nbsp;=&nbsp;0;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">long</span>&nbsp;<span style="color: #0000FF; ">long</span>&nbsp;a,b;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(scanf("%lld%lld",&amp;A,&amp;L),A&gt;=0&amp;&amp;L&gt;=0)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b&nbsp;=&nbsp;0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;=&nbsp;A;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(b++,a&nbsp;!=&nbsp;1&nbsp;&amp;&amp;&nbsp;b&nbsp;&lt;&nbsp;L)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(a%2)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;=&nbsp;3&nbsp;*&nbsp;a&nbsp;+&nbsp;1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(a&nbsp;&gt;&nbsp;L)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">break</span>;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;/=&nbsp;2;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("Case&nbsp;%d:&nbsp;A&nbsp;=&nbsp;%lld,&nbsp;limit&nbsp;=&nbsp;%lld,&nbsp;number&nbsp;of&nbsp;terms&nbsp;=&nbsp;%lld\n",++C,A,L,b);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;0;<br />}<br /></span></div><img src ="http://www.cppblog.com/JaceForce/aggbug/202926.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JaceForce/" target="_blank">Porridge</a> 2013-09-01 18:23 <a href="http://www.cppblog.com/JaceForce/archive/2013/09/01/202926.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>