C语言任意输入一个数,查找这个数在已存在的一组数据中所在的位置,存在则输出数组元素下标,不存在则打印提示
时间: 2024-06-04 12:13:02 浏览: 17
可以简单地使用循环比对数组中的元素,找到该数所在的位置,如果找到就输出该数的下标,否则打印提示。下面是 C 语言的实现代码:
#include <stdio.h>
int main() {
int data[5] = {10, 20, 30, 40, 50};
int num, i, pos = -1;
printf("请输入一个数:");
scanf("%d", &num);
for (i = 0; i < 5; i++) {
if (data[i] == num) {
pos = i;
break;
}
}
if (pos != -1) {
printf("%d 在数组中的位置是:%d\n", num, pos);
} else {
printf("%d 不在数组中\n", num);
}
return 0;
}
相关问题
用c语言输入一个整数,在数组中查找是否存在该数,若存在则显示其所在的数组下标位置,否则显示NOEXIST,查找使用search函数实现
可以使用一个名为search的函数实现查找操作。以下是示例代码:
```c
#include <stdio.h>
#define MAX_N 100
int search(int x, int arr[], int n) {
for (int i = 0; i < n; ++i) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
int main() {
int n, x, arr[MAX_N];
printf("请输入数组大小:");
scanf("%d", &n);
// 输入数组
printf("请输入%d个整数:\n", n);
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}
// 输入查找的数
printf("请输入要查找的整数:");
scanf("%d", &x);
// 查找并输出结果
int idx = search(x, arr, n);
if (idx != -1) {
printf("该数在数组中的下标为:%d\n", idx);
} else {
printf("NOEXIST\n");
}
return 0;
}
```
在search函数中,使用for循环遍历数组中的所有元素,如果找到了与x相等的元素,则返回其下标;否则返回-1表示没找到。在主函数中,调用search函数进行查找,并根据返回值输出结果。
在数组中查找两个数之和等于输入的另一个数(栈和队列)c语言
在数组中查找两个数之和等于输入的另一个数可以使用栈和队列的方法来实现。下面是一种基本的思路:
1. 创建一个栈和一个队列,分别用于存储数组中的元素和已经遍历过的元素。
2. 遍历数组中的每个元素:
- 如果当前元素与队列中的某个元素之和等于目标数,则找到了符合条件的两个数。
- 否则,将当前元素入栈,并将其加入队列。
3. 如果遍历完整个数组都没有找到符合条件的两个数,则表示不存在这样的组合。
以下是C语言的示例代码:
```c
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 100
bool findSum(int arr[], int size, int target) {
int stack[MAX_SIZE];
int queue[MAX_SIZE];
int top = -1;
int front = 0;
int rear = 0;
for (int i = 0; i < size; i++) {
// 检查当前元素与队列中的元素之和是否等于目标数
for (int j = front; j < rear; j++) {
if (arr[i] + queue[j] == target) {
printf("找到两个数:%d 和 %d\n", arr[i], queue[j]);
return true;
}
}
// 将当前元素入栈,并加入队列
stack[++top] = arr[i];
queue[rear++] = arr[i];
}
printf("不存在符合条件的两个数\n");
return false;
}
int main() {
int arr[] = {2, 4, 6, 8, 10};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 14;
findSum(arr, size, target);
return 0;
}
```
相关推荐
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)