<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>绝对零度</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/" />
    <link rel="self" type="application/atom+xml" href="http://www.zhuxinquan.com/cn/atom.xml" />
   <id>tag:www.zhuxinquan.com,2006:/cn//4</id>
    <link rel="service.post" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4" title="绝对零度" />
    <updated>2006-04-24T13:02:09Z</updated>
    <subtitle>朱鑫泉-&gt;泉泉-&gt;小猪-&gt;Jason-&gt;...</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type 3.2</generator>
 
<entry>
    <title>Problem 2723(半素数)</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/04/problem_2723.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=256" title="Problem 2723(半素数)" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.256</id>
    
    <published>2006-04-24T12:58:44Z</published>
    <updated>2006-04-24T13:02:09Z</updated>
    
    <summary>题目定义了一种叫半素数的数：只要一个数能被分解成两个素数，那么这个数就是半素数。 Prime Number Definition An integer greater than one is called a prime number if its only positive divisors (factors) are one and itself. For instance, 2, 11, 67, 89 are prime numbers but 8, 20, 27 are not. Semi-Prime...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="ZOJ题解" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>题目定义了一种叫半素数的数：只要一个数能被分解成两个素数，那么这个数就是半素数。<br />
 Prime Number Definition<br />
An integer greater than one is called a prime number if its only positive divisors (factors) are one and itself. For instance, 2, 11, 67, 89 are prime numbers but 8, 20, 27 are not.</p>

<p>Semi-Prime Number Definition<br />
An integer greater than one is called a semi-prime number if it can be decompounded to <span class="caps">TWO </span>prime numbers. For example, 6 is a semi-prime number but 12 is not.</p>

<p>Your task is just to determinate whether a given number is a semi-prime number.</p>

<p>Input</p>

<p>There are several test cases in the input. Each case contains a single integer N (2 &lt;= N &lt;= 1,000,000)</p>

<p>Output</p>

<p>One line with a single integer for each case. If the number is a semi-prime number, then output "Yes", otherwise "No".</p>

<p>Sample Input</p>

<p>3<br />
4<br />
6<br />
12</p>

<p>Sample Output<br />
No<br />
Yes<br />
Yes<br />
No</p>]]>
        <![CDATA[<p>没什么好说的，解法很简单，解法如下：</p>


<pre>
#include &lt;iostream&gt;
#include &lt;cmath&gt;
using namespace std;
bool isprime(long test)
{
     int i;
     for(i=2;i&lt;=sqrt((long double)test);i++)
     {
          if(test%i ==0)
               return false;
     }
     return true;
}
bool isSemiPrime(long test)
{
     int i;
     for(i=2;i&lt;=sqrt((long double)test);i++)
     {
          if(test%i ==0)
          {
               int temp = test/i;
               return isprime(i) &amp;&amp; isprime(temp);
          }
     }
     return false;
}
int main()
{
     long n;
     while(cin&gt;&gt;n &amp;&amp; n !=0)
     {
          if(isSemiPrime(n))
               cout&lt;&lt;&quot;Yes&quot;&lt;&lt;endl;
          else
               cout&lt;&lt;&quot;No&quot;&lt;&lt;endl;
     }
}
</pre>]]>
    </content>
</entry>
<entry>
    <title>征求虚拟主机合租者</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/04/post.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=138" title="征求虚拟主机合租者" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.138</id>
    
    <published>2006-04-22T09:43:17Z</published>
    <updated>2006-06-18T11:33:21Z</updated>
    
    <summary>最近买了DreamHost虚拟主机服务，经过一段时间的试用，总体感觉还行。 在Herock的Blog上看到合租一个空间的主意，我一个人也用不掉这么多空间，也决定征集10个左右的合租者，而且最好也是Blogger，可以顺便认识一些优秀的Blogger。如果你想搭建一个自己的Blog或者网站并且对这个合租计划有兴趣的话请联系我吧。 当然和Herock一样，我不欢迎那些提供违反国家法律的色情、赌博、反动等信息资料、还有mp3, 软件下载等服务的人士，原因大家都清楚，这些服务会严重影响其他合租者。 按照Herock的合租的价格我适当做了一些下调（数字好听点^_^)， 初步定为￥88元/年包括 # 1个 FTP用户（300M）（update 现为1G） # 和我共享1000G/月的流量（每周增加8G、单用户不得超过200G/月） # 2个 Mysql数据库 # 支持无限顶级域名（需自己准备） # 无限子域名（如果你不准备购买域名，我可以送你一个xxx.zhuxinquan.com二级域名） # 支持CGI，PHP，Perl # 不支持ASP # 支持数据备份 每增加100M空间￥38元/年 每增加1个Mysql数据库￥18元/年 联系方式 至于Web访问速度，你可以测试一下连这个Blog的速度。 另外值得一提的是，因为这款空间的管理权限只有一个，所以像域名的解析、数据库的管理等等，只能由我来操作，有一点点的麻烦，这也是对我信誉度的考验，你只能相信我。 （update 合租已满，但你也可以联系我，如果有人到期退出，我会通知你） 有任何问题也可以在这里留言。...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="虚拟主机" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>最近买了DreamHost虚拟主机服务，经过一段时间的试用，总体感觉还行。<br />
