bài tập Pascal khó

K

kieuquocdat

[TẶNG BẠN] TRỌN BỘ Bí kíp học tốt 08 môn
Chắc suất Đại học top - Giữ chỗ ngay!!

ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.

giúp mình làm các bài tập này với, mình phải nộp gấp trong khi phải ôn thi học kì
bài 1: nhập 1 số tự nhiên n từ bàn phím (n<=999)thông báo:
Nếu n=15:in ra 'mười năm'
Nếu n=46:in ra 'bốn mươi sáu'
Nếu n=105:in ra 'một trăm linh năm' ..........................
bài 2:viết chương trình tính tổng và hiệu 2 số nguyên lớn nhập từ bàn phím (có thể có 255 kí tự) quan trọng là kết quả không vượt quá 255 kí tự (Cô giáo bảo fải cộng từng chữ số , rồi lại chuyển về xâu)
 
N

ngobaochauvodich

giúp bạn

giúp mình làm các bài tập này với, mình phải nộp gấp trong khi phải ôn thi học kì
bài 1: nhập 1 số tự nhiên n từ bàn phím (n<=999)thông báo:
Nếu n=15:in ra 'mười năm'
Nếu n=46:in ra 'bốn mươi sáu'
Nếu n=105:in ra 'một trăm linh năm' ..........................

Bài giải tham khảo
uses crt;
type vitri=array[1..3] of integer;
so=array[1..9] of string;
var s,dem,i:integer;
Vt:vitri;
doc:so;
procedure docso(n:integer);
begin
doc[1]:='mot'; doc[2]:='hai'; doc[3]:='ba'; doc[4]:='bon';
doc[5]:='nam'; doc[6]:='sau'; doc[7]:='bay'; doc[8]:='tam'; doc[9]:='chin';
write(doc[n]);
end;
begin
clrscr;
repeat
write('Nhap so can doc ');readln(s);
until (s>=100) and (s<=999);
for i:=1 to 3 do
begin
vt[4-i]:=s mod 10;
s:=s div 10;
end;
docso(VT[1]);
write(' tram ');
if VT[2]=1 then write('') else docso(VT[2]);
if VT[2]=0 then write('le ') else write(' muoi ');

docso(VT[3]);

readln;
end.
 
S

star.love

Mã:
  var
 a: array[1..100] of integer;
  k,n: integer;
  dem,i: integer;
 begin
  readln(n);
  k:=n;
   while n<>0 do
   begin
    dem:=dem+1;
    a[dem]:=n mod 10;
    n:=n div 10;
  end;

 if dem=1 then
      begin

       case k of
    1: write('mot ');
    2: write('hai');
    3: write('ba');
    4: write( 'bon');
    5:write( 'nam');

    end;
    end else
     if dem=2 then
      begin
       k:=a[2];
        case k of
    1: write(' muoi ');
    2: write('hai muoi');
    3: write('ba muoi');
    4: write( 'bon muoi');
    5:write( 'nam muoi');

          end;
      k:=a[1];
       case k of
    1: write(' mot ');
    2: write(' hai ');
    3: write(' ba ');
    4: write( ' bon');
    5:write( ' nam ');
              end;
      end;
readln;

         end.
Mình code hơi dài còn bài 2 thì bạn add nick yahoo mình mình sẽ sent cho bạn !!
 
O

ochuotqb



Bài giải tham khảo
uses crt;
type vitri=array[1..3] of integer;
so=array[1..9] of string;
var s,dem,i:integer;
Vt:vitri;
doc:so;
procedure docso(n:integer);
begin
doc[1]:='mot'; doc[2]:='hai'; doc[3]:='ba'; doc[4]:='bon';
doc[5]:='nam'; doc[6]:='sau'; doc[7]:='bay'; doc[8]:='tam'; doc[9]:='chin';
write(doc[n]);
end;
begin
clrscr;
repeat
write('Nhap so can doc ');readln(s);
until (s>=100) and (s<=999);
for i:=1 to 3 do
begin
vt[4-i]:=s mod 10;
s:=s div 10;
end;
docso(VT[1]);
write(' tram ');
if VT[2]=1 then write('') else docso(VT[2]);
if VT[2]=0 then write('le ') else write(' muoi ');

docso(VT[3]);

readln;
end.
Mình đã thử test bài bạn nhưng có 1 số chỗ như sau:
-Nhâp 1 với 2 chữ số không được
VD: Nhap vào 16 thì bắt nhập lại

Mã:
  var
 a: array[1..100] of integer;
  k,n: integer;
  dem,i: integer;
 begin
  readln(n);
  k:=n;
   while n<>0 do
   begin
    dem:=dem+1;
    a[dem]:=n mod 10;
    n:=n div 10;
  end;

 if dem=1 then
      begin

       case k of
    1: write('mot ');
    2: write('hai');
    3: write('ba');
    4: write( 'bon');
    5:write( 'nam');

    end;
    end else
     if dem=2 then
      begin
       k:=a[2];
        case k of
    1: write(' muoi ');
    2: write('hai muoi');
    3: write('ba muoi');
    4: write( 'bon muoi');
    5:write( 'nam muoi');

          end;
      k:=a[1];
       case k of
    1: write(' mot ');
    2: write(' hai ');
    3: write(' ba ');
    4: write( ' bon');
    5:write( ' nam ');
              end;
      end;
