﻿<?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++博客-I believe-随笔分类-规律</title><link>http://www.cppblog.com/luyulaile/category/11039.html</link><description>I  can</description><language>zh-cn</language><lastBuildDate>Tue, 14 Jul 2009 09:38:16 GMT</lastBuildDate><pubDate>Tue, 14 Jul 2009 09:38:16 GMT</pubDate><ttl>60</ttl><item><title> joj 2243 Endless Carry</title><link>http://www.cppblog.com/luyulaile/archive/2009/07/14/90058.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Tue, 14 Jul 2009 09:18:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2009/07/14/90058.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/90058.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2009/07/14/90058.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/90058.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/90058.html</trackback:ping><description><![CDATA[<div class=prob_text>作为2进制的加法，从k加1变成k+1可能会出现若干进位。如1011加1就会有2个进位。给定n，从0开始不停地加1直到n，计算在此过程中总共会有多少次进位。
<h3>Input</h3>
输入的每一行有单独的一个值n。n=0标志输入结束
<h3>Output</h3>
对于输入的每一行，使用单独一行输出对应结果。
<h3>Sample Input</h3>
<pre>2
5
10
0
</pre>
<h3>Sample Output</h3>
<pre>1
3
8
</pre>
总结规律：<br>1，0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>2，1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>3，1<br>4，3<br>5，3<br>6，4<br>7，4<br>8，7<br>9，7<br>10，8<br>11，8<br>12，10<br>16，15<br>32，31<br>64，63<br>你会发现2^n的都是2^n-1<br>其他13=8+4+1=(8-1)+(4-1)+(1-1),有多少个2^n,则减多少次1。<br>直接根据位运算即可求<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br></div>
<img src ="http://www.cppblog.com/luyulaile/aggbug/90058.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2009-07-14 17:18 <a href="http://www.cppblog.com/luyulaile/archive/2009/07/14/90058.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>joj 2278 count the square</title><link>http://www.cppblog.com/luyulaile/archive/2009/07/03/89170.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Fri, 03 Jul 2009 08:41:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2009/07/03/89170.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/89170.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2009/07/03/89170.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/89170.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/89170.html</trackback:ping><description><![CDATA[<div class=prob_text>There is chessboard which has N * M squares size of 1 * 1 and small squares can build up to a large one. Can you tell Mr. Guo that how many squares in the chessboard? <br><br>For Example, when N = 2 and M = 3, there are 6 squares size of 1 * 1 and 2 squares size of 2 * 2 in the chessboard, so the answer is 8.
<h3>Input</h3>
There are several lines in the input, each line consist of two positive integers N and M (1 &lt;= N &lt;= 100, 1 &lt;= M &lt;= 100) except the last line which N = 0 and M = 0 implying the end of the input and you should not process it.
<h3>Output</h3>
Please output the number of the squares for each chessboard in a single line.
<h3>Sample Input</h3>
<pre>1 1
2 3
3 3
0 0
</pre>
<h3>Sample Output</h3>
<pre>1
8
14
</pre>
</div>
<br>启发：人是怎么算的，就让程序怎么算，一步一步推。<br>#include&lt;iostream&gt;<br>using namespace std;<br>int main()<br>{<br>int n,m;<br>long sum=0;<br>int i;<br>while(cin&gt;&gt;n&gt;&gt;m,n)<br>{<br>if(n==m)<br>{<br>sum=n*(n+1)*(2*n+1)/6;<br>cout&lt;&lt;sum&lt;&lt;endl;<br>}<br>else<br>{<br>for(i=0;i&lt;n&amp;&amp;i&lt;m;i++)<br>sum+=(n-i)*(m-i);<br>cout&lt;&lt;sum&lt;&lt;endl;<br>}<br>sum=0;<br>}<br>return 0;<br>}
<img src ="http://www.cppblog.com/luyulaile/aggbug/89170.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2009-07-03 16:41 <a href="http://www.cppblog.com/luyulaile/archive/2009/07/03/89170.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Round Up by Six巧妙转化</title><link>http://www.cppblog.com/luyulaile/archive/2009/07/03/89124.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Thu, 02 Jul 2009 16:56:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2009/07/03/89124.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/89124.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2009/07/03/89124.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/89124.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/89124.html</trackback:ping><description><![CDATA[<pre>
<div class=prob_text>
<p>In arithmetic we often use the scheme called 'round up by five' to round up a decimal fraction number into a decimal integer number whose fraction part equals to or is bigger than 0.5, otherwise, its fraction part is discarded.</p>
<p>In this problem, you are to implement a similar sheme called 'round up by six'.</p>
<h3>Input</h3>
<p>The first line of the input is an integer N indicating the number of cases you are to process. Then from the second line there are N lines, each containing a number (may or may not be fractional).</p>
<h3>Output</h3>
<p>For each number in the input, you are to print the round-up-by-six version of the number in a single line.</p>
<h3>Sample Input</h3>
<pre>2
25.599
0.6
</pre>
<h3>Sample Output</h3>
<pre>25
1
</pre>
</div>
</pre>
<pre>&nbsp;</pre>
<pre>#include&lt;iostream&gt;
#include&lt;cmath&gt;
using namespace std;
int main()
{int n;
cin&gt;&gt;n;
while(n--)
{  double a;
cin&gt;&gt;a;
double b=floor(a);
if(a-b&gt;=0.6)
cout&lt;&lt;b+1&lt;&lt;endl;
else
cout&lt;&lt;b&lt;&lt;endl;
}
return 0;
}
</pre>
<img src ="http://www.cppblog.com/luyulaile/aggbug/89124.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2009-07-03 00:56 <a href="http://www.cppblog.com/luyulaile/archive/2009/07/03/89124.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>kmp算法实现 及 kmp改进算法的实现笔记</title><link>http://www.cppblog.com/luyulaile/archive/2009/06/30/88890.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Tue, 30 Jun 2009 06:12:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2009/06/30/88890.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/88890.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2009/06/30/88890.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/88890.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/88890.html</trackback:ping><description><![CDATA[<div>本文是结合刘大有主编《数据结构》所写笔记，如果想学习kmp.推荐<a href="http://blog.csdn.net/china8848/archive/2008/04/29/2342243.aspx">http://blog.csdn.net/china8848/archive/2008/04/29/2342243.aspx</a><br>下面的估计我都看不懂<br>#include&lt;iostream&gt;<br>#include&lt;cstdlib&gt;<br>#include&lt;string&gt;<br>using namespace std;<br>int f[100];<br>void createf(string pat)//find数组 <br>{<br>&nbsp;memset(f,0,sizeof(f));<br>&nbsp;int m=pat.size();<br>&nbsp;f[0]=-1;<br>&nbsp;for(int j=1;j&lt;m;j++)<br>&nbsp;{<br>&nbsp;&nbsp;int i=f[j-1];<br>&nbsp;&nbsp;while(pat[j]!=pat[i+1]&amp;&amp;i&gt;=0)//只要p[j]与p[i+1]不等，则递推计算i<br>&nbsp;&nbsp;i=f[i]; <br>&nbsp;&nbsp;if(pat[j]==pat[i+1])<br>&nbsp;&nbsp;f[j]=i+1;<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;f[j]=-1;<br>&nbsp;}<br>}<br>/*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 已知f[13]=6;acbacbcacbacb,求f[14]acbacbcacbacb<u>a</u>则先判断pat[6+1]是否等于pat[14]，不等<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;再判断 i=f[6]acbacb=3;判断pat[14]pat[3+1]相等，则i=3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可得f[j]=3+1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br>*/<br>int kmp(string pat,string str)//求pat在str中的第一个匹配的起点 <br>{<br>&nbsp;int p=0,s=0;<br>&nbsp;createf(pat); <br>&nbsp;int m=pat.size();<br>&nbsp;int n=str.size();<br>&nbsp;while(p&lt;m&amp;&amp;s&lt;n)<br>&nbsp;{<br>&nbsp;&nbsp;if(pat[p]==str[s])<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;p++;s++;<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;if(p==0)s++;<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;p=f[p-1]+1;//s无需变化,举例，abababababba<br>/*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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&lt;m)<br>&nbsp;&nbsp;&nbsp;&nbsp; return -1;<br>&nbsp;&nbsp;&nbsp;&nbsp; return s-m;<br>}</div>
<p style="COLOR: #000000">&nbsp; int main()<br>&nbsp; {<br>&nbsp; //freopen("s.txt","r",stdin);<br>&nbsp; //freopen("key.txt","w",stdout);<br>&nbsp; cout&lt;&lt;kmp("fgfh","abcabfgfdhsfgfhdfdfhfcdab");//返回的是数组下标，从0开始 <br>&nbsp; getchar();<br>&nbsp; //system("PAUSE");<br>&nbsp; return&nbsp;&nbsp; 0;<br>&nbsp; }<br><br>改进的kmp<br>----------------------以下是原kmp生成next值的算法---------------------<br>void createf(string pat)//find数组 <br>{<br>&nbsp;memset(f,0,sizeof(f));<br>&nbsp;int m=pat.size();<br>&nbsp;f[0]=-1;<br>&nbsp;for(int j=1;j&lt;m;j++)<br>&nbsp;{<br>&nbsp;&nbsp;int i=f[j-1];<br>&nbsp;&nbsp;while(pat[j]!=pat[i+1]&amp;&amp;i&gt;=0)//只要p[j]与p[i+1]不等，则递推计算i<br>&nbsp;&nbsp;i=f[i]; <br>&nbsp;&nbsp;if(pat[j]==pat[i+1])<br>&nbsp;&nbsp;f[j]=i+1;<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;f[j]=-1;<br>&nbsp;}<br>}<br>/*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 已知f[13]=6;acbacbcacbacb,求f[14]acbacbcacbacb<u>a</u>则先判断pat[6+1]是否等于pat[14]，不等<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;再判断 i=f[6]acbacb=3;判断pat[14]pat[3+1]相等，则i=3<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可得f[j]=3+1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br>*/<br>-------------------以下还是未改进kmp------------<br>void get_next(String T,int &amp;next[])<br>{<br>&nbsp; i=1;j=0;next[1]=0;<br>&nbsp; while(i&lt;=T.Length)<br>&nbsp; {<br>&nbsp;&nbsp;&nbsp; if(j==0 || T[i]==T[j]){++i;++j; next[i]=j;/**********(2)*/}<br>&nbsp;&nbsp;&nbsp; else j=next[j];<br>&nbsp; }<br>}<br><br>--------------------以下是改进型----------<br>上面的f[j]相当于next[]<br>&nbsp;int i,j,len,n;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len=strlen(s);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //KMP构造<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i=0,j=-1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; next[0]=-1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(i&lt;len)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(j==-1||s[i]==s[j])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>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nbsp;&nbsp; printf("next[%d]=%d\n",i,next[i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else j=next[j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br></p>
<img src ="http://www.cppblog.com/luyulaile/aggbug/88890.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2009-06-30 14:12 <a href="http://www.cppblog.com/luyulaile/archive/2009/06/30/88890.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>joj 2344 Dissolution of Integer</title><link>http://www.cppblog.com/luyulaile/archive/2009/06/30/88860.html</link><dc:creator>luis</dc:creator><author>luis</author><pubDate>Tue, 30 Jun 2009 02:49:00 GMT</pubDate><guid>http://www.cppblog.com/luyulaile/archive/2009/06/30/88860.html</guid><wfw:comment>http://www.cppblog.com/luyulaile/comments/88860.html</wfw:comment><comments>http://www.cppblog.com/luyulaile/archive/2009/06/30/88860.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/luyulaile/comments/commentRss/88860.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/luyulaile/services/trackbacks/88860.html</trackback:ping><description><![CDATA[<div class=prob_text>
<p>As you known,a positive integer can be written to a form of products of several positive integers(N = x1 * x2 * x3 *.....* xm (xi!=1 (1&lt;=i&lt;=m))).Now, given a positive integer, please tell me how many forms of products. </p>
<h2>Input</h2>
<p>Input consists of several test cases.Given a positive integer N (2&lt;=N&lt;=200000) in every case. </p>
<h2>Output</h2>
<p>For each case, you should output how many forms of the products. </p>
<h2>Sample Input</h2>
<pre>12
100
5968
</pre>
<h2>Sample Output</h2>
<pre>4
9
12
</pre>
</div>
<p><br>#include&lt;iostream&gt;<br>#include&lt;cstdlib&gt;<br>using namespace std;</p>
<p>&nbsp; int main()<br>&nbsp; {<br>&nbsp; int a[200001];<br>&nbsp; freopen("s.txt","r",stdin);<br>&nbsp; freopen("key.txt","w",stdout);<br>&nbsp; int i,j,n;<br>&nbsp; memset(a,0,sizeof(a));<br>&nbsp; a[1]=1;<br>&nbsp; for(i=1;i&lt;=200000;i++)<br>&nbsp;&nbsp;for(j=1;i*j&lt;=200000;j++)<br>&nbsp;&nbsp;&nbsp;a[i*j]+=a[j];<br>&nbsp;while(scanf("%d",&amp;n)!=EOF)<br>&nbsp;&nbsp;printf("%d\n",a[n]/2);<br>&nbsp; //system("PAUSE");<br>&nbsp; return&nbsp;&nbsp; 0;<br>&nbsp; }</p>
启发：申请数组一定要大一号a[200001]，否则无法清空a[200000];<br>发现递推规律要从特点和出发，乘法自然是乘法。
<img src ="http://www.cppblog.com/luyulaile/aggbug/88860.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/luyulaile/" target="_blank">luis</a> 2009-06-30 10:49 <a href="http://www.cppblog.com/luyulaile/archive/2009/06/30/88860.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>