在<a href="http://herock.net/archives/000157.html">Herock的Blog</a>上看到合租一个空间的主意，我一个人也用不掉这么多空间，也决定征集10个左右的合租者，而且最好也是Blogger，可以顺便认识一些优秀的Blogger。如果你想搭建一个自己的Blog或者网站并且对这个合租计划有兴趣的话请联系我吧。<br />
当然和Herock一样，我不欢迎那些提供违反国家法律的色情、赌博、反动等信息资料、还有mp3, 软件下载等服务的人士，原因大家都清楚，这些服务会严重影响其他合租者。<br />
按照Herock的合租的价格我适当做了一些下调（数字好听点^_^)，<br />
初步定为<strong>￥88元/年</strong>包括<br />
# 1个 FTP用户（<strong>300M</strong>）<span style="color: #FF0000"><em>（update 现为<strong>1G</strong>）</em></span><br />
# 和我共享<strong>1000G/月</strong>的流量（每周增加8G、单用户不得超过200G/月）<br />
# 2个 Mysql数据库<br />
# 支持无限顶级域名（需自己准备）<br />
# 无限子域名（如果你不准备购买域名，我可以送你一个xxx.zhuxinquan.com二级域名）<br />
# 支持CGI，PHP，Perl <br />
# 不支持ASP<br />
# 支持数据备份</p>

<p><em>每增加100M空间￥38元/年<br />
每增加1个Mysql数据库￥18元/年</em><br />
联系方式<img name="contact" src="http://zhuxinquan.com/image/gmail.gif" alt="email" border="0" /></p>

<p>至于Web访问速度，你可以测试一下连这个Blog的速度。</p>

<p>另外值得一提的是，因为这款空间的管理权限只有一个，所以像域名的解析、数据库的管理等等，只能由我来操作，有一点点的麻烦，这也是对我信誉度的考验，你只能相信我。</p>

<p><span style="color: #FF0000"><em>（update 合租已满，但你也可以联系我，如果有人到期退出，我会通知你）</em></span><br />
有任何问题也可以在这里留言。</p>]]>
        
    </content>
</entry>
<entry>
    <title>Problem 2722(淘汰赛问题)</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/04/problem_2722.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=254" title="Problem 2722(淘汰赛问题)" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.254</id>
    
    <published>2006-04-21T09:48:13Z</published>
    <updated>2006-04-21T09:52:29Z</updated>
    
    <summary>题目： Our school is planning to hold a new exciting computer programming contest. During each round of the contest, the competitors will be paired, and compete head-to-head. The loser will be eliminated, and the winner will advance to next round....</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="ZOJ题解" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>题目：<br />
Our school is planning to hold a new exciting computer programming contest. During each round of the contest, the competitors will be paired, and compete head-to-head. The loser will be eliminated, and the winner will advance to next round. It proceeds until there is only one competitor left, who is the champion. In a certain round, if the number of the remaining competitors is not even, one of them will be chosed randomly to advance to next round automatically, and then the others will be paired and fight as usual. The contest committee want to know how many rounds is needed to produce to champion, then they could prepare enough problems for the contest.</p>

<p>Input <br />
The input consists of several test cases. Each case consists of a single line containing a integer N - the number of the competitors in total. 1 &lt;= N &lt;= 2,147,483,647. An input with 0(zero) signals the end of the input, which should not be processed.</p>

<p>Output <br />
For each test case, output the number of rounds needed in the contest, on a single line.</p>

<p>Sample Input <br />
8<br />
16<br />
15<br />
0</p>


<p>Sample Output <br />
3<br />
4<br />
4</p>]]>
        <![CDATA[<p>题目比较简单，下面是我给的解法。其实就是计算一个数是2的几次方。</p>


<pre>
#include &lt;iostream&gt;
using namespace std;
long calculate(long test)
{
     long ret = 0;
     bool is2square = true;
     while(test!=1)
     {
          if(test % 2)
               is2square = false;
          test /= 2;
          ret++;
     }
     if(!is2square)
          ret++;
     return ret;
}

int main()
{
     long n;
     while(cin&gt;&gt;n &amp;&amp; n !=0)
     {
          cout&lt;&lt;calculate(n)&lt;&lt;endl;
     }
}
</pre>]]>
    </content>
</entry>
<entry>
    <title>What Do MSN Technologies (China) Do ? </title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/04/what_do_msn_technologies_china.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=253" title="What Do MSN Technologies (China) Do ? " />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.253</id>
    
    <published>2006-04-21T07:56:24Z</published>
    <updated>2006-04-21T07:57:48Z</updated>
    
    <summary>From : http://spaces.msn.com/ligongatmsn/blog/cns!1130EB21E0E5C23!108.entry?_c=BlogPart Since I joined Microsoft (MS) to be the Managing Director of MSN Technologies (China) (which let us refer to as MSNTC) almost 2 months ago, a lot of people ask me what MSNTC does. Here is a...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="文摘" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>From :<br />
<a href="http://spaces.msn.com/ligongatmsn/blog/cns!1130EB21E0E5C23!108.entry?_c=BlogPart">http://spaces.msn.com/ligongatmsn/blog/cns!1130EB21E0E5C23!108.entry?_c=BlogPart</a></p>

