6-4 在数组中查找指定元素 (10 分)

6-4 在数组中查找指定元素 (10 分)
本题要求实现一个在数组中查找指定元素的简单函数。

函数接口定义:

int search( int list[], int n, int x );

其中list[]是用户传入的数组;n(≥0)是list[]中元素的个数;x是待查找的元素。如果找到

则函数search返回相应元素的最小下标(下标从0开始),否则返回−1。

裁判测试程序样例:

#include <stdio.h>
#define MAXN 10

int search( int list[], int n, int x );

int main()
{
    int i, index, n, x;
    int a[MAXN];

    scanf("%d", &n);
    for( i = 0; i < n; i++ )
        scanf("%d", &a[i]);
    scanf("%d", &x);
    index = search( a, n, x );
    if( index != -1 )
        printf("index = %d\n", index);
    else
        printf("Not found\n");

    return 0;
}

/* 你的代码将被嵌在这里 */
输入样例1:

5
1 2 2 5 4
2

输出样例1:

index = 1

输入样例2:

5
1 2 2 5 4
0

输出样例2:

Not found

、、、 最简单明了,如果找到了就直接返回,没找到就走完这个循环在出来返回-1,
不要想太多 =_=;
int search( int list[], int n, int x ){

  int i;
  for(i=0;i<n;i++){
    if(list[i]==x)
    return i;}
    return -1;

  
  
}
好的,我已经接收到你的问题了。以下是对你的问题的回答: 1. 顺序查找程序的代码如下: ``` int sequential_search(int arr[], int n, int x) { for (int i = 0; i < n; i++) { if (arr[i] == x) { return i; } } return -1; } ``` 其中,arr[]为待查找的数组,n为数组的长度,x为要查找元素。函数返回查找到的元素数组中的下标,若未找到则返回-1。 2. 二查找程序的代码如下: ``` int binary_search(int arr[], int n, int x) { int left = 0, right = n - 1; while (left <= right) { int mid = (left + right) / 2; if (arr[mid] == x) { return mid; } else if (arr[mid] > x) { right = mid - 1; } else { left = mid + 1; } } return -1; } ``` 其中,arr[]为待查找的有序数组,n为数组的长度,x为要查找元素。函数返回查找到的元素数组中的下标,若未找到则返回-1。 3. 建立二叉排序树的程序代码如下: ``` struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; void insert(struct TreeNode **root, int val) { if (*root == NULL) { struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode)); node->val = val; node->left = node->right = NULL; *root = node; } else if (val < (*root)->val) { insert(&((*root)->left), val); } else { insert(&((*root)->right), val); } } ``` 其中,TreeNode结构体表示二叉排序树的节点,val表示节点的值,left和right别表示左子树和右子树。insert函数用于向二叉排序树中插入一个元素。 4. 在二叉排序树上的查找、插入、删除结点的程序代码如下: ``` struct TreeNode *find(struct TreeNode *root, int val) { if (root == NULL || root->val == val) { return root; } else if (val < root->val) { return find(root->left, val); } else { return find(root->right, val); } } void delete(struct TreeNode **root, int val) { if (*root == NULL) { return; } else if (val < (*root)->val) { delete(&((*root)->left), val); } else if (val > (*root)->val) { delete(&((*root)->right), val); } else { if ((*root)->left == NULL) { struct TreeNode *temp = *root; *root = (*root)->right; free(temp); } else if ((*root)->right == NULL) { struct TreeNode *temp = *root; *root = (*root)->left; free(temp); } else { struct TreeNode *temp = (*root)->right; while (temp->left != NULL) { temp = temp->left; } (*root)->val = temp->val; delete(&((*root)->right), temp->val); } } } void insert(struct TreeNode **root, int val) { if (*root == NULL) { struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode)); node->val = val; node->left = node->right = NULL; *root = node; } else if (val < (*root)->val) { insert(&((*root)->left), val); } else { insert(&((*root)->right), val); } } ``` 其中,find函数用于在二叉排序树中查找一个元素,delete函数用于在二叉排序树中删除一个元素,insert函数用于向二叉排序树中插入一个元素。 5. 使二叉排序树中序输出的程序代码如下: ``` void inorder(struct TreeNode *root) { if (root == NULL) { return; } inorder(root->left); printf("%d ", root->val); inorder(root->right); } ``` 其中,inorder函数用于对二叉排序树进行中序遍历,即先遍历左子树,然后输出根节点,最后遍历右子树。 6. 选择式菜单程序代码如下: ``` #include <stdio.h> int main() { int choice; while (1) { printf("查找子系统\n"); printf("****************************************\n"); printf("* 1------顺 序 查 找 *\n"); printf("* 2------二 查 找 *\n"); printf("* 3------二 叉 排 序 树 *\n"); printf("* 0------返 回 *\n"); printf("****************************************\n"); printf("请选择菜单号(0--3):"); scanf("%d", &choice); switch (choice) { case 0: printf("您已退出查找子系统。\n"); return 0; case 1: // 顺序查找 break; case 2: // 二查找 break; case 3: // 二叉排序树 break; default: printf("输入有误,请重新输入。\n"); break; } } return 0; } ``` 其中,使用while循环不断显示菜单,根据用户的选择执行相应的操作。如果用户选择0,则退出程序。如果用户输入的菜单号不在1-3之间,则提示输入有误。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值