<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>{finalbug.org} &#187; C语言</title>
	<atom:link href="http://finalbug.org/tag/c-language/feed/" rel="self" type="application/rss+xml" />
	<link>http://finalbug.org</link>
	<description>Keep it simple &#38; stupid</description>
	<lastBuildDate>Sun, 20 May 2012 06:04:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>八皇后问题</title>
		<link>http://finalbug.org/2008/12/%e5%85%ab%e7%9a%87%e5%90%8e%e9%97%ae%e9%a2%98/</link>
		<comments>http://finalbug.org/2008/12/%e5%85%ab%e7%9a%87%e5%90%8e%e9%97%ae%e9%a2%98/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 01:14:27 +0000</pubDate>
		<dc:creator>Tang Bin</dc:creator>
				<category><![CDATA[学习笔记]]></category>
		<category><![CDATA[C语言]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.finalbug.org/?p=482</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>虽然八皇后问题很早就知道，但是也没有自己实现过，上周用C实现了一下，发现有很多算法还可以进一步优化的。<br />
<span id="more-482"></span><br />
基本的思路是这样：<br />
1，创建一个长度为8的数组（queens），每一项表示棋盘上每一行皇后在的位置（0-7）。-1表示还没有开始算这一行，或这一行找不到可以放置皇后的位置；<br />
2，先将第一行的第一个位置放上皇后（queens[0] = 0），然后依次寻找下一行可以放置的位置；<br />
3，如果找不到位置，则返回上一行寻找下一个可用的位置；<br />
4，如果已经找到最后一行，则输出当前的布局，然后再返回上一行寻找下一个可以用的位置；<br />
5，如果已经回到第一行，没有找到下一个可以用的位置，程序退出。</p>
<p>一共找到92个。和问题的标准答案吻合。</p>
<p>最粗糙的第一版代码附上：</p>
<p>[code lang="c"]<br />
#include <stdio.h><br />
#include <stdlib.h></p>
<p>#define LINE 8</p>
<p>void printChess(int *queens);<br />
int checkLine(int *queens, int checkLine);</p>
<p>int<br />
main(void)<br />
{<br />
	int queens[] = {0, -1, -1, -1, -1, -1, -1, -1};<br />
	int thisLine = 1;<br />
	while(1)<br />
	{<br />
		queens[thisLine] = checkLine(queens, thisLine);<br />
		if(queens[thisLine] == -1)<br />
		{<br />
			thisLine -= 1;<br />
			if(thisLine < 0)<br />
			{<br />
				break;<br />
			}<br />
		}<br />
		else if(thisLine == (LINE - 1))<br />
		{<br />
			printChess(queens);<br />
			queens[thisLine] = -1;<br />
			thisLine -= 1;<br />
		}<br />
		else<br />
		{<br />
			thisLine += 1;<br />
		}<br />
	}<br />
	return EXIT_SUCCESS;<br />
}</p>
<p>int<br />
checkLine(int *queens, int checkLine)<br />
{<br />
	int i;<br />
	int startNum = *(queens + checkLine);<br />
	for(i = startNum + 1 ; i < LINE ; i += 1)<br />
	{<br />
		int getValue = 0;<br />
		int targetLine = checkLine;<br />
		while(--targetLine >= 0)<br />
		{<br />
			int step = checkLine - targetLine;<br />
			int target = *(queens + targetLine);<br />
			if((target == i) || (target == i - step) || (target == i + step))<br />
			{<br />
				break;<br />
			}<br />
			else<br />
			{<br />
				getValue += 1;<br />
			}<br />
		}<br />
		if(getValue == checkLine)<br />
		{<br />
			return i;<br />
		}<br />
	}<br />
	return -1;<br />
}</p>
<p>void<br />
printChess(int *queens)<br />
{<br />
	static int count = 0;<br />
	printf("nOutput %d:n", ++count);</p>
<p>	int i, j;<br />
	for(i = 0 ; i < LINE ; i += 1)<br />
	{<br />
		for(j = 0 ; j < LINE ; j += 1)<br />
		{<br />
			printf("%c ", (j == queens[i] ? 'Q' : '*'));<br />
		}<br />
		printf("%s", "n");<br />
	}<br />
}<br />
[/code] </p>
]]></content:encoded>
			<wfw:commentRss>http://finalbug.org/2008/12/%e5%85%ab%e7%9a%87%e5%90%8e%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>++ problem in C</title>
		<link>http://finalbug.org/2008/11/problem-in-c/</link>
		<comments>http://finalbug.org/2008/11/problem-in-c/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 02:10:39 +0000</pubDate>
		<dc:creator>Tang Bin</dc:creator>
				<category><![CDATA[学习笔记]]></category>
		<category><![CDATA[C语言]]></category>

		<guid isPermaLink="false">http://www.finalbug.org/?p=466</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>C已经忘得差不多了，再学习，遇到个++问题。记录一下。</p>