<p>Since I joined Microsoft (MS) to be the Managing Director of <span class="caps">MSN</span> Technologies (China) (which let us refer to as <span class="caps">MSNTC</span>) almost 2 months ago, a lot of people ask me what <span class="caps">MSNTC </span>does. Here is a rough outline, and is my estimate and subject to change without notice :-)<br />
 </p>]]>
        <![CDATA[<p>1. <span class="caps">MSNTC </span>is a part of the worldwide <span class="caps">MSN </span>business group (one of the 7 business groups, or BGs, in MS).<br />
 <br />
2. <span class="caps">MSNTC </span>covers all <span class="caps">R&amp;D </span>activities of <span class="caps">MSN </span>in China.<br />
 <br />
3. 80-90% of what we do would be worldwide <span class="caps">MSN </span>projects, such as Messenger, Hotmail, etc. 10-20% of what do would be targeted at China and the Asian region specifically, for example to make msn.com.cn more competitive.<br />
 <br />
4. We will grow to multiple hundreds of people very quickly, in both Beijing and Shanghai. We may have other locations, but so far the focus is in BJ and <span class="caps">SH.</span><br />
 <br />
5. We welcome folks who want to return from abroad, but their expectations have to be in line with reality in China. Unless they are super special, or with a lot of MS experience in the <span class="caps">US, </span>they would normally not be paid a premium since the local talent in China is very strong.<br />
 <br />
For those people who actually are interested in finding out what sort of jobs are available in <span class="caps">MSNTC, </span>here are a few things to keep in mind.<br />
 <br />
(a) <span class="caps">MSNTC </span>has all sorts of opennings, from admins to developers to managers to program managers ... but MS is somewhat different from other multinationals in how they are organized (see below).<br />
 <br />
(b) A first-line manager is called a Dev Lead (Development Lead) or Test Lead. So if you are used to sporting a manager title, be prepared for this change. (But I guess in Chinese the title for Dev Lead can still be Manager? Have to check.) And Dev Leads are expected to be extremely hands on, so if you are a pure people manager, it is going to be hard for you to be a Dev Lead.<br />
 <br />
&#169; A second-line manager is called a Dev Manager, who typically manages Dev Leads and others. This manager can also be very hands-on, but can also somewhat more focused on project and people management.<br />
 <br />
(d) A PM (program manager) usually is very technical. Apart from managing product schedules and dependencies, a PM at MS is expected to drive product feature spec and such. There are PM ladders, such as <span class="caps">LPM </span>(Lead PM) and <span class="caps">GPG </span>(Group PM), for larger projects.<br />
 <br />
(e) Test leads are usually regular employees but testers are typically outsourced to vendor companies.<br />
 <br />
Note the above are just rules of thumb and you will find out more accurate info if you talk to MS at some point.<br />
 <br />
Finally, I am busy hiring a few hiring managers. Once they come on board, perhaps in the next couple of weeks, they will turn around and start hiring like crazy. I am sure they will make their contact information known to all those who want to know :-)<br />
 <br />
So much for today.<br />
 <br />
Cheers,<br />
 <br />
Li Gong<br />
 </p>]]>
    </content>
</entry>
<entry>
    <title>激情与梦想</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/04/post_13.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=246" title="激情与梦想" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.246</id>
    
    <published>2006-04-11T10:18:57Z</published>
    <updated>2006-04-11T10:22:35Z</updated>
    
    <summary>如果没有梦想，就不会有激情，如果没有激情，就难以体味淋漓畅快的人生。by k_eckel 这正是现在的我所需要的两样东西。...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="人生" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>如果没有梦想，就不会有激情，如果没有激情，就难以体味淋漓畅快的人生。by <a href="http://www.mscenter.edu.cn/blog/k_eckel">k_eckel</a><br />
这正是现在的我所需要的两样东西。</p>]]>
        
    </content>
</entry>
<entry>
    <title>链表反转</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/04/post_12.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=242" title="链表反转" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.242</id>
    
    <published>2006-04-10T08:29:40Z</published>
    <updated>2006-04-10T09:23:51Z</updated>
    
    <summary><![CDATA[单向链表的反转是一个经常被问到的一个面试题，也是一个非常基础的问题。比如一个链表是这样的： 1-&gt;2-&gt;3-&gt;4-&gt;5 通过反转后成为5-&gt;4-&gt;3-&gt;2-&gt;1。最容易想到的方法遍历一遍链表，利用一个辅助指针，存储遍历过程中当前指针指向的下一个元素，然后将当前节点元素的指针反转后，利用已经存储的指针往后面继续遍历。源代码如下：...]]></summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="编程笔试题" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>单向链表的反转是一个经常被问到的一个面试题，也是一个非常基础的问题。比如一个链表是这样的： 1-&gt;2-&gt;3-&gt;4-&gt;5 通过反转后成为5-&gt;4-&gt;3-&gt;2-&gt;1。最容易想到的方法遍历一遍链表，利用一个辅助指针，存储遍历过程中当前指针指向的下一个元素，然后将当前节点元素的指针反转后，利用已经存储的指针往后面继续遍历。源代码如下：</p>]]>
        <![CDATA[<pre>
struct linka {
     int data;
     linka* next;
};

void reverse(linka*&amp; head)
{
     if(head ==NULL)
          return;
     linka*pre, *cur, *ne;
     pre=head;
     cur=head-&gt;next;
     while(cur)
     {
          ne = cur-&gt;next;
          cur-&gt;next = pre;
          pre = cur;
          cur = ne;
     }
     head-&gt;next = NULL;
     head = pre;
}
</pre>


<p>还有一种利用递归的方法。这种方法的基本思想是在反转当前节点之前先调用递归函数反转后续节点。源代码如下。不过这个方法有一个缺点，就是在反转后的最后一个结点会形成一个环，所以必须将函数的返回的节点的next域置为NULL。因为要改变head指针，所以我用了引用。算法的源代码如下：</p>


<pre>
linka* reverse(linka* p,linka*&amp; head)
{
     if(p == NULL || p-&gt;next == NULL)
     {
          head=p;
          return p;
     }
     else
     {
          linka* tmp = reverse(p-&gt;next,head);
          tmp-&gt;next = p;
          return p;
     }
}
</pre>]]>
    </content>
</entry>
<entry>
    <title>判断两个数组中是否存在相同的数字</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/04/post_11.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=237" title="判断两个数组中是否存在相同的数字" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.237</id>
    
    <published>2006-04-02T14:18:47Z</published>
    <updated>2006-04-03T02:33:39Z</updated>
    
    <summary>给定两个排好序的数组，怎样高效得判断这两个数组中存在相同的数字？ 这个问题首先想到的是一个O(nlogn)的算法。就是任意挑选一个数组，遍历这个数组的所有元素，遍历过程中，在另一个数组中对第一个数组中的每个元素进行binary search。用C++实现代码如下：...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="编程笔试题" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>给定两个排好序的数组，怎样高效得判断这两个数组中存在相同的数字？<br />
