ada - 如何获得整数的 10 对数?
问题描述
我想要求用户输入一个整数(N),然后我显示他/她输入的整数的 10 对数。我已经成功计算了 10 对数,但不知道如何显示它,如下所示:
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.
这是我的代码:
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;
解决方案
以下程序将计算满足 Ada 子类型的任何值Positive
对任何Positive
底的整数对数。
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;
使用上面的示例执行时,结果输出为:
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 时,结果为
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
推荐阅读
- python - 如何使用 less 作为函数并合并绘图?
- c++ - 可序列化对象与谷物
- nginx - 将 https://www.subdomain.example.com 重定向到 https://subdomain.example.com(删除 www)
- c# - 如何发布这个依赖 Nuget 的 C# 源代码生成器?
- node.js - 运行“npm run start-dev”时 Hyper 出错(尝试在没有解决方案代码的情况下打开项目)
- html - CSS DIV 和 span 优先级
- bnf - Purescript 是否有可用的 BNF 类型语法?
- api - 从 Google Api 中删除“$”
- flutter-web - 如何在每个页面上都有一个导航栏 Flutter Web
- c# - 循环遍历一个大数组并将值加在一起 C#