Tìm dãy con có tổng lớn nhất của dãy số gồm n số nguyên

Tìm dãy con có tổng lớn nhất của dãy số gồm n số nguyên

Cho dãy số gồm n số nguyên. Tìm dãy con có tổng lớn nhất

Giải thuật:

- Sử dụng kỹ thuật vét cạn các dãy con, dùng hàm tính tổng dãy con để kiểm tra.

Cài đặt:

Program Day_con1b;

Type KM= array[1..100] of integer;

     Var M:KM;

    i,j,n,ldau, dai,Max: integer;

Function TONG(A:KM;m,l:byte):Integer;

Var Tam,i:integer;

Begin

    Tam:=0;

    For i:=m to m+l do Tam:=Tam + A[i];

    TONG:=Tam;

End;

Begin

     Write('Nhap so nc: '); Readln(n);

     For i:=1 to n do Begin Write('[',i,']='); Readln(M[i]); End;

     Max:=M[1];dai:=1;ldau:=1;

     For i:= 1 to n do

      For  j:=0 to n-i+1 do

           if TONG(M,i,j)> Max then

           Begin ldau:=i; Max:=Tong(M,i,j) ; dai:=j+1 End;

     Write('Xau con co tong:',max,' bat dau tu: ',ldau, ' dai: ',dai);

     Readln

End.
Bạn thấy bài viết này như thế nào?: 
Average: 7.5 (250 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Stanford Law School

Thiết kế Drupal site: Stanford Law School

Stanford Law School (SLS) wanted to migrate their unwieldy proprietary legacy CMS called Pentagram to Drupal. The primary reasons for the migration include:

Cludo + Drupal = Instant User-Friendly Search

Thử trải nghiệm Friendly Search của Cludo module trên Drupal 8

Search is a key feature in web experience, and for a lot of people, it's the go-to method to find content. We use search countless times a day on our smartphones in various contexts.

Google Sandbox

Google Sandbox bộ lọc website mới

Vào tháng 5 năm 2004, Google cài đặt một bộ lọc mới được giới webmaster gọi là Sandbox.

Công ty diệt chuột T&C

 

Diet con trung