这个问题首先想到的是一个O(nlogn)的算法。就是任意挑选一个数组，遍历这个数组的所有元素，遍历过程中，在另一个数组中对第一个数组中的每个元素进行binary search。用C++实现代码如下：</p>]]>
        <![CDATA[<pre>
bool findcommon(int a[],int size1,int b[],int size2)
{
     int i;
     for(i=0;i&lt;size1;i++)
     {
          int start=0,end=size2-1,mid;
          while(start&lt;=end)
          {
               mid=(start+end)/2;
               if(a[i]==b[mid])
                    return true;
               else if (a[i]&lt;b[mid])
                    end=mid-1;
               else
                    start=mid+1;
          }
     }
     return false;
}
</pre>


<p>后来发现有一个 O(n)算法。因为两个数组都是排好序的。所以只要一次遍历就行了。首先设两个下标，分别初始化为两个数组的起始地址，依次向前推进 。推进的规则是比较两个 数组中的数字，小的那个数组的下标向前推进一步，直到任何一个数组的下标到达数组末尾时，如果这时还没碰到相同的数字，说明数组中没有相同的数字。</p>


<pre>
bool findcommon2(int a[], int size1, int b[], int size2)
{
     int i=0,j=0;
     while(i&lt;size1 &amp;&amp; j&lt;size2)
     {
          if(a[i]==b[j])
               return true;
          if(a[i]&gt;b[j])
               j++;
          if(a[i]&lt;b[j])
               i++;
     }
     return false;
}
</pre>]]>
    </content>
</entry>
<entry>
    <title>最大子序列</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/04/post_10.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=236" title="最大子序列" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.236</id>
    
    <published>2006-04-02T04:00:49Z</published>
    <updated>2006-04-02T04:55:56Z</updated>
    
    <summary>问题： 给定一整数序列A1， A2，... An （可能有负数），求A1~An的一个子序列Ai~Aj，使得Ai到Aj的和最大 例如： 整数序列-2, 11, -4, 13, -5, 2, -5, -3, 12, -9的最大子序列的和为21。 对于这个问题，最简单也是最容易想到的那就是穷举所有子序列的方法。利用三重循环，依次求出所有子序列的和然后取最大的那个。当然算法复杂度会达到O(n^3)。显然这种方法不是最优的，下面给出一个算法复杂度为O(n)的线性算法实现，算法的来源于Programming Pearls一书。...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="编程笔试题" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>问题：<br />
       给定一整数序列A1， A2，... An （可能有负数），求A1~An的一个子序列Ai~Aj，使得Ai到Aj的和最大<br />
例如：<br />
       整数序列-2, 11, -4, 13, -5, 2, -5, -3, 12, -9的最大子序列的和为21。<br />
对于这个问题，最简单也是最容易想到的那就是穷举所有子序列的方法。利用三重循环，依次求出所有子序列的和然后取最大的那个。当然算法复杂度会达到O(n^3)。显然这种方法不是最优的，下面给出一个算法复杂度为O(n)的线性算法实现，算法的来源于<a href="http://netlib.bell-labs.com/cm/cs/pearls/">Programming Pearls</a>一书。</p>]]>
        <![CDATA[<p>在给出线性算法之前，先来看一个对穷举算法进行优化的算法，它的算法复杂度为O(n^2)。其实这个算法只是对对穷举算法稍微做了一些修改：其实子序列的和我们并不需要每次都重新计算一遍。假设Sum(i, j)是A[i] ... A[j]的和，那么Sum(i, j+1) = Sum(i, j) + A[j+1]。利用这一个递推，我们就可以得到下面这个算法：</p>


<pre>
int max_sub(int a[],int size)
{
     int i,j,v,max=a[0];
     for(i=0;i&lt;size;i++)
     {
          v=0;
          for(j=i;j&lt;size;j++)
          {
               v=v+a[j];//Sum(i, j+1) = Sum(i, j) + A[j+1]
               if(v&gt;max)
                    max=v;
          }
     }
     return max;
}
</pre>


<p>那怎样才能达到线性复杂度呢？这里运用动态规划的思想。先看一下源代码实现：</p>


<pre>
int max_sub2(int a[], int size)
{
     int i,max=0,temp_sum=0;
     for(i=0;i&lt;size;i++)
     {
          temp_sum+=a[i];
          if(temp_sum&gt;max)
               max=temp_sum;
          else if(temp_sum&lt;0)
               temp_sum=0;
     }
     return max;
}
</pre>


<p>在这一遍扫描数组当中，从左到右记录当前子序列的和temp_sum，若这个和不断增加，那么最大子序列的和max也不断增加(不断更新max)。如果往前扫描中遇到负数，那么当前子序列的和将会减小。此时temp_sum 将会小于max，当然max也就不更新。如果temp_sum降到0时，说明前面已经扫描的那一段就可以抛弃了，这时将temp_sum置为0。然后，temp_sum将从后面开始将这个子段进行分析，若有比当前max大的子段，继续更新max。这样一趟扫描结果也就出来了。</p>]]>
    </content>
</entry>
<entry>
    <title>找出单向链表的中间结点</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/03/post_9.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=235" title="找出单向链表的中间结点" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.235</id>
    
    <published>2006-03-31T14:33:51Z</published>
    <updated>2006-03-31T14:48:08Z</updated>
    
    <summary><![CDATA[这道题和解判断链表是否存在环，我用的是非常类似的方法，只不过结束循环的条件和函数返回值不一样罢了。设置两个指针p1，p2。每次循环p1向前走一步，p2向前走两步。当p2到达链表的末尾时，p1指向的时链表的中间。 link* mid(link* head) { link* p1,*p2; p1=p2=head; if(head==NULL || head-&gt;next==NULL) return head; do { p1=p1-&gt;next; p2=p2-&gt;next-&gt;next; } while(p2 &amp;&amp; p2-&gt;next); return p1; }...]]></summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="编程笔试题" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>这道题和解<a href="http://www.zhuxinquan.com/cn/2006/03/post_6.html">判断链表是否存在环</a>，我用的是非常类似的方法，只不过结束循环的条件和函数返回值不一样罢了。设置两个指针p1，p2。每次循环p1向前走一步，p2向前走两步。当p2到达链表的末尾时，p1指向的时链表的中间。</p>


