首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何得到一个整数的10-对数?

如何得到一个整数的10-对数?
EN

Stack Overflow用户
提问于 2020-09-23 05:40:59
回答 1查看 120关注 0票数 2

我想让用户输入一个Integer(N),然后显示他/她输入的Integer的10个对数。我已经成功地计算了10个对数,但不知道如何像下面的示例那样显示它:

代码语言:javascript
复制
Write in an Integer: 455666
455666 / 10 = 45566
45566 / 10 = 4556
4556 / 10 = 455
455 / 10 = 45
45 / 10 = 4
Answer: 10-logarithm of 455666 is 5.

下面是我的代码:

代码语言:javascript
复制
with Ada.Text_IO;                    use Ada.Text_IO;
with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;
with Ada.Float_Text_IO;              use Ada.Float_Text_IO;

procedure Logarithm is
  X: Integer;
begin
  Put("Write in an Integer: ");
  Get(X);
  for I in 1..X loop
    if 10**I<=X then
      Put(I);
    end if;
  end loop;
end Logarithm;
EN

回答 1

Stack Overflow用户

发布于 2020-09-23 07:23:23

下面的程序将计算满足Ada子类型Positive到任意Positive底的任意值的整数对数。

代码语言:javascript
复制
with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Integer_Logarithm is
   subtype Base_Range is Integer range 2..Integer'Last;
   Count : Natural := 0;
   Base  : Base_Range;
   Num   : Positive;
   Inpt  : Positive;
begin
   Put ("Enter the integer logarithmic base: ");
   Get (Base);
   Put ("Enter the integer: ");
   Get (Inpt);
   Num := Inpt;
   while Num >= Base loop
      Put (Num'Image & " /" & Base'Image & " =");
      Num := Num / Base;
      Put_Line (Num'Image);
      Count := Count + 1;
   end loop;
   Put_Line
     ("The integer logarithm (base" & Base'image & ") of" & Inpt'Image &
      " is" & Count'Image);
end Integer_Logarithm;

当使用上面的示例执行时,结果输出为:

代码语言:javascript
复制
Enter the integer logarithmic base: 10
Enter the integer: 455666
 455666 / 10 = 45566
 45566 / 10 = 4556
 4556 / 10 = 455
 455 / 10 = 45
 45 / 10 = 4
The integer logarithm (base 10) of 455666 is 5

当为基数2和值256执行时,结果为

代码语言:javascript
复制
Enter the integer logarithmic base: 2
Enter the integer: 256
 256 / 2 = 128
 128 / 2 = 64
 64 / 2 = 32
 32 / 2 = 16
 16 / 2 = 8
 8 / 2 = 4
 4 / 2 = 2
 2 / 2 = 1
The integer logarithm (base 2) of 256 is 8
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64018043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档