readln;

         end.
Mình code hơi dài còn bài 2 thì bạn add nick yahoo mình mình sẽ sent cho bạn !!

Bài thứ 2 có hơn bài trước ở phần nhập nhưng mà:
- Nhập số có 3 chữ số thì không xuất kết quả ra màn hình :D
VD: Input: 369
Output:
- Nhập 2 chữ số thì có lúc xuất ra được có lúc không:
Vd: Input: 36
Output: ba muoi
Còn đây là bài làm của mình bạn có thể tham khảo :)

PHP:
var i:char; st:string; j:byte;
const a: array ['1'..'9'] of string = ('mot','hai','ba','bon','nam','sau','bay','tam','chin');
  begin
    write('Nhap xau: ');
    readln(st);
  if (st[1]='0') and (length(st)=1) then write('Khong')
  else
  begin
   while st[1]='0' do delete(st,1,1);
  while length(st)<>0 do
   for i:='1' to '9' do
     if (length(st)=1) and ((st[1]=i) or (st[1]='0')) then
       begin
        if st[1]<>'0' then
         write(a[i]);
         delete(st,1,1);
         break;

       end
     else
      if (length(st)=2) and ((st[1]=i) or (st[1]='0'))   then
       begin
         if st[1]='1' then write('muoi ')
          else
            if st[1]='0' then write('linh ')
             else
            write(a[i],' muoi ');
            delete(st,1,1);
            break;
         end else
       if (length(st)=3) and (st[1]=i) then
         begin
           write(a[i],' tram ');
           delete(st,1,1);
           break;
         end;
      end;
      readln
  end.
P/s: Bài của mình hơi cổ điển bạn có thể tìm các cách nhanh hơn :)
 
Last edited by a moderator:
O

ochuotqb

Bài 2:
PHP:
var st1,st2,u,st:string; m,t,c,d,i:byte; code:integer;
  begin
    write('Nhap so thu nhat: ');
    readln(st1);
    write('Nhap so thu hai: ');
    readln(st2);
  while length(st1)>length(st2) do
   st2:='0'+st2;
  while length(st2)>length(st1) do
   st1:='0'+st1;
for i:=length(st1) downto 1 do
  begin
    val(st1[i],c,code);
    val(st2[i],d,code);
  m:=(c+d) mod 10 +t;
  t:=(c+d)div 10;
 str(m,u);
 st:=u+st;
 end;
if t>0 then
 begin
  str(t,u);
  st:=u+st;
 end;
write(st);
readln
end.
Đây là thuật toán tính tổng :D
 
Last edited by a moderator:
N

ngobaochauvodich

Giải bài 2 theo cách lớp 11

function cong_2_xau(st1,st2:string):string;
var i,so1,so2,nho:byte;
sai:integer;
kq,tam:string;
begin
nho:=0; kq:='';
while length(st2)<length(st1) do st2:='0'+st2;
while length(st2)>length(st1) do st1:='0'+st1;
for i:= length(st2) downto 1 do
begin
val(st1,so1,sai);
val(st2,so2,sai);
str((so1+so2+nho) mod 10,tam);
kq:=tam+kq;
nho:=(so1+so2+nho) div 10;
end;
if nho<>0 then
begin
str(nho,tam);
kq:=tam+kq;
end;
cong_2_xau:=kq;
end;
 

Học trò Lập

Học sinh mới
Thành viên
1 Tháng tư 2019
1
0
1
19
Cần Thơ
thcs tt Vinh Thanh
theo kiến thức của bé lớp 9, "đừng lo ko sai đâu ", hsg tin học nhất huyện cho hay:
bài 1:

program cucngan;
uses crt;
var n:string; so,code : integer;
a: array[1..9] of string;
begin
clrscr;
write('nhap n : '); readln(n);
a[1]:='mot '; a[2]:='hai '; a[3]:='ba'; a[4]:='bon'; a[5]:='nam';
a[6]:='sau'; a[7]:='bay'; a[8]:='tam'; a[9]:='chin';
val(n,so,code);
if so<9 then
begin
val(n, so,code);
write (a[so]);
end;
if (so>9) and (so<20) then
begin
write('muoi');
val(n[2],so,code);
write(a[so]:5);
end;
if (so>19) and (so<99) then
begin
val(n[1],so,code); write(a[so]); write('muoi');
val(n[2],so,code);write(a[so]);
end;
if (so>99) and (so<999) then
begin
val(n[1],so,code); write(a[so],' tram');
val(n[2],so,code);
if so=0 then
begin
write('linh':5) ;
val(n[3],so,code); write(a[so]:5);
end
else
begin
val(n[2],so,code); write(a[so]:5); write('muoi':5);
val(n[3],so,code);
if so=5 then write('lam':5) else
write(a[so]:5);
end;
end;
readln;
end.
 
Top Bottom