<pre>
link* mid(link* head)
{
	link* p1,*p2;
	p1=p2=head;
	if(head==NULL || head-&gt;next==NULL)
		return head;
	do {
		p1=p1-&gt;next;
		p2=p2-&gt;next-&gt;next;
	} while(p2 &amp;&amp; p2-&gt;next);
	return p1;
}
</pre>]]>
        
    </content>
</entry>
<entry>
    <title>平面体检一百关</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/03/post_8.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=234" title="平面体检一百关" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.234</id>
    
    <published>2006-03-31T09:52:01Z</published>
    <updated>2006-03-31T09:59:42Z</updated>
    
    <summary>从这里看到的，蛮实用的，就摘了下来，看看你能过几关。身体还是革命的本钱啊，大家注意自己的身体哦。 粉色题板 1. 每周一次晨爱 2. 在干净的床上裸睡（） 3. 生理期不吃巧克力，因为会加重痛经 4. 经期只穿无钢圈内衣 5. 养成记录生理周期的习惯 6. 通过运动而非调整型内衣来塑造曲线 7. 不翘二郎腿，以免压迫神经 8. ML后去一次洗手间 9. 贴身衣物不干洗 10. 拉风的丁字裤不适宜日常穿着 11. 不穿标有“免烫”标识的衣服 12. 丝巾扎得过紧会导致头晕 13. 去年的衣服要进行曝晒后才可以穿 14. 如非必要，不使用卫生护垫 15. 定期检查化妆品的保质期 16. 洗浴后一小时再化妆 17. 每周只用一次清洗液，用温水清洁私密部位 18. 即使爱美，也不要在耳朵上部的外缘软骨部位穿耳洞 19. 染发频率控制在半年一次...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="生活" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>从<a href="http://www.99wed.com/iforum/content/61615_1.htm">这里</a>看到的，蛮实用的，就摘了下来，看看你能过几关。身体还是革命的本钱啊，大家注意自己的身体哦。</p>

<p>粉色题板<br />
1. 每周一次晨爱<br />
2. 在干净的床上裸睡（）<br />
3. 生理期不吃巧克力，因为会加重痛经<br />
4. 经期只穿无钢圈内衣<br />
5. 养成记录生理周期的习惯<br />
6. 通过运动而非调整型内衣来塑造曲线<br />
7. 不翘二郎腿，以免压迫神经<br />
8. ML后去一次洗手间<br />
9. 贴身衣物不干洗<br />
10. 拉风的丁字裤不适宜日常穿着<br />
11. 不穿标有“免烫”标识的衣服<br />
12. 丝巾扎得过紧会导致头晕<br />
13. 去年的衣服要进行曝晒后才可以穿<br />
14. 如非必要，不使用卫生护垫<br />
15. 定期检查化妆品的保质期<br />
16. 洗浴后一小时再化妆<br />
17. 每周只用一次清洗液，用温水清洁私密部位<br />
18. 即使爱美，也不要在耳朵上部的外缘软骨部位穿耳洞<br />
19. 染发频率控制在半年一次<br />
20. 了解自己的家庭病史，特别是母亲和外婆的病史<br />
</p>]]>
        <![CDATA[<p>蓝色题板<br />
21. 每天踏进办公室，先将窗户打开透气，再坐下来工作<br />
22. 如果一天要接听5小时电话，使用无线耳机<br />
23. 复印文件时，与复印机保持至少一米<br />
24. 只在非常必要时才使用滴眼液<br />
25. 不趴在办公桌上午睡<br />
26. 该午休的时候不玩电脑游戏<br />
27. 在办公室为自己准备小靠垫，放在腰部<br />
28. 从不戴着眼镜接听手机<br />
29. 别让电脑包围你，它的侧、背面辐射更凶<br />
30. 不要将笔记本电脑放在膝上使用<br />
31. 在办公桌上养一盆仙人掌，帮助吸收辐射<br />
32. 阅读完报纸后，记得清洗掉沾在手上的油墨<br />
33. 每30分钟伸一次懒腰<br />
34. 办公室地毯定期清洗杀虫<br />
35. 用完电脑后要清洁面部及手部，清除辐射微尘<br />
36. 下班时将洗净的水杯倒扣在办公桌上<br />
37. 单肩的短带挎包会加重肩周炎症状<br />
38. 公文包时的口红与签字笔分格存放<br />
39. 每天保证有2小时以上的时间，让脚从高跟鞋时解放出来<br />
40. 每周晚过22：00的加班不超过一次</p>

<p><br />
绿色题板<br />
41. 浴室保持干燥，防止霉菌滋生<br />
42. 沐浴不超过10分钟<br />
43. 不要坐在马桶上看报纸<br />
44. 用温水刷牙，同时刷刷舌头<br />
45. 用冷热水交替洗脸<br />
46. 不用塑料器皿盛装热水<br />
47. 定期清理冰箱<br />
48. 微波炉在工作时，请离开厨房<br />
49. 使用抽油烟机<br />
50. 晚餐时关掉电视机<br />
51. 尽量避免使用厚绒布窗帘<br />
52. 杀虫剂和清洁剂要放在远离起居场所的储物间<br />
53. 用天然的花香或果香代替芳香剂<br />
54. 和宠物嬉戏完要认真洗手<br />
55. 冬天居室里的加湿器使用纯净水<br />
56. 不要贪图方便将电脑带进卧室<br />
57. 不要把手机放在枕边充当闹钟<br />
58. 别在临睡前服药，安眠药也要提前半小时服用<br />
59. 头发没干时，别急着入睡<br />
60. 卧室的房间要用柔和色彩</p>

<p><br />
黄色题板<br />
61. 在牛奶和豆浆之间，选择后者<br />
62. 觉得还可以再吃半碗饭时，离开餐桌<br />
63. 如果身体不感到饥渴，每天只需饮用4杯水<br />
64. 多喝酸奶<br />
65. 无论什么原因，都别抽烟<br />
66. 在食谱里添加杂粮和蔬菜<br />
67. 饮绿茶胜过红茶<br />
68. 重视早餐多过晚餐<br />
69. 控制盐的用量<br />
70. 起床后先刷牙，再喝水<br />
71. 经常嚼口香糖<br />
72. 一早一晚，两个苹果可以有效改善便秘<br />
73. 纯素食可能导致荷尔蒙分泌异常，造成不孕<br />
74. 每周至少吃一次鱼<br />
75. 远离可乐等碳酸饮料<br />
76. 不喝久煮的火锅汤<br />
77. 没有果汁牛奶这回事，它们是天生的冤家<br />
78. 饭前吃水果胜过饭后<br />
79. 睡前可以来一杯红葡萄酒<br />
80. 喝咖啡可能引起女性骨质疏松</p>

<p><br />
橙色题板<br />
81. 多享受早晨8－9点的阳光<br />
82. 跑步、骑脚踏车等运动可以保持优美的腿部线条<br />
83. 热水泡脚可有效预防静脉曲张<br />
84. 精神极度疲倦时并不适宜以运动减压，休息更重要<br />
85. 冬季少做户外运动<br />
86. 10层以下，不乘坐电梯<br />
87. 每三个月改变一次你的健身菜单<br />
88. 每天运动半小时，而非周末运动3小时<br />
89. 运动前先卸妆<br />
90. 边看电视边做柔软体操<br />
91. 在游泳池里一定要戴上泳帽和泳镜<br />
92. 经常散步<br />
93. 午休也是健身的好时间，不一定非等到晚上<br />
94. 光脚穿运动鞋固然舒服，却对健康不利<br />
95. 睡半硬的床铺更有利于颈椎健康<br />
96. 去正规的医院而非美容院接受按摩<br />
97. 非运动状态下不喝功能性饮料<br />
98. 运动后休息半小时再入浴<br />
99. 不在过吵的健身房中锻炼<br />
100. 正确的姿势比专程去健身更有效</p>]]>
    </content>
</entry>
<entry>
    <title>按单词反转字符串</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/03/post_7.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=233" title="按单词反转字符串" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.233</id>
    
    <published>2006-03-31T03:58:08Z</published>
    <updated>2006-03-31T06:01:14Z</updated>
    
    <summary>并不是简单的字符串反转，而是按给定字符串里的单词将字符串倒转过来，就是说字符串里面的单词还是保持原来的顺序，这里的每个单词用空格分开。例如： Here is www.zhuxinquan.com 经过反转后变为： www.zhuxinquan.com is Here 如果只是简单的将所有字符串翻转的话，可以遍历字符串，将第一个字符和最后一个交换，第二个和倒数第二个交换，依次循环。其实按照单词反转的话可以在第一遍遍历的基础上，再遍历一遍字符串，对每一个单词再反转一次。这样每个单词又恢复了原来的顺序。...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="编程笔试题" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>并不是简单的字符串反转，而是按给定字符串里的单词将字符串倒转过来，就是说字符串里面的单词还是保持原来的顺序，这里的每个单词用空格分开。例如：<br />
Here is www.zhuxinquan.com<br />
经过反转后变为：<br />
www.zhuxinquan.com is Here<br />
如果只是简单的将所有字符串翻转的话，可以遍历字符串，将第一个字符和最后一个交换，第二个和倒数第二个交换，依次循环。其实按照单词反转的话可以在第一遍遍历的基础上，再遍历一遍字符串，对每一个单词再反转一次。这样每个单词又恢复了原来的顺序。</p>]]>
        <![CDATA[<pre>
char* reverse_word(const char* str)
{
     int len = strlen(str);
     char* restr = new char[len+1];
     strcpy(restr,str);
     int i,j;
     for(i=0,j=len-1;i&lt;j;i++,j--)
     {
          char temp=restr[i];
          restr[i]=restr[j];
          restr[j]=temp;
     }
     int k=0;
     while(k&lt;len)
     {
          i=j=k;
          while(restr[j]!=' ' &amp;&amp; restr[j]!='\0' )
               j++;
          k=j+1;
          j--;
          for(;i&lt;j;i++,j--)
          {
               char temp=restr[i];
               restr[i]=restr[j];
               restr[j]=temp;
          }
     }
     return restr;
}
</pre>


<p>如果考虑空间和时间的优化的话，当然可以将上面代码里两个字符串交换部分改为异或实现。<br />
例如将</p>


<pre> 
          char temp=restr[i];
          restr[i]=restr[j];
          restr[j]=temp;
</pre>

<p> <br />
改为</p>


<pre> 
          restr[i]^=restr[j];
          restr[j]^=restr[i];
          restr[i]^=restr[j];
</pre>]]>
    </content>
</entry>
<entry>
    <title>判断链表是否存在环</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/03/post_6.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=232" title="判断链表是否存在环" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.232</id>
    
    <published>2006-03-30T12:36:16Z</published>
    <updated>2006-04-15T07:56:32Z</updated>
    
    <summary><![CDATA[问题：判断一个链表是否存在环，例如下面这个链表就存在一个环： 例如N1-&gt;N2-&gt;N3-&gt;N4-&gt;N5-&gt;N2就是一个有环的链表，环的开始结点是N5 这里有一个比较简单的解法。设置两个指针p1，p2。每次循环p1向前走一步，p2向前走两步。直到p2碰到NULL指针或者两个指针相等结束循环。如果两个指针相等则说明存在环。 struct link { int data; link* next; }; bool IsLoop(link* head) { link* p1=head, *p2 = head; if (head ==NULL || head-&gt;next ==NULL) { return false; } do{ p1= p1-&gt;next; p2 = p2-&gt;next-&gt;next; } while(p2 &amp;&amp; p2-&gt;next...]]></summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="编程笔试题" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>问题：判断一个链表是否存在环，例如下面这个链表就存在一个环：<br />
例如N1-&gt;N2-&gt;N3-&gt;N4-&gt;N5-&gt;N2就是一个有环的链表，环的开始结点是N5</p>

<p>这里有一个比较简单的解法。设置两个指针p1，p2。每次循环p1向前走一步，p2向前走两步。直到p2碰到NULL指针或者两个指针相等结束循环。如果两个指针相等则说明存在环。</p>


<pre>
struct link {
    int data;
    link* next;
};

bool IsLoop(link* head)
{
    link* p1=head, *p2 = head;
	if (head ==NULL || head-&gt;next ==NULL) {
		return false;
	}
    do{
        p1= p1-&gt;next;
        p2 = p2-&gt;next-&gt;next;
    } while(p2 &amp;&amp; p2-&gt;next &amp;&amp; p1!=p2);     
	if(p1 == p2)
		return true;
	else
		return false;
}
</pre>]]>
        
    </content>
</entry>
<entry>
    <title>XML初探（一）</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/03/xml.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=230" title="XML初探（一）" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.230</id>
    
    <published>2006-03-28T07:54:37Z</published>
    <updated>2006-03-31T09:46:59Z</updated>
    
    <summary>趁工作的空闲时间，随手拿了一本Erik T. Ray写的 《Learning XML》 翻了一下。一直想了解一下XML，确苦于没有时间。还有就是技术这个东西，你学了不用，过了一段时间就会全部忘光光。可惜我的项目用不到XML，不过反正没事，就看了一个下午，觉得容易忘记的一些东西记了一点下来。...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="XML" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>趁工作的空闲时间，随手拿了一本Erik T. Ray写的 《Learning <span class="caps">XML</span>》 翻了一下。一直想了解一下XML，确苦于没有时间。还有就是技术这个东西，你学了不用，过了一段时间就会全部忘光光。可惜我的项目用不到XML，不过反正没事，就看了一个下午，觉得容易忘记的一些东西记了一点下来。</p>]]>
        <![CDATA[<p>Elements, attributes, namespaces, and entities are the most important markup objects in <span class="caps">XML.</span></p>

<p><strong>保留属性</strong><br />
<span class="caps">XML</span>可以定义自己的元素(element)包括这个元素的属性，但下没保留的元素属性不能重新定义：<br />
<em>xml:lang</em><br />
Classifies an element by the language of its content. For example, xml:lang="en" describes an element<br />
as having English content. This is useful for creating conditional text, which is content selected by an<br />
<span class="caps">XML </span>processor based on criteria such as what language the user wants to view a document in. We'll<br />
return to this topic in Chapter 7.<br />
<em>xml:space</em><br />
Specifies whether whitespace should be preserved in an element's content. If set to preserve, any <span class="caps">XML</span><br />
processor displaying the document should honor all newlines, spaces, and tabs in the element's<br />
content. If it is set to default, then the processor can do whatever it wants with whitespace (i.e., it<br />
sets its own default). If the xml:space attribute is omitted, the processor preserves whitespace by<br />
default. Thus, if you want to compress whitespace in an element, set the attribute xml:space="default"<br />
and make sure you are using an <span class="caps">XML </span>processor whose default is to remove extra whitespace.<br />
<em>xml:link</em><br />
告诉 XLink processor 这是一个xlink element。相当于HTML里面的超链接<br />
<em>xml:attribute</em><br />
In addition to xml:link, XLink relies on a number of attribute names. But to prevent conflict with other<br />
potential uses of those attributes, XLink defines the xml:attribute attribute, which allows you to<br />
"remap" those special attributes. That is, you can say, "When XLink is looking for an attribute called<br />
title, I want you to use the attribute called linkname instead." This attribute is also discussed in more<br />
detail in Chapter 3.<br />
<strong>名字空间 (namespace)</strong><br />
名字空间是属性和元素的集合，为了解决元素和属性的名字冲突的问题，XML也有命名空间。处于不用的名字空间的元素和属性的名字可以不同。名字空间声明在一个某一个element中，所有被这个element包含的element都将属于这个名字空间<br />
例如下面的例子声明了一个名字空间bob，并且告诉XML parser 这里有一个名字空间的属性</p>


<pre> &lt;part-catalog xmlns:bob=&quot;http://www.bobco.com/&quot;&gt; </pre>


<p><strong>实体 (entity)</strong><br />
实体是XML内容中的占位符(placeholder)，一旦你定义一个实体后，接下来就可以用这个实体标识符在XML文档里来表示相同的内容。例如下面的文档定义了一个client 实体。在文档中，&client; 引用了该实体。</p>


<pre>
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!DOCTYPE message SYSTEM &quot;/xmlstuff/dtds/message.dtd&quot;
[
&lt;!ENTITY client &quot;Mr. Rufus Xavier Sasperilla&quot;&gt;
]&gt;
&lt;message&gt;
&lt;opening&gt;Dear &amp;client;&lt;/opening&gt;
&lt;body&gt;We have an exciting opportunity for you! 
&lt;/body&gt;
&lt;/message&gt;
</pre>


<p>在XML中有多种实体。</p>]]>
    </content>
</entry>
<entry>
    <title>删除数组中重复的数字</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/03/post_5.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=229" title="删除数组中重复的数字" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.229</id>
    
    <published>2006-03-27T13:41:47Z</published>
    <updated>2006-03-30T08:48:00Z</updated>
    
    <summary>问题：一个动态长度可变的数字序列，以数字0为结束标志，要求将重复的数字用一个数字代替，例如： 将数组 1,1,1,2,2,2,2,2,7,7,1,5,5,5,0 转变成1,2,7,1,5,0 问题比较简单，要注意的是这个数组是动态的。所以避免麻烦我还是用了STL的vector。...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="编程笔试题" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>问题：一个动态长度可变的数字序列，以数字0为结束标志，要求将重复的数字用一个数字代替，例如：<br />
