题目描述
请编写程序,将 nnn 个整数存入顺序表,对任一给定整数 xxx,查找其在顺序表中的位置。
输入格式:
输入首先在第一行给出正整数 nnn(≤104\le 10^4≤104);随后一行给出 nnn 个 int 范围内的不重复的整数,数字间以空格分隔;最后一行给出待查找的元素 xxx,也是 int 范围内的整数。
输出格式:
在一行中输出 xxx 在顺序表中的位置,即数组下标。如果没找到,则输出 -1。注意数组下标从 0 开始。
输入样例:
5
1 2 3 4 5
4
输出样例:
3
输入样例:
5
4 3 6 8 0
1
输出样例:
-1
解题思路
顺序表在内存中是连续存储的,查找元素 xxx 最直接的方法是顺序查找(线性查找):从下标 0 开始逐个比较,找到第一个等于 xxx 的元素即返回其下标。
- 由于题目保证数据不重复,第一个匹配到的位置就是唯一答案;用
pos记录结果,默认 -1 表示未找到。 - 时间复杂度:O(n)O(n)O(n),最坏情况需要比较全部元素。
- 空间复杂度:O(n)O(n)O(n)(存储顺序表本身)。
代码流程说明
- 读入 nnn,并将 nnn 个整数存入数组
a。 - 读入待查找元素 xxx。
- 将
pos初始化为 -1。 - 从下标 0 到 n−1n-1n−1 遍历数组,若
a[i] == x,记录pos = i并跳出循环。 - 输出
pos并换行。
代码实现
cpp
#include <iostream>
using namespace std;
const int MAXN = 10005;
int a[MAXN];
int main() {
int n, x;
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
cin >> x;
int pos = -1;
for (int i = 0; i < n; ++i) {
if (a[i] == x) { pos = i; break; }
}
cout << pos << endl;
return 0;
}
代码流程图
#mermaid-svg-AP5Kzjzy7Qtn1Y3b{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .error-icon{fill:#552222;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .marker{fill:#333333;stroke:#333333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .marker.cross{stroke:#333333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b p{margin:0;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .cluster-label text{fill:#333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .cluster-label span{color:#333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .cluster-label span p{background-color:transparent;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .label text,#mermaid-svg-AP5Kzjzy7Qtn1Y3b span{fill:#333;color:#333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .node rect,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .node circle,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .node ellipse,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .node polygon,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .rough-node .label text,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .node .label text,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .image-shape .label,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .icon-shape .label{text-anchor:middle;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .rough-node .label,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .node .label,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .image-shape .label,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .icon-shape .label{text-align:center;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .node.clickable{cursor:pointer;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .arrowheadPath{fill:#333333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .cluster text{fill:#333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .cluster span{color:#333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b rect.text{fill:none;stroke-width:0;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .icon-shape,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .icon-shape p,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .icon-shape .label rect,#mermaid-svg-AP5Kzjzy7Qtn1Y3b .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-AP5Kzjzy7Qtn1Y3b :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
是
否
否
开始
读入 n 和数组元素
读入待查找元素 x
pos 初始化为 -1
i 从 0 到 n-1
a i 是否等于 x
pos 等于 i 并跳出循环
输出 pos
i 加 1
结束
解题流程图
#mermaid-svg-yfNLG9saJOmZGCTD{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yfNLG9saJOmZGCTD .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yfNLG9saJOmZGCTD .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yfNLG9saJOmZGCTD .error-icon{fill:#552222;}#mermaid-svg-yfNLG9saJOmZGCTD .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yfNLG9saJOmZGCTD .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yfNLG9saJOmZGCTD .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yfNLG9saJOmZGCTD .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yfNLG9saJOmZGCTD .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yfNLG9saJOmZGCTD .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yfNLG9saJOmZGCTD .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yfNLG9saJOmZGCTD .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yfNLG9saJOmZGCTD .marker.cross{stroke:#333333;}#mermaid-svg-yfNLG9saJOmZGCTD svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yfNLG9saJOmZGCTD p{margin:0;}#mermaid-svg-yfNLG9saJOmZGCTD .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-yfNLG9saJOmZGCTD .cluster-label text{fill:#333;}#mermaid-svg-yfNLG9saJOmZGCTD .cluster-label span{color:#333;}#mermaid-svg-yfNLG9saJOmZGCTD .cluster-label span p{background-color:transparent;}#mermaid-svg-yfNLG9saJOmZGCTD .label text,#mermaid-svg-yfNLG9saJOmZGCTD span{fill:#333;color:#333;}#mermaid-svg-yfNLG9saJOmZGCTD .node rect,#mermaid-svg-yfNLG9saJOmZGCTD .node circle,#mermaid-svg-yfNLG9saJOmZGCTD .node ellipse,#mermaid-svg-yfNLG9saJOmZGCTD .node polygon,#mermaid-svg-yfNLG9saJOmZGCTD .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yfNLG9saJOmZGCTD .rough-node .label text,#mermaid-svg-yfNLG9saJOmZGCTD .node .label text,#mermaid-svg-yfNLG9saJOmZGCTD .image-shape .label,#mermaid-svg-yfNLG9saJOmZGCTD .icon-shape .label{text-anchor:middle;}#mermaid-svg-yfNLG9saJOmZGCTD .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yfNLG9saJOmZGCTD .rough-node .label,#mermaid-svg-yfNLG9saJOmZGCTD .node .label,#mermaid-svg-yfNLG9saJOmZGCTD .image-shape .label,#mermaid-svg-yfNLG9saJOmZGCTD .icon-shape .label{text-align:center;}#mermaid-svg-yfNLG9saJOmZGCTD .node.clickable{cursor:pointer;}#mermaid-svg-yfNLG9saJOmZGCTD .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yfNLG9saJOmZGCTD .arrowheadPath{fill:#333333;}#mermaid-svg-yfNLG9saJOmZGCTD .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yfNLG9saJOmZGCTD .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yfNLG9saJOmZGCTD .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yfNLG9saJOmZGCTD .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yfNLG9saJOmZGCTD .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yfNLG9saJOmZGCTD .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yfNLG9saJOmZGCTD .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yfNLG9saJOmZGCTD .cluster text{fill:#333;}#mermaid-svg-yfNLG9saJOmZGCTD .cluster span{color:#333;}#mermaid-svg-yfNLG9saJOmZGCTD div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yfNLG9saJOmZGCTD .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yfNLG9saJOmZGCTD rect.text{fill:none;stroke-width:0;}#mermaid-svg-yfNLG9saJOmZGCTD .icon-shape,#mermaid-svg-yfNLG9saJOmZGCTD .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yfNLG9saJOmZGCTD .icon-shape p,#mermaid-svg-yfNLG9saJOmZGCTD .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yfNLG9saJOmZGCTD .icon-shape .label rect,#mermaid-svg-yfNLG9saJOmZGCTD .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yfNLG9saJOmZGCTD .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yfNLG9saJOmZGCTD .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yfNLG9saJOmZGCTD :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
是
否
在顺序表中查找元素 x
读入 n 和表元素
读入待查找元素 x
从下标 0 开始逐个比较
是否找到等于 x 的元素
返回该元素下标
是否还有元素
返回 -1
输出结果