- Input cho trong tập tin BAI2.INP gồm 01 (một) dòng duy nhất có giá trị là số n.
- Output ghi vào tập tin BAI2.OUT thể hiện cách đọc bằng chữ của số n.
Chương trình ví dụ:
Mã: Chọn tất cả
BAI2.INP BAI2.OUT
0005 nam
0555 nam tram nam muoi nam
00500000005000000005 nam tram trieu ti khong tram le nam ti khong tram le nam
(Đề thi tuyển sinh vào lớp 10 chuyên Tin năm 2019 - Câu 4)
(Đề thi chọn Học sinh giỏi Tin học cấp THPT tỉnh An Giang năm 2019 - Câu 2)
Mã nguồn (Đọc số thành chữ):
Mã: Chọn tất cả
Program DocSoThanhChu;
Uses Crt;
Procedure Trim(Var St: String);
Begin
While (Length(St) > 0) And (St[1] = ' ') Do Delete(St, 1, 1);
While St[Length(St)] = ' ' Do Delete(St, Length(St), 1);
While Pos(' ', St) > 0 Do Delete(St, Pos(' ', St), 1);
While (Length(St) > 0) And (St[1] = '0') Do Delete(St, 1, 1);
End;
Function DoiSoSangChu(numSt: String) : String;
Var pt, i, j, k: Integer;
result, check: String;
thousand: Array[1..7] Of String;
hundred: Array[1..2] Of String;
Begin
If Length(numSt) <= 0 Then Exit('');
hundred[1] := 'muoi'; hundred[2] := 'tram';
thousand[1] := '';
thousand[2] := 'nghin'; thousand[3] := 'trieu';
thousand[4] := 'ti'; thousand[5] := 'nghin ti';
thousand[6] := 'trieu ti'; thousand[7] := 'ti ti';
result := '';
{ Bat dau tu nhom 3 so vi tri pt }
pt := Length(numSt);
For i := 1 To Length(numSt) Do Begin
{ Doc so tai vi tri dang xet }
result := result + numSt[i] + ' ';
{ Xac dinh thuoc nhom nao }
k := pt Div 3;
If (k >= 0) And (pt > 0) Then Begin
If ((pt-1) Mod 3 = 0)
Then result := result + thousand[k+1] + ' '
Else result := result + hundred[(pt-1) Mod 3] + ' ';
{ Giam vi tri pt de xet so tiep theo }
Dec(pt);
End;
End;
{ Xoa cac tu du thua }
While Pos('0 tram 0 muoi 0 ', result) > 0 Do Begin
k := Pos('0 tram 0 muoi 0 ', result);
Delete(result, k, 16);
End;
While Pos('0 muoi 0 ', result) > 0 Do Begin
k := Pos('0 muoi 0 ', result);
Delete(result, k, 9);
End;
{ Chuyen doi mot so tu dac biet }
While Pos('muoi 0', result) > 0 Do Begin
k := Pos('muoi 0', result);
Delete(result, k, 6);
Insert('muoi', result, k);
End;
While Pos('1 muoi 5', result) > 0 Do Begin
k := Pos('1 muoi 5', result);
Delete(result, k, 8);
Insert('muoi lam', result, k);
End;
While Pos('1 muoi', result) > 0 Do Begin
k := Pos('1 muoi', result);
Delete(result, k, 6);
Insert('muoi', result, k);
End;
While Pos('0 muoi', result) > 0 Do Begin
k := Pos('0 muoi', result);
Delete(result, k, 6);
Insert('le', result, k);
End;
{ Tiep tu xoa cac tu du thua }
{ Vi du: 'trieu ti nghin ti' -> 'trieu ti' }
{ 'trieu ti nghin ti' -> 'trieu ti' }
For i := 7 DownTo 3 Do
For j := i-1 DownTo 2 Do Begin
check := Concat(thousand[i], ' ', thousand[j]);
While Pos(check, result) > 0 Do Begin
k := Pos(check, result);
Delete(result, k, Length(check));
Insert(thousand[i], result, k);
End;
End;
{ Doi cac so tu 0 den 9 sang chu }
While Pos('0', result) > 0 Do Begin
k := Pos('0', result);
Delete(result, k, 1);
Insert('khong', result, k);
End;
While Pos('1', result) > 0 Do Begin
k := Pos('1', result);
Delete(result, k, 1);
Insert('mot', result, k);
End;
While Pos('2', result) > 0 Do Begin
k := Pos('2', result);
Delete(result, k, 1);
Insert('hai', result, k);
End;
While Pos('3', result) > 0 Do Begin
k := Pos('3', result);
Delete(result, k, 1);
Insert('ba', result, k);
End;
While Pos('4', result) > 0 Do Begin
k := Pos('4', result);
Delete(result, k, 1);
Insert('bon', result, k);
End;
While Pos('5', result) > 0 Do Begin
k := Pos('5', result);
Delete(result, k, 1);
Insert('nam', result, k);
End;
While Pos('6', result) > 0 Do Begin
k := Pos('6', result);
Delete(result, k, 1);
Insert('sau', result, k);
End;
While Pos('7', result) > 0 Do Begin
k := Pos('7', result);
Delete(result, k, 1);
Insert('bay', result, k);
End;
While Pos('8', result) > 0 Do Begin
k := Pos('8', result);
Delete(result, k, 1);
Insert('tam', result, k);
End;
While Pos('9', result) > 0 Do Begin
k := Pos('9', result);
Delete(result, k, 1);
Insert('chin', result, k);
End;
result[1] := Upcase(result[1]);
Exit(result);
End;
Var numSt: String;
Begin
ClrScr;
ReadLn(numSt);
Trim(numSt);
Write(DoiSoSangChu(numSt));
ReadLn;
End.
Mã: Chọn tất cả
Program DoiChuRaSo;
Uses Crt;
Var St: String;
nArray: Array[1..100] Of String;
i, j, k, n, tmp1, tmp2, Max: Integer;
Re: Array[1..100] Of Integer;
Procedure Trim(Var St: String);
Begin
While (Length(St) > 0) And (St[1] = ' ') Do Delete(St, 1, 1);
While (Length(St) > 0) And (St[Length(St)] = ' ') Do Delete(St, Length(St), 1);
While Pos(' ', St) > 0 Do Delete(St, Pos(' ', St), 1);
End;
Procedure Swap(Var a, b: Integer);
Begin
a := a+b;
b := a-b;
a := a-b;
End;
Function CharToNumber(c: String): Integer;
Begin
Case c Of
'MOT': Exit(1);
'HAI': Exit(2);
'BA': Exit(3);
'BON': Exit(4);
'NAM': Exit(5);
'SAU': Exit(6);
'BAY': Exit(7);
'TAM': Exit(8);
'CHIN': Exit(9);
'TU': Exit(4);
'LAM': Exit(5);
Else
Exit(0);
End;
End;
Begin
ClrScr;
St := 'nam tram trieu ti khong tram linh nam ti khong tram linh nam';
ReadLn(St);
Trim(St);
St := Upcase(St);
St := St + ' ';
n := 0;
While Length(St) > 0 Do Begin
Inc(n);
nArray[n]:= Copy(St, 1, Pos(' ', St)-1);
Delete(St, 1, Pos(' ', St));
End;
tmp1 := 0;
tmp2 := 0;
k := 1;
Max := 1;
{ FillByte(Re, SizeOf(Re), 0);}
For i := 1 To n Do Begin
WriteLn('Dang xu ly: "', nArray[i], '", Do dai la ', Length(nArray[i]));
Case nArray[i] Of
'KHONG', 'MOT', 'HAI', 'BA', 'BON', 'NAM', 'SAU', 'BAY', 'TAM', 'CHIN', 'TU', 'LAM': Begin
tmp1 := tmp1 + tmp2;
tmp2 := CharToNumber(nArray[i]);
End;
'MUOI', 'CHUC': If tmp2 > 0 Then tmp2 := tmp2 * 10 Else tmp2 := 10;
'TRAM': tmp2 := tmp2 * 100;
'TI': Begin
tmp1 := tmp1 + tmp2;
tmp2 := 0;
k := 4;
For j := i+1 To n Do
If (nArray[j] = 'TI') Then k := k + 3;
If tmp1 > 0 Then Re[k] := tmp1;
tmp1 := 0;
End;
'TRIEU': Begin
tmp1 := tmp1 + tmp2;
tmp2 := 0;
k := 3;
For j := i+1 To n Do
If (nArray[j] = 'TI') Then k := k + 3;
If tmp1 > 0 Then Re[k] := tmp1;
tmp1 := 0;
End;
'NGHIN', 'NGAN': Begin
tmp1 := tmp1 + tmp2;
tmp2 := 0;
k := 2;
For j := i+1 To n Do
If (nArray[j] = 'TI') Then k := k + 3;
If tmp1 > 0 Then Re[k] := tmp1;
tmp1 := 0;
End;
Else
WriteLn('Da bo qua chu: ', nArray[i]);
End;
If k > Max Then Max := k;
WriteLn('tmp1: ', tmp1, ', tmp2: ', tmp2, ', Max: ', Max);
End;
WriteLn('Ket qua: ');
If tmp2 > 0 Then Re[1] := Re[1] + tmp1 + tmp2;
For i := Max DownTo 1 Do
Case Re[i] Of
100..999: If i = Max Then Write(Re[i]) Else Write(',', Re[i]);
10..99: If i = Max Then Write(Re[i]) Else Write(',0', Re[i]);
1..9: If i = Max Then Write(Re[i]) Else Write(',00', Re[i]);
0: Write(',000');
End;
ReadLn;
End.