Cho N, tìm số bé hơn N có độ bền lớn nhất (0 ≤ N ≤ 2.000.000.000)

Cho N, tìm số bé hơn N có độ bền lớn nhất (0 ≤ N ≤ 2.000.000.000)

Năm 1973, nhà Toán học Neil Sloan đưa ra khái niệm độ bền của một số nguyên không âm N như sau:

  • Nếu N có một chữ số thì độ bền của N bằng 0.
  • Nếu N có từ 2 chữ số trở lên thì độ bền của N bằng độ bền của số nguyên là tích các chữ số của N cộng 1.

Cho N, tìm số bé hơn N có độ bền lớn nhất (0 ≤ N ≤ 2.000.000.000).

Persist.inp

persist.out

Giải thích

100

77

Doben(77)=Doben(49)+1=Doben(36)+1+1=Doben(18)+1+1+1=

Doben(8)+1+1+1+1=0+1+1+1+1=4

Giải thuật:

- Để tìm độ bền một số cần một hàm tính tích các chữ số của n TICH(n).

- Cho d = 0. Lặp lại điều kiện n >9 việc: Tăng d lên 1 thay n = TICH(n).

Program Do_ben;

uses crt;

Var n,d,i,j,max:longint;

Function TICH(n:Longint):Longint;

Var tam:integer;

Begin

     if n=0 then tam:=0 else tam:=1;

     While n<> 0 do

     Begin

          Tam:=tam*(n mod 10);

          n:=n div 10;

     End;

     TICH:=Tam;

End;

Begin

     clrscr;


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

     Max:=0;

     For i:=1 to n do

     Begin

     d:=0;

     j:=i;

     gotoxy(1,2);

     Write('Dang duyet den so: ',i);

     While j>9 do

     Begin

          d:=d+1;

          j:=TICH(j);

     End;

     if d>=Max then

     Begin

          max:=d;

          gotoxy(1,3);

          Writeln('So co do ben lon nhat dang la ',i,' do ben la:',d);

     End;

     End;

     Gotoxy(1,4);

     Writeln('Da duyet xong');

     Readln

End.

 

Bạn thấy bài viết này như thế nào?: 
Average: 5.1 (15 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

 
SEO

Chợ Điện Tử và thủ thuật SEO mũ đen

Chuyện gì xảy ra khi Chợ Điện Tử, một ông trùm thương mại điện tử, dùng tiền để nhanh chóng khẳng định sức mạnh của mình trên các máy tìm kiếm với các thủ thuật seo mũ đen. Liệu họ có thành công với phương pháp của mình?

Drupal WYSIWYG Editor

Thêm tính năng Spellchecker cho Drupal WYSIWYG Editor

Most of us are used to having a spellchecker when we type. If we use Microsoft Word, Google Docs and most other writing tools, you're used to seeing a red line under your spelling mistakes.

Hướng dẫn Custom Search Module trong Drupal 6

Hướng dẫn Custom Search Module trong Drupal 6

The Drupal core provides a Search module that is great for many sites. However, it doesn't provide some of the more sophisticated features that some sites need. The Custom Search module is a good alternative if you want more control over what gets searched, who gets to search, and what results you see.

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

 

Diet con trung