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;

CodePudding user response:

reference 4 floor BlueStorm response:
//can not use TStringList, the following writing efficiency higher point:

I just copy the finish my controls traversal modification, you said that the high efficiency, haha
So the question: for T in the List do and for I:=1 to Len do exactly which one is more efficient?

CodePudding user response:

For T in the List do and for I:=1 to Len do basic is the same, the efficiency of any use to the TStringList is key,
Only when there are vast amounts of data need to decide, the even number of cases, these two programs will have the difference, if we can only judge '21,33,03,45,31,04,56,66,86,55, assume that a used 1 millisecond, it took another 0.1 milliseconds, is there a difference for you

CodePudding user response:

Strictly speaking, for I:=1 to Len do efficiency will be high, but little difference in general

CodePudding user response:

To:
 
USES System. SysUtils, System. StrUtils;

Procedure StatNumbers (const S: a string; Var Odds, Evens: integer);
Var
P1, p2, n, c: integer;
The begin
Odds:=0;
Evens:=0;
P1:=1;
P2:=1;
Repeat
If p1 & gt; Length (S) then break;
P2:=PosEx (', 'S, p2);
If p2=0 then the p2:=Length (S) + 1;
Val (Copy (S, p1, p2, p1). The Trim, n, c);
If c=0 then
If n and 1 & lt;> 0 then Inc (Odds) else Inc (Evens);
P1:=p2 + 1;
P2:=p;
Until the false;
end;

CodePudding user response:

 procedure TForm1. Button1Click (Sender: TObject); 
Var
S: AnsiString.
I, Len, Odd, Even: Integer;
The begin
S:='21,33,03,45,31,04,56,66,86,55';
Odd:=0; Even:=0; Len:=Length (S);
For I:=1 to Len do
The begin
If iThe begin
If S=[I + 1] ', 'then
The begin
If S [I] in [' 1 ', '3', '5', '7', '9']
Then the Inc (Odd)
The else Inc (Even);
End
End
The else begin
If S [I] in [' 1 ', '3', '5', '7', '9']
Then the Inc (Odd)
The else Inc (Even);
end;
end;
ShowMessage (Format (' Odd number: % d, the Even number: % d ', [Odd, Even]));
end;

Also can such, way of kinds,

CodePudding user response:

This robustness is not good, if you have any blank space before and after the comma...

CodePudding user response:

references to the tenth floor DelphiGuy response:
this robustness is not good, if you have any blank space before and after the comma...
just algorithm is different, of course, use the Stringlist, is better, when there is a separator and varied: when this algorithm is practical,
Only modify a if (S=[I + 1] ', ') or (S='[I + 1]; ') or (S [I + 1]=' ') then can,
At the same time, more standardized operation, should be prior to the format String, and

CodePudding user response:

references to the tenth floor DelphiGuy response:
this robustness is not good, if you have any blank space before and after the comma...
just algorithm is different, of course, use the Stringlist, is better, when there is a separator and varied: when this algorithm is practical,
Only modify a if (S=[I + 1] ', ') or (S='[I + 1]; ') or (S [I + 1]=' ') then can,
At the same time, more standardized operation, should be prior to the format String, and

CodePudding user response:

Procedure OddEvenNumCounter (S: a String; Var OddNumCount: Integer; Var EvenNumCount: Integer; Var ZeroNumCount: Integer);
Var
The List: TStringList;
I: Integer;
The begin
//0 is not surprising that even, outside the process of setting the initial value, which is beneficial to the batch
List:=TStringList. Create;
List. DelimitedText:=S;
For I:=0 to the List. Do the Count - 1
The begin
If StrToInt (List [I])=0 then
INC (ZeroNumCount)
Else if StrToInt (List [I]) then
INC (OddNumCount);
end;
EvenNumCount:=EvenNumCount + List. The Count - OddNumCount - ZeroNumCount;
List. Free;
end;

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related