random的方式 |
尚未結案
|
mismmx
一般會員 發表:4 回覆:6 積分:2 註冊:2003-05-26 發送簡訊給我 |
|
mismmx
一般會員 發表:4 回覆:6 積分:2 註冊:2003-05-26 發送簡訊給我 |
這是小弟寫的程式
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int a=StrToInt(Edit1->Text),b=0;
int **ary;
ary=new int*[a];
for(int i=0;iCells[i][j]=IntToStr(ary[i][j]);
}
}
for(int i=0;iColCount=StrToInt(Edit1->Text);
sg->RowCount=StrToInt(Edit1->Text);
}
|
Justmade
版主 發表:94 回覆:1934 積分:2030 註冊:2003-03-12 發送簡訊給我 |
沒學過 BCB 也沒 BCB 來 Test 亂貼 BCB 程式系列 II 看了你的問題後寫了一個 class="code">
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i=StrToInt(Edit1->Text);
TStringList* SL = new TStringList();
for (int j=0; j < i * i; j ){
SL->Add(IntToStr(j 1));
}
for (j=0; j < i; j ){
for (int k=0; j < i; k ){
int r = Random(SL->Count);
SG->Cells[j][k] = SL[r];
SL->Delete(r);
}
}
delete SL;
} void __fastcall TForm1::Edit1Change(TObject *Sender)
{
sg->ColCount=StrToInt(Edit1->Text);
sg->RowCount=StrToInt(Edit1->Text);
}
修正錯誤,不知還有多少錯> 發表人 -
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
修正版來囉
< class="code">
//----------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i=StrToInt(Edit1->Text);
TStringList* SL = new TStringList();
for (int j=0; j < i * i; j ){
SL->Add(IntToStr(j 1));
}
for (int j=0; j < i; j ){
for (int k=0; k < i; k ){
int r = random(SL->Count);
SG->Cells[j][k] = SL->Strings[r];
SL->Delete(r);
}
}
delete SL;
}
//----------------------------------------------------------------------------
void __fastcall TForm1::Edit1Change(TObject *Sender)
{
SG->ColCount=StrToInt(Edit1->Text);
SG->RowCount=StrToInt(Edit1->Text);
}
//----------------------------------------------------------------------------
本來想加上修改的標示 可是...在 〔code〕內可能不行...
就請 Justmade 版主自行比較好囉 <>沒空更新的網頁...
href="http://dllee.adsldns.org">http://dllee.adsldns.org 介紹Shells,LiteStep,GeoShell.... 發表人 - dllee 於 2003/06/04 22:55:15
------
http://www.ViewMove.com |
mismmx
一般會員 發表:4 回覆:6 積分:2 註冊:2003-05-26 發送簡訊給我 |
|
artist1002
高階會員 發表:2 回覆:155 積分:151 註冊:2002-09-26 發送簡訊給我 |
|
Justmade
版主 發表:94 回覆:1934 積分:2030 註冊:2003-03-12 發送簡訊給我 |
引言: 請問一下~~如果不要用StringList 那該怎麼做呢~~~用 Array 用 Set 等也可以但比較煩 如用 Array 1. 你也要像用 StringList 一樣先埴入所有可能的數值 2. 之後 Random Index 用 Array 取值,檢查是否大過 0 3. 若大過 0 即該數值未被取用,可放在 StringGrid 並將值設為 0 4. 若等於 0 即該數值已被取用重新 Random 這不是比較麻煩嗎? 其實我看完你的題目因為是BCB的最初沒打算 Post 只是想想,而且也是想用 Arry 做但覺煩,突然想起可用 TStringList 寫出又簡單又快的 Code,所以才會 post, 看來還是不合意< > 看來以後應少些花這種時間< > |
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
引言: 請問一下~~如果不要用StringList 那該怎麼做呢~~~除非您認為用 TStringList 的速度不能滿足您的需求,不然,用內建的元件可以省比較多的開發時間。 如 artist1002 所說,自己寫的話,就要比對,去除等等,如果寫得不好,執行速度可能比 TStringList 還慢,當然,如果寫得好,一定可以比 TStringList 快 <>沒空更新的網頁... href="http://dllee.adsldns.org">http://dllee.adsldns.org 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com |
artist1002
高階會員 發表:2 回覆:155 積分:151 註冊:2002-09-26 發送簡訊給我 |
中午休息實在太無聊了, 寫寫看動態陣列的方式
參考了Justmade兄的程式碼和建議
如下
void __fastcall TForm1::Button1Click(TObject *Sender) { int i=StrToInt(Edit1->Text); // TStringList* SL = new TStringList(); Variant V(OPENARRAY(int,(0,i*i-1)),varInteger); int tmp; for (int j=0; j < i * i; j ){ tmp=j 1; V.PutElement(&tmp, j); } for (int j=0; j < i; j ){ for (int k=0; k < i; k ){ int r = random(i * i); if (V.GetElement(r)==0){ k--; continue; } SG->Cells[j][k] = IntToStr(int(V.GetElement(r))); tmp=0; V.PutElement(&tmp, r); } } } void __fastcall TForm1::Edit1Change(TObject *Sender) { SG->ColCount=StrToInt(Edit1->Text); SG->RowCount=StrToInt(Edit1->Text); } |
artist1002
高階會員 發表:2 回覆:155 積分:151 註冊:2002-09-26 發送簡訊給我 |
中午休息實在太無聊了, 寫寫看動態陣列的方式
參考了Justmade兄的程式碼和建議
如下
void __fastcall TForm1::Button1Click(TObject *Sender) { int i=StrToInt(Edit1->Text); Variant V(OPENARRAY(int,(0,i*i-1)),varInteger); int tmp; for (int j=0; j < i * i; j ){ tmp=j 1; V.PutElement(&tmp, j); } for (int j=0; j < i; j ){ for (int k=0; k < i; k ){ int r = random(i * i); if (V.GetElement(r)==0){ k--; continue; } SG->Cells[j][k] = IntToStr(int(V.GetElement(r))); tmp=0; V.PutElement(&tmp, r); } } } void __fastcall TForm1::Edit1Change(TObject *Sender) { SG->ColCount=StrToInt(Edit1->Text); SG->RowCount=StrToInt(Edit1->Text); }給 mismmx 兄:如果你要儲存產生出來的二維陣列也可以使用這樣的方式來動態產生陣列儲存 Variant V(OPENARRAY(int,(0,x-1,0,y-1)),varInteger);其中x,y是陣列維度大小 發表人 - artist1002 於 2003/06/06 13:31:40 |
Justmade
版主 發表:94 回覆:1934 積分:2030 註冊:2003-03-12 發送簡訊給我 |
哈哈,又學到 OpenArray 了 (但我沒BCB學到也沒大用< >)。
剛又有一個想法原理差不多但不用中介儲存 不知程式碼又是不是一大堆錯< >
void __fastcall TForm1::Button1Click(TObject *Sender) { for(int i=0; i < SG->RowCount; i ) SG->Rows[i]->Clear(); //清空所有 SG 資料 int i=StrToInt(Edit1->Text); for (int z=0; z < i * i; z ){ int j = Random(i); int k = Random(i); if (SG->Cells[j][k] <> ''){ z--; continue; } SG->Cells[j][k] = IntToStr(z); } }清空SG資料程式碼來自包子/dllLee 的解答 : http://delphi.ktop.com.tw/topic.php?TOPIC_ID=31758 發表人 - Justmade 於 2003/06/06 14:20:09 |
mismmx
一般會員 發表:4 回覆:6 積分:2 註冊:2003-05-26 發送簡訊給我 |
|
Justmade
版主 發表:94 回覆:1934 積分:2030 註冊:2003-03-12 發送簡訊給我 |
|
mismmx
一般會員 發表:4 回覆:6 積分:2 註冊:2003-05-26 發送簡訊給我 |
|
Justmade
版主 發表:94 回覆:1934 積分:2030 註冊:2003-03-12 發送簡訊給我 |
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
to Justmade
if (SG->Cells[j][k] <> '')應為 if (SG->Cells[j][k] != "")看了那麼多種方法(都不是我提的,但分數卻給了我 ),那我也來提一個,應該比 class="code"> void __fastcall TForm1::Button1Click(TObject *Sender) { int i=StrToInt(Edit1->Text); int *IArray=new int[i*i]; for (int j=0; j < i * i; j ){ IArray[j]=j 1; } int iCount=i*i; for (int j=0; j < i; j ){ for (int k=0; k < i; k ){ int r = random(iCount); SG->Cells[j][k] = IntToStr(IArray[r]); IArray[r]=IArray[--iCount]; } } delete [] IArray; } 沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://dllee.adsldns.org 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |