Home > Back-end > How to determine the odd and even number in a string of Numbers
How to determine the odd and even number in a string of Numbers
Time:11-18
Such as: a string S=21,33,03,45,31,04,56,66,86,55 How to determine how many odd how many even in this string?
CodePudding user response:
Procedure TForm1. Button1Click (Sender: TObject); Var S, T: String; Odd, Even: Integer; The List: TStringList; The begin List:=TStringList. Create; S:='21,33,03,45,31,04,56,66,86,55'; List. DelimitedText:=S; Odd:=0; Even:=0; In the List for T do The begin If (StrToInt (T) mod 2)=1 then Inc (Odd) The else Inc (Even); end; List. Free; ShowMessage (Format (' Odd number: % d, the Even number: % d ', [Odd, Even])); end;
CodePudding user response:
Before I will not use for T in the List do, ha, ha, ha
CodePudding user response:
D7 since language enhances a lot, because some of the veteran of inertia are not familiar with the
CodePudding user response:
//can not use TStringList, the following writing efficiency higher point: Procedure TForm1. Button1Click (Sender: TObject); Var C: AnsiChar; S: AnsiString. Found: Boolean; I, Len, Odd, Even: Integer; The begin S:='21,33,03,45,31,04,56,66,86,55'; Found:=False; Odd:=0; Even:=0; Len:=Length (S); For I:=1 to Len do The begin If S [I] in [' 0 '... '9'] then The begin C: [I]=S; Found:=True; end; If Found and ((I=Len) or (not (S [I] in [' 0 '... '9']))) then The begin If (C [in '1', '3', '5', '7', '9']) then Inc (Odd) The else Inc (Even); Found:=False; end; end; ShowMessage (Format (' Odd number: % d, the Even number: % d ', [Odd, Even])); end;