将数组 1,1,1,2,2,2,2,2,7,7,1,5,5,5,0 转变成1,2,7,1,5,0</p>

<p>问题比较简单，要注意的是这个数组是动态的。所以避免麻烦我还是用了STL的vector。</p>]]>
        <![CDATA[<pre>
#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;
//remove the duplicated numbers in an intger array, the array was end with 0;
//e.g. 1,1,1,2,2,5,4,4,4,4,1,0 ---&gt;1,2,5,4,1,0
void static remove_duplicated(int a[], vector&lt;int&gt;&amp; _st)
{
	_st.push_back(a[0]);
	for(int i=1;_st[_st.size()-1]!=0;i++)
	{
		if(a[i-1]!=a[i])
			_st.push_back(a[i]);
	}
}
</pre>


<p>当然如果可以改变原来的数组的话，可以不用STL，仅需要指针操作就可以了。下面这个程序将修改原来数组的内容。</p>


<pre>
void static remove_duplicated2(int a[])
{
	if(a[0]==0 || a==NULL)
		return;
	int insert=1,current=1;
	while(a[current]!=0)
	{
		if(a[current]!=a[current-1])
		{
			a[insert]=a[current];
			insert++;
			current++;
		}
		else
			current++;
	}
	a[insert]=0;
}
</pre>]]>
    </content>
</entry>
<entry>
    <title>字符串反转</title>
    <link rel="alternate" type="text/html" href="http://www.zhuxinquan.com/cn/2006/03/post_4.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.zhuxinquan.com/mt_zxq/mt-atom.cgi/weblog/blog_id=4/entry_id=228" title="字符串反转" />
    <id>tag:www.zhuxinquan.com,2006:/cn//4.228</id>
    
    <published>2006-03-27T10:24:44Z</published>
    <updated>2006-03-27T13:32:42Z</updated>
    
    <summary>我没有记错的话是一道MSN的笔试题，网上无意中看到的，拿来做了一下。题目是这样的，给定一个字符串，一个这个字符串的子串，将第一个字符串反转，但保留子串的顺序不变。例如： 输入： 第一个字符串: &quot;This is zhuxinquan&apos;s Chinese site: http://www.zhuxinquan.com/cn&quot; 子串: &quot;zhuxinquan&quot; 输出： &quot;nc/moc.zhuxinquan.www//:ptth :etis esenihC s&apos;zhuxinquan si sihT&quot; 一般的方法是先扫描一边第一个字符串，然后用stack把它反转，同时记录下子串出现的位置。然后再扫描一遍把记录下来的子串再用stack反转。我用的方法是用一遍扫描数组的方法。扫描中如果发现子串，就将子串倒过来压入堆栈。 最后再将堆栈里的字符弹出，这样子串又恢复了原来的顺序。源代码如下：...</summary>
    <author>
        <name>Zhu Xinquan</name>
        
    </author>
            <category term="编程笔试题" />
    
    <content type="html" xml:lang="en" xml:base="http://www.zhuxinquan.com/cn/">
        <![CDATA[<p>我没有记错的话是一道MSN的笔试题，网上无意中看到的，拿来做了一下。题目是这样的，给定一个字符串，一个这个字符串的子串，将第一个字符串反转，但保留子串的顺序不变。例如：<br />
输入： 第一个字符串: "This is zhuxinquan's Chinese site: http://www.zhuxinquan.com/cn"<br />
            子串: "zhuxinquan"<br />
输出： "nc/moc.zhuxinquan.www//:ptth :etis esenihC s'zhuxinquan si sihT"<br />
一般的方法是先扫描一边第一个字符串，然后用stack把它反转，同时记录下子串出现的位置。然后再扫描一遍把记录下来的子串再用stack反转。我用的方法是用一遍扫描数组的方法。扫描中如果发现子串，就将子串倒过来压入堆栈。<br />
最后再将堆栈里的字符弹出，这样子串又恢复了原来的顺序。源代码如下：</p>

<p>          </p>]]>
        <![CDATA[<pre>
#include &lt;iostream&gt;
#include &lt;cassert&gt;
#include &lt;stack&gt;
using namespace std;
//reverse the string 's1' except the substring 'token'.
const char* reverse(const char* s1, const char* token)
{
	assert(s1 &amp;&amp; token);
	stack&lt;char&gt; stack1;
	const char* ptoken = token, *head = s1, *rear = s1;
	while (*head != '\0')
	{
		while(*head!= '\0' &amp;&amp; *ptoken == *head)
		{
			ptoken++;
			head++;
		}
		if(*ptoken == '\0')//contain the token
		{
			const char* p;
			for(p=head-1;p&gt;=rear;p--)
				stack1.push(*p);

			ptoken = token;
			rear = head;
		}
		else
		{
			stack1.push(*rear);
			head=++rear;
			ptoken = token;
		}
	}
	char * return_v = new char[strlen(s1)+1];
	int i=0;
	while(!stack1.empty())
	{
		return_v[i++] = stack1.top();
		stack1.pop();
	}
	return_v[i]='\0';
	return return_v;
}
int main(int argc, char* argv[])
{
	
	cout&lt;&lt;&quot;This is zhuxinquan's Chinese site: http://www.zhuxinquan.com/cn\n&quot;;
	cout&lt;&lt;reverse(&quot;This is zhuxinquan's Chinese site: http://www.zhuxinquan.com/cn&quot;,&quot;zhuxinquan&quot;);
	return 0;
}
</pre>]]>
    </content>
</entry>

</feed> 

