0. 建立適當結構體
在做任何演化式演算法時,較建議直接先把該定的結構定出來。如下所示
typedef unsigned char byte;
/* 母體個數 */
const unsigned parent_cnt = 50;
const unsigned gene_len = 20;
typedef struct TagChrom{
byte* gene; /* gene[基因長度] */
double fitness; /* 對應之適應值 */
}Parent, Child;
/* 父代與子代配置 */
Parent *p = (Parent*)malloc(parent_cnt*sizeof(Parent));
Child *d = (Child*)malloc(parent_cnt*sizeof(Child));
for(int i=0; i!=parent_cnt){
p[i].gene = (byte*)malloc(sizeof(byte)*gene_len);
d[i].gene = (byte*)malloc(sizeof(byte)*gene_len);
}
edisonx 發表在 痞客邦 留言(0) 人氣(3,575)
x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
int main()
{
unsigned i;
int head[N]={0}, test[N]={0};
srand(0);
for(i=0; i!=N; ++i) head[i] = rand();
i=0;
while(memcmp(head, test, N*sizeof(int))){
memcpy(test, test+1, (N-1)*sizeof(int));
test[N-1]=rand();
++i;
}
printf("period=%u\n", i);
printf("int_max=%d\n", INT_MAX);
return 0;
}
edisonx 發表在 痞客邦 留言(0) 人氣(254)

在說明此文時,必須特別強調一件事,如果現在初學者的你還在思考:”要怎麼學Visual C++”、”要選Visual C++還是Borland C++”、”作業要用 Visual C++ 還是要用 Dev-C++ 寫比較好?”,那我只能跟你說:誰沒菜過?但你一定要看完以下的說明。
edisonx 發表在 痞客邦 留言(0) 人氣(2,509)
吾人認為基本常用指令有項,這些事實上下 「指令 /?」 去查都可以找到更多資料,如 dir /?,便可查到 dir 所有相關資訊!
1. 查詢目錄 (dir)
(empty):查詢目前所在目錄之資料夾與檔案
/A : 依指定屬性,又分 H(隱藏), S(系統), D(目錄), R(唯讀), A(保存)
/B : 單純格式
/C : 大小以千分位顯示
/D : 寬列表,依欄排序
/L : 小寫顯示
/O : 指定順序排序,N (名稱), S(大小), E(副檔名), D(日期), G(子目錄先), -(反向)
/S : 指定目錄及目錄中所有檔案 (一層一層進去看)
/T : 指定顯示或排序之欄位,C(建立), A(讀取), W(寫入)
/W: 寬格式顯示
edisonx 發表在 痞客邦 留言(2) 人氣(482,036)

C/C++ 標準之 main 寫法有二種,一種是不接受任何參數列;另一種是接受二個參數列。程式碼大致如下
int main(int argc, char **argv)
{
return 0;
}
int main(void)
{
return 0;
}
edisonx 發表在 痞客邦 留言(0) 人氣(30,958)
吾人本身一陣最常用到的二個運算: Sqrt, 1/Sqrt,於是會找些奇淫怪技想辦法加速。
事實上在做 Sqrt 加速時,實測之結果效能都沒 VC 開 O2 來得快,即使只求整數之 sqrt 之演算法效果也沒很好,所幸在 codeproject 看到有人已實測,該程式碼共寫了 14 份 sqrt,有興趣可去下載下來看看,其中 asm 崁入的有 2 份,測出來結果都比內建的還快,且精度也完全正確。以下為該作者提供之 2 份 sqrt,實測出來約比內建 sqrt 快上 20% 左右 (11390 : 8922),其它的 12 種方法若有興趣,也可再進行實測。
edisonx 發表在 痞客邦 留言(0) 人氣(834)
這是個讓 CS 領域驚豔的計算方式,同時裡面使用的 magic number - 0x5f3759df 拿去 google 可得到一狗票的東西,本文並不進行 magic number 之推導,整個推導最詳盡的,應是 這篇pdf 。一開始的原始碼長得像這樣 (轉自 wiki)
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking [sic]
i = 0x5f3759df - ( i >> 1 ); // what the fuck? [sic]
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
edisonx 發表在 痞客邦 留言(0) 人氣(2,137)
[Lemma 說明]
會寫 recursive 通常讓初學者覺得「很強、思緒很清晰」,就吾人所知會「避開用 recursive 」有以下二個原因
edisonx 發表在 痞客邦 留言(1) 人氣(89,459)
關於 讓 CPU 使用率曲線聽你指揮 之相關文章中,已對原作之解法提出分析與實做,接下來繼續討論相關之議題。原作提出之議題吾人大致分成以下四部份:
1. 取得 CPU 核心週期數
2. 取得 CPU 使用率之方法
3. 高精度計時器
* 4. 取得 CPU 時脈率
edisonx 發表在 痞客邦 留言(0) 人氣(1,330)
題目說明
寫一程式,讓使用者決定 windows 工作管理員之 cpu 使用率。程式越精簡越好,電腦語言不限。如,可實現下列三種情況:
1. cpu 使用率固定在 50%
2. cpu 使用率為一直線
3. cpu 使用率狀態為一正弦曲線
edisonx 發表在 痞客邦 留言(0) 人氣(6,806)