致力于人工智能的废物 2021-11-24 19:03 采纳率: 0%
浏览 30

无C不行-废物一个 学习第五天打卡

6-13 折半查找 (15 common.points)

给一个严格递增数列,函数int Search_Bin(SSTable T, KeyType k)用来二分地查找k在数列中的位置。

函数接口定义:

int  Search_Bin(SSTable T, KeyType k)

其中T是有序表,k是查找的值。

裁判测试程序样例:


#include <iostream>
using namespace std;

#define MAXSIZE 50
typedef int KeyType;

typedef  struct                     
{ KeyType  key;                                             
} ElemType;  

typedef  struct
{ ElemType  *R; 
  int  length;
} SSTable;                      

void  Create(SSTable &T)
{ int i;
  T.R=new ElemType[MAXSIZE+1];
  cin>>T.length;
  for(i=1;i<=T.length;i++)
     cin>>T.R[i].key;   
}

int  Search_Bin(SSTable T, KeyType k);

int main () 
{  SSTable T;  KeyType k;
   Create(T);
   cin>>k;
   int pos=Search_Bin(T,k);
   if(pos==0) cout<<"NOT FOUND"<<endl;
   else cout<<pos<<endl;
   return 0;
}

/* 请在这里填写答案 */

###输入格式:

第一行输入一个整数n,表示有序表的元素个数,接下来一行n个数字,依次为表内元素值。 然后输入一个要查找的值。

###输出格式:

输出这个值在表内的位置,如果没有找到,输出"NOT FOUND"。

int  Search_Bin(SSTable T, KeyType k)
{
    int start=0,end=T.length-1;
    while(start<=end)
    {
        int mid=(start+end)/2;
        if(T.R[mid].key==k)
        {
            return mid;
        }
        else if(T.R[mid].key>k)
        {
            end=mid-1;
        }
        else
        {
            start=mid+1;
        }
    }
    return 0;
}

6-12 判断奇偶性 (10 common.points)

本题要求实现判断给定整数奇偶性的函数。

函数接口定义:

int even( int n );

其中n是用户传入的整型参数。当n为偶数时,函数返回1;n为奇数时返回0。注意:0是偶数。

裁判测试程序样例:

#include <stdio.h>

int even( int n );

int main()
{    
    int n;

    scanf("%d", &n);
    if (even(n))
        printf("%d is even.\n", n);
    else
        printf("%d is odd.\n", n);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1:

-6

结尾无空行

输出样例1:

-6 is even.

结尾无空行

int even( int n )
{
    if(n%2==0)
        return 1;
    else
        return 0;
}
  • 写回答

1条回答 默认 最新

  • 雨下,听风 2023-01-02 10:18
    关注

    ?没有问题是吗?

    评论

报告相同问题?

问题事件

  • 创建了问题 11月24日

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像