有兩條循環語句的練習不懂做,請各位幫忙 |
答題得分者是:jow
|
017911
一般會員 發表:5 回覆:2 積分:1 註冊:2007-07-08 發送簡訊給我 |
160123321interface
type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; implementation procedure TForm1.Button1Click(Sender: TObject); var i,j,k,l,x,a,n,y,b,m,z,sum,c,one,two,three,max:integer; s:string; aa,bb,cc:boolean; begin s:=''; for i:=1 to 160 do begin max:=i; for j:=1 to i do begin n:=0; one:=j 1; aa:=false; while a>0 do begin x:=a mod 10; n:=n*0 x; a:=a div 10; end; if n mod one<>0 then aa:=true; end; for l:=1 to i do begin sum:=1; three:=l 1; cc:=false; while c>0 do begin z:=c mod 10; sum:=sum*z; c:=c div 10; end; if n mod one<>0 then aa:=true; end; if (aa=true)and(bb=true)and(cc=true) then s:=s inttostr(max) ' '; end; showmessage(s); end; 由m個數組成一個圓環,找出4個相鄰的數,使其和為最大、最小。如下圖 1 4 5 8 6 _________________________________________________________________________________________ 求具有下列兩個性質的最小自然數:1的個位數是6;2若將的個位數移到其餘各位數字之前,所得的新數是的4倍。 |
017911
一般會員 發表:5 回覆:2 積分:1 註冊:2007-07-08 發送簡訊給我 |
|
jackiemi2_seed
中階會員 發表:37 回覆:97 積分:76 註冊:2006-09-11 發送簡訊給我 |
第四題:最小自然數
[code delphi] var n,n_temp:integer; begin n:=1; while true do begin if ((n mod 10)=6) then begin n_temp:=strtoint('6' inttostr((n div 10))); if (n_temp=(n*4)) then begin showmessage(inttostr(n)); break; end; end; n:=n 1; end; [/code]
------
OS : Win 7 pro Program : Delphi 7 DataBase : Ms Sql 2008
編輯記錄
jackiemi2_seed 重新編輯於 2007-10-15 17:25:50, 註解 無‧
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
第五題:
[code delphi] procedure TForm1.Button5Click(Sender: TObject); function IsPrime(X: Integer): Boolean; var I, Q: Integer; begin if X < 4 then Result := True else begin Result := True; Q := Trunc(Sqrt(X)) 1; for I := 2 to Q do if X mod I = 0 then begin Result := False; Break; end; end; end; function CheckCondition(X: Integer): Boolean; var S: string; I: Integer; A: array[0..2] of Integer; begin A[0] := 1; A[1] := 0;//Sum A[2] := 1;//Mul S := IntToStr(X); for I := Length(S) downto 1 do begin A[0] := A[0] * 10 StrToInt(S[I]); A[1] := A[1] StrToInt(S[I]); A[2] := A[2] * StrToInt(S[I]); end; Result := True; for I := 0 to Length(A)-1 do if not IsPrime(A[I]) then begin Result := False; Break; end; end; var S: string; I, J, N, Z: Integer; A: array of Integer; begin Z := 16000;//最大數 J := 0; SetLength(A, Z); try for N := 1 to Z do if IsPrime(N) and CheckCondition(N) then begin A[J] := N; Inc(J); end; SetLength(A, J); S := ''; for I := 0 to Length(A)-1 do S := S IntToStr(A[I]) ', '; if S <> '' then S := Copy(S, 1, Length(S)-2); Label1.Caption := S; finally A := nil; end; end; [/code] 第六題: [code delphi] procedure TForm1.Button1Click(Sender: TObject); var I, J, K, m: Integer; max_i, min_i: Integer; sum, max_sum, min_sum: Integer; A: array of Integer; S: string; begin m := 20; SetLength(A, m); //亂數取樣, 10的整數 for I := 0 to Length(A)-1 do A[I] := Random(100); max_i := 0; min_i := 0; max_sum := -MaxInt; min_sum := MaxInt; for I := 0 to m-1 do begin sum := 0; for J := I to I 3 do begin K := J mod m; sum := sum A[K]; end; if sum > max_sum then begin max_i := I; max_sum := sum; end; if sum < min_sum then begin min_i := I; min_sum := sum; end; end; ListBox1.Clear; S := ''; for I := 0 to Length(A)-1 do S := S IntToStr(A[I]) ', '; S := Copy(S, 1, Length(S)-2); ListBox1.Items.Add('Sample: ' S); S := ''; for I := max_i to max_i 3 do S := S IntToStr(A[I mod m]) ', '; S := Copy(S, 1, Length(S)-2); S := 'MAX SUM: ' IntToStr(max_sum) ', the members are [' S ']'; ListBox1.Items.Add(S); S := ''; for I := min_i to min_i 3 do S := S IntToStr(A[I mod m]) ', '; S := Copy(S, 1, Length(S)-2); S := 'MIN SUM: ' IntToStr(min_sum) ', the members are [' S ']'; ListBox1.Items.Add(S); ListBox1.Items.SaveToFile('D:\RESULT.TXT'); end; [/code] 在我電腦的執行結果: ( D:\RESULT.TXT ) Sample: 0, 3, 86, 20, 27, 67, 31, 16, 37, 42, 8, 47, 7, 84, 5, 29, 91, 36, 77, 32 MAX SUM: 236, the members are [91, 36, 77, 32] MIN SUM: 103, the members are [16, 37, 42, 8] |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |