大大問題! |
尚未結案
|
keare
一般會員 發表:1 回覆:2 積分:0 註冊:2009-04-06 發送簡訊給我 |
|
st33chen
尊榮會員 發表:15 回覆:591 積分:1201 註冊:2005-09-30 發送簡訊給我 |
您好,
沒人回答應該是要等您自己先努力寫一下post上來吧. 我比較好心, 給一個 hint 1. 先設好一個比較 ?a?b 的function 以簡化程式 : function tform1.calc_nanb(answ_str, gues_str: string) : string; var ii, jj, a, b : integer; begin if (length(trim(answ_str))=4) and (length(trim(gues_str))=4) then begin a := 0; b := 0; for ii:=1 to 4 do begin jj := pos(copy(gues_str,ii,1), answ_str); if jj>0 then begin if jj=ii then a := a 1 else begin b := b 1; end; end; end; result := inttostr(a) 'a ' inttostr(b) 'b'; end else begin result := 'xa xb'; end; end; 2. 依您的邏輯先取得 edit1 及 listbox.items[0] 的 ?a?b 放在變數 bstr 中. bstr := calc_nanb(edit1.text, listbox1.items[0]); 3. 接下來靠您自己囉 用一個loop 從1跑到 listbox 的長度-1 loop 中, 取 listbox1.items[0] 與 listbox1.items[ii] 的 ?a?b 值, 和 bstr 比較, 不等則刪之. 我沒實測, 不過感覺這個 loop 用 while 會比較恰當.(我忘了 for loop 中可否改指標的值) 參考一下並努力一下. (不過, 您比的邏輯很怪, 怎會是固定跟 listbox 的第一個比呢)
------
IS IT WHAT IT IS 我是 李慕白 請倒著唸. 又想把老話拿出來說, 請用台語發音 : 專家專家全是ROBOT CAR (滷肉腳啦); 都已接手這麼久了, 績效還是那麼爛, 講話還那麼大聲. |
keare
一般會員 發表:1 回覆:2 積分:0 註冊:2009-04-06 發送簡訊給我 |
|
小傑克
資深會員 發表:5 回覆:209 積分:357 註冊:2009-02-16 發送簡訊給我 |
st33桑, 這個前不久有人問,一模一樣的怪需求, 不知是哪邊的怪老師出的怪題
樓上這位同學,你還漏寫一個,要把ListBox中有重字的數字刪掉, 例如 1332 33重複 1272 22重複 底下的程式給你參考,希望你能看懂 [code delphi] function TForm1.isDuplicate(Str: String): boolean; var i:integer; begin Result := false; for i := 1 to length(str)-1 do begin if Pos(Str[i], Copy(Str,i 1,Length(str)-i) ) <> 0 then Result := true ; end; end; function TForm1.CalcAB(AnsStr, GuessStr: string): string; var i,iA,iB:integer; begin iA := 0; iB := 0; for i := 1 to Length(GuessStr) do begin if Pos( GuessStr[i],AnsStr) <> 0 then begin if GuessStr[i] = AnsStr[i] then Inc(iA) else Inc(iB); end; end; Result := IntToStr(iA) 'A' IntToStr(iB) 'B'; end; procedure TForm1.Button6Click(Sender: TObject); var i:integer; begin for i := 1000 to 9999 do begin ListBox1.Items.Add(IntToStr(i)); end; for i := ListBox1.Count-1 downto 0 do begin if isDuplicate( ListBox1.Items.Strings[i]) then ListBox1.Items.Delete(i); end; end; procedure TForm1.Button5Click(Sender: TObject); var i:integer; sAB:String; begin sAB := CalcAB( ListBox1.Items.Strings[0], Edit1.Text ); for i := ListBox1.Count-1 downto 1 do begin if sAB <> CalcAB(Edit1.Text ,ListBox1.Items.Strings[i] ) then ListBox1.Items.Delete(i); end; end; [/code] 再多來幾個同學,我積分就增加很快 ╮(﹀_﹀")╭ ,上次是透過mail回答,這次乾脆連程式都貼上
------
額有朝天骨,眼中有靈光
編輯記錄
小傑克 重新編輯於 2009-04-09 00:45:53, 註解 無‧
|
keare
一般會員 發表:1 回覆:2 積分:0 註冊:2009-04-06 發送簡訊給我 |
小弟先謝謝大家幫忙!
但是還是執行不出來 var xA,xB,xc,xd:array [1..4] of integer; xTmp,i,j,a,b,c,xtmp1,d,e,xtmp2,f,h:integer; begin for c:=0 to 9998 do a:=0;b:=0; //把顯示答案的a與b先設定為0 xtmp:=strtoint(edit1.text); for i:=1 to 4 do begin xA[i]:=xtmp mod 10; //將尾數取出,存在相對位置 xtmp:=xtmp div 10; //將尾數去除,存在相對位置 end; xtmp1:=strtoint(listbox2.items.text[c]); for j:=1 to 4 do begin xB[j]:=xtmp1 mod 10; // 將尾數取出,存在相對位置 xtmp1:=xtmp1 div 10; //將尾數去除,存在相對位置 end; //以下程式碼為判斷數字及位置是否相同,計算A和B的數量 for i:=1 to 4 do begin for j:=1 to 4 do begin if xA[i]=xB[j] then begin if i=j then a:=a 1; //如果位置及數字皆同則A 1 if i<>j then b:=b 1; //如果數字同位置不同則B 1 end; end; end; begin xtmp1:=strtoint(listbox2.items.text[c]); for f:=1 to 4 do begin xc[f]:=xtmp1 mod 10; // 將尾數取出,存在相對位置 xtmp1:=xtmp1 div 10; //將尾數去除,存在相對位置 end; end; begin xtmp2:=strtoint(listbox2.items.text[c-1]); for h:=1 to 4 do begin xd[h]:=xtmp2 mod 10; // 將尾數取出,存在相對位置 xtmp2:=xtmp2 div 10; //將尾數去除,存在相對位置 end; end; for i:=1 to 4 do begin for j:=1 to 4 do begin if xc[f]=xd[h] then begin if f=h then d:=d 1; //如果位置及數字皆同則d 1 if f<>h then e:=e 1; //如果數字同位置不同則e 1 end; end; end; listbox2.clear; listbox3.items.add(inttostr(xa)); end; 這一大段是自己打的但是下面不知道怎麼從 陣列裡刪除特定數字 |
小傑克
資深會員 發表:5 回覆:209 積分:357 註冊:2009-02-16 發送簡訊給我 |
xtmp:=strtoint(edit1.text);
for i:=1 to 4 do begin xA[i]:=xtmp mod 10; //將尾數取出,存在相對位置 xtmp:=xtmp div 10; //將尾數去除,存在相對位置 end; 為何要用不直觀的方式來描述簡單的作業呢 你確定了解上面這段執行後的結果和目的嗎? for i := 1 to 4 do xA[i] := StrToInt( Copy( Edit1.text , i ,1) ); 你可能還要再用debug 逐行看一下這段程式的內容在做什麼 你需要的功能, 在我上面po那篇的四段程式都可以完成
------
額有朝天骨,眼中有靈光
編輯記錄
小傑克 重新編輯於 2009-04-09 21:19:10, 註解 無‧
|
Ktop_Robot
站務副站長 發表:0 回覆:3511 積分:0 註冊:2007-04-17 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |