题目
In this question, the function INT() is the largest integer less than or equal to . For example, INT(5.7) = 5, INT(8) = 8, INT(−2.3) = −3.
Consider the following algorithm.
Step 1: Input
Step 2: Calculate
Step 3: Let = INT()
Step 4: Calculate
Step 5: Calculate
Step 6: Output
Step 7: Replace by
Step 8: If then STOP, otherwise go back to Step 2
(a) Complete the table in the answer book, using , to show the results obtained at each step of the algorithm.
(b) Explain how the output values of the algorithm relate to the original input , where is any positive integer.
题目中文翻译
在此问题中,函数 INT() 是小于或等于 的最大整数。例如,INT(5.7) = 5,INT(8) = 8,INT(−2.3) = −3。
考虑以下算法。
第 1 步:输入
第 2 步:计算
第 3 步:设 = INT()
第 4 步:计算
第 5 步:计算
第 6 步:输出
第 7 步:用 替换
第 8 步:如果 则停止,否则回到第 2 步
(a) 使用 完成答案本中的表格,显示算法每一步获得的结果。
(b) 解释算法的输出值与原始输入 之间的关系,其中 是任意正整数。
解答
(a)
解法一
思路
展开
每轮先把当前 除以 10,再取整数部分得到 。这样 是删去当前个位数后的整十部分,而 就是被删去的个位数。随后用 替换 ,重复到 。
答题过程
展开
The completed table is
| 4217 | 421.7 | 421 | 4210 | 7 |
| 421 | 42.1 | 42 | 420 | 1 |
| 42 | 4.2 | 4 | 40 | 2 |
| 4 | 0.4 | 0 | 0 | 4 |
| 0 |
Therefore the output values, in order, are
(b)
解法一
思路
展开
每轮输出当前 的个位数,然后把该个位数删去。因此输出依次是原输入的个位、十位、百位,直到最高位,也就是把原输入的各位数字反向输出。
答题过程
展开
For any positive integer , the first output is the units digit, the second output is the tens digit, the third output is the hundreds digit, and so on.
Hence the algorithm outputs the digits of the original input in reverse order.