<p><span id="more-2834"></span></p>
<p>following codes are wrote in C by using code::blocks + WinGW.<br />
[code]<br />
int a = 0;<br />
a = (a++) + (a++);<br />
printf(&quot;%d&quot;, a);<br />
[/code]</p>
<p>in my opinion (most of which from ActionScript coding), first “a++” make a&#8217;s value increase to “1&#8243; and return 0, next “a++” make a&#8217;s value increase to “2&#8243; and return 1, so the resulte is 0 + 1 and assign back to a, so print 1. but it shows 2.</p>
<p>i modified code to:</p>
<p>[code]<br />
int a = 0;<br />
int b;<br />
b = (a++) + (a++);<br />
printf(&quot;%d&quot;, b);<br />
[/code]</p>
<p>it is strange, value “0&#8243; is shown on screen. much different from Java and AS as i known before.</p>
<p>i am not sure, but is have these results:</p>
<p><del datetime="2008-11-19T04:42:02+00:00">1, the strange print value is caused by compiler.<br />
2, in WinGW, if you use ++(&#8211;) to the same value, it returns the original value.<br />
3, in WinGW, one variate cannot be used in both L-exp and R-exp.</del></p>
<p>i think i find the answer, see <a href="http://blog.ez2learn.com/2008/09/27/evil-undefined-behavior/">this</a>, and <a href="http://en.wikipedia.org/wiki/Nasal_demon">this</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://finalbug.org/2008/11/problem-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Return to the real world(1)</title>
		<link>http://finalbug.org/2008/06/back-to-the-real-world1-2/</link>
		<comments>http://finalbug.org/2008/06/back-to-the-real-world1-2/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 09:09:56 +0000</pubDate>
		<dc:creator>Tang Bin</dc:creator>
				<category><![CDATA[学习笔记]]></category>
		<category><![CDATA[C语言]]></category>

		<guid isPermaLink="false">http://finalbug.cn/?p=172</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>对我来说最有用的vi命令：</p>
<ul>
<li>ZZ：保存并退出</li>
<li>:w 文件名：将当前文件保存为“文件名”，不能覆盖同名文件</li>
<li>:w! 文件名：将当前文件保存为“文件名”，覆盖同名</li>
<li>:set nu：显示行号</li>
<li>G：移动到末行行首</li>
<li>nG：移动到第n行行首</li>
<li>&lt;ctrl&gt;+g：报告光标位置</li>
<li>dd：删除当前行</li>
<li>u：取消前一次操作</li>
<li>?string&lt;enter&gt;：查找字符串</li>
<li>n：重复向前查找</li>
<li>N：重复向后查找</li>
<li>:1,$s/oldstr/newstr/g：在整个文件中，用newstr替换oldstr</li>
<li>yw：将当前行光标以后的文字复制</li>
<li>nyw：将当前行光标后的n个文字复制</li>
<li>yy：复制当前行</li>
<li>p：将复制的内容插入到当前光标后</li>
</ul>
<p>关于C：</p>
<ul>
<li>建议用全小写加下划线的方式命名，不推荐骆驼命名法。</li>
<li>++运算只针对变量，不能针对表达式。</li>
<li>C中的变量有效字符只有字母数字和下划线。</li>
<li>以“”括起来的字符是字符串常量，系统会自动在结尾添加 标示字符串的结束，‘’的是字符。</li>
<li>C中的强制类型转换格式是(int)a，而不是int(a)。</li>
<li>C函数中，实参对形参的数据传递是单向的“值传递”。</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://finalbug.org/2008/06/back-to-the-real-world1-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

