在描述数字范围时,“独占”和“包含”是什么意思?

时间:2016-08-18 04:30:51

标签: algorithm terminology definition

简单的问题但是,在引用数字范围时,我看到了排他性和包容性。

例如,这是算法手册中的一行:

  

以下函数打印从1到n(包括)的2的幂。

这是什么意思?什么使数字范围包容或排他?

4 个答案:

答案 0 :(得分:27)

在计算机科学中,包容性/排他性不适用于算法,而是适用于数字范围(更具体地说,适用于范围的端点):

1 through 10 (inclusive)
1 2 3 4 5 6 7 8 9 10

1 through 10 (exclusive)
1 2 3 4 5 6 7 8 9

在数学中,上面的2个范围是:

[1, 10]
[1, 10)

你可以轻松记住它:

  • Inclu sive - Inclu ding最后一个号码
  • 排除 sive - 排除 ding最后一个号码

答案 1 :(得分:16)

  

以下函数打印从1到n(包括)的2的幂。

这意味着该函数会计算 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" tools:context=".SettingActivity" android:baselineAligned="false"> <Button android:id="@+id/imageButtonHomeSelector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/icon_home_config" android:onClick="imageButtonHomeSelector" /> <Button android:id="@+id/imageButtonLoginSelector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/icon_login_config" android:onClick="imageButtonLoginSelector" /> <Button android:id="@+id/imageButtonSettingSelector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/setting_button_hover" android:onClick="imageButtonSettingSelector" /> </LinearLayout> 2^i,换句话说,i = 1, 2, ..., n可以包含从1到的值,包括值{ {1}}。即 包含 包含

另一方面,如果你的书说过:

  

以下函数从1到n打印2的幂(独占)。

这意味着i,即n可以取值达n-1,但包括i = 1, 2, ..., n-1,这意味着{{1}它是可能拥有的最高价值。 排除 独占

答案 2 :(得分:2)

n 的值包括 2 和 5 [2,5] 包括两个数字,以防万一只有第一个被包括在内 编程术语 n>=2 && n<=5

n 的值不包括 2 和 5 [2,5) n>=2 && n<5

答案 3 :(得分:1)

简单来说,“包含”是指数字n之内和,而“独占”是指数字n之内和没有。

注意:每个参数都应标记为“包含性” /“参与性”

# 1 (inclusive) through 5 (inclusive)
1 <= x <= 5 == [1, 2, 3, 4, 5]

# 1 (inclusive) through 5 (exclusive)
1 <= x < 5 == [1, 2, 3, 4]

# 1 (exclusive) through 5 (inclusive)
1 < x <= 5 == [2, 3, 4, 5]

# 1 (exclusive) through 5 (exclusive)
1 < x < 5 == [2, 3, 4]