当k比较小的时候,可获得O(n)的时间复杂度.
#include
<
iostream
>
using namespace std;
const
int
arrLen
=
5
;
void
countingSort(
int
*
a,
int
*
b,
int
k)
{
//
a数组元素在[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的元素个数
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 豪 阅读(682) |
评论 (2) |
编辑 收藏
创建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) {
//接收服务器返回信息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 豪 阅读(840) |
评论 (0) |
编辑 收藏
循环不变式
循环不变式主要用来帮助我们理解算法的正确性。
初始化:它在循环的第一轮迭代开始之前,应该是正确的。
保持:如果在循环的某一次迭代开始之前它是正确,那么,在下一次迭代开始之前,它也应该保持正确。
终止:当循环结束时,不变式给了我们一个有用的性质,它有助于表明算法是正确的。
posted @
2007-02-05 19:43 豪 阅读(395) |
评论 (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 豪 阅读(353) |
评论 (0) |
编辑 收藏
以后不想在这里写心情了,这里就只记录一些技术性文章吧~
posted @
2007-01-16 01:30 豪 阅读(164) |
评论 (0) |
编辑 收藏