當(dāng)k比較小的時候,可獲得O(n)的時間復(fù)雜度.
#include?
<
iostream
>
using?namespace?std;

const
?
int
?arrLen?
=
?
5
;


void
?countingSort(
int
?
*
a,?
int
?
*
b,?
int
?k)?
{
????
//
a數(shù)組元素在[0..k]
????
int
?i,?j;
????
int
?
*
c?
=
?
new
?
int
[k
+
1
];
????
for
?(i
=
0
;?i
<=
k;?i
++
)?c[i]?
=
?
0
;
????
for
?(i
=
0
;?i
<
arrLen;?i
++
)?c[a[i]]
++
;
????
//
包含小于或等于i的元素個數(shù)
????
for
?(i
=
1
;?i
<=
k;?i
++
)?c[i]?
+=
?c[i
-
1
];

????
for
?(j
=
arrLen
-
1
;?j
>=
0
;?j
--
)?
{
????????b[c[a[j]]
-
1
]?
=
?a[j];
????????c[a[j]]
--
;
????}
}
int
?main()?
{

????
int
?a[arrLen]?
=
?
{
4
,?
4
,?
3
,?
1
,?
1
}
;
????
int
?b[arrLen];
????countingSort(a,?b,?
5
);

????
for
?(
int
?i
=
0
;?i
<
5
;?i
++
)?
{
????????printf(
"
%d?
"
,?b[i]);
????}
????printf(
"
\n
"
);
????system(
"
pause
"
);
}
posted @
2007-02-06 20:43 豪 閱讀(689) |
評論 (2) |
編輯 收藏
創(chuàng)建http_request對象

function?createAjaxObj()?
{
????var?http_request?=?false;

????if?(window.XMLHttpRequest)?
{?//?Mozilla,?Safari,
????????http_request?=?new?XMLHttpRequest();

????????if?(http_request.overrideMimeType)?
{
????????????http_request.overrideMimeType('text/xml');
????????}

????}?else?if?(window.ActiveXObject)?
{?//?IE

????????try?
{
????????????http_request?=?new?ActiveXObject("Msxml2.XMLHTTP");

????????}?catch?(e)?
{

????????????try?
{
????????????????http_request?=?new?ActiveXObject("Microsoft.XMLHTTP");

????????????}?catch?(e)?
{}
????????}
????}
????return?http_request;
}http_request對象的使用

????function?ajax_add(obj)?
{
????????var?http_request?=?createAjaxObj();
????????var?feedURL?=?obj.feedURL;
????????var?feedName?=?obj.feedName;

????????http_request.onreadystatechange?=?function()?
{

????????????if?(http_request.readyState?==?4)?
{

????????????????if?(http_request.status?==?200)?
{
????????????????????//接收服務(wù)器返回信息responseText或responseXML
????????????????????var?text_data?=?http_request.responseText;
????????????????????var?parentNode?=?document.getElementById("leftcolumn");
????????????????????var?newDiv?=?document.createElement("div");
????????????????????newDiv.innerHTML?=?unescape(text_data);
????????????????????parentNode.appendChild(newDiv);
????????????????????waiting.innerHTML?=?"";
????????????????}
????????????}
????????}
????????var?submitURL?=?"add_rss.php";
????????http_request.open("POST",?submitURL,?true);
????????http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");?
????????http_request.send("feedURL="+feedURL.value+"&feedName="+escape(feedName.value));
????????var?waiting?=?document.getElementById("waiting");
????????waiting.innerHTML?=?"正在載入
";
????????//http_request.send(null);
????}
posted @
2007-02-05 19:54 豪 閱讀(845) |
評論 (0) |
編輯 收藏
循環(huán)不變式
循環(huán)不變式主要用來幫助我們理解算法的正確性。
初始化:它在循環(huán)的第一輪迭代開始之前,應(yīng)該是正確的。
保持:如果在循環(huán)的某一次迭代開始之前它是正確,那么,在下一次迭代開始之前,它也應(yīng)該保持正確。
終止:當(dāng)循環(huán)結(jié)束時,不變式給了我們一個有用的性質(zhì),它有助于表明算法是正確的。
posted @
2007-02-05 19:43 豪 閱讀(407) |
評論 (0) |
編輯 收藏
沒辦法,近來靠php吃飯-_-
<?
/*************
|?+-------------------------------------------------
|?Id:??????????????????????????????
|?+-------------------------------------------------
|?Copyright?(c)?
|?Author:Howie
|?Email:?qywyh_scut@163.com
|?+-------------------------------------------------
|?Create?Date:?2007-2-3
|?Modify?Date:?
|?Note:??
|?demo:
|???$objRSS?=?new?loadRSS('http://news.163.com/special/00011K6L/rss_gn.xml',?10,?'cache/test.xml');
|???echo?$objRSS->getRSS();
|
|?+-------------------------------------------------
***************/
class?loadRSS?{
????
????//RSS?url鏈接
????var?$_url?=?'';

????//RSS?緩存目錄
????var?$_cacheDir?=?'';

????//緩存時間
????var?$_cacheTime?=?'';

????//是否從緩存讀取
????var?$_loadFromCache?=?true;

????//保存rss字符串
????var?$_str?=?'';

????function?loadRSS?($url,?$cacheTime=0,?$cacheDir=''){
????????$this->_url?=?$url;
????????if?($cacheDir?==?''?||?$cacheTime?==?0)?{
????????????$this->_loadFromCache?=?false;
????????}?else?{
????????????$this->_cacheTime?=?$cacheTime;
????????????$this->_cacheDir?=?$cacheDir;
????????}
????}

????function?getRSS?()?{
????????if?(!$this->_loadFromCache)?{
????????????$fp?=?fopen($this->_url,?'r');
????????????while?($tmp?=?fgets($fp,?4096))?{
????????????????$this->_str?.=?$tmp;
????????????}
????????????fclose($fp);
????????????//echo?$this->_str;
????????}?else?{
????????????$fileModifyTime?=?@filemtime($this->_cacheDir);
????????????$nowTime?=?time();
????????????//修改緩存文件
????????????if?(!$fileModifyTime?||?$nowTime-$fileModifyTime?>=?$this->_cacheTime)?{
????????????????$fp?=?fopen($this->_url,?'r');
????????????????$fp2?=?fopen($this->_cacheDir,?'w');
????????????????while?($tmp?=?fgets($fp,?4096))?{
????????????????????fwrite($fp2,?$tmp);
????????????????????$this->_str?.=?$tmp;
????????????????}
????????????????fclose($fp);
????????????????fclose($fp2);
????????????????return?$this->_str;
????????????}
????????????$fp?=?fopen($this->_cacheDir,?'r');
????????????while?($tmp?=?fgets($fp,?4096))?{
????????????????$this->_str?.=?$tmp;
????????????}
????????????fclose($fp);
????????}
????????return?$this->_str;
????}

}
?>
posted @
2007-02-03 01:32 豪 閱讀(360) |
評論 (0) |
編輯 收藏
以后不想在這里寫心情了,這里就只記錄一些技術(shù)性文章吧~
posted @
2007-01-16 01:30 豪 閱讀(182) |
評論 (0) |
編輯 收藏