山东科技大学OJ 1383 动态的数组

Problem E: 动态的数组

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 13471  Solved: 4822
[Submit][Status]

Description

输入N个浮点数,输出其中第m个~第n个数,满足m<n。

-----------------------------------------------------------------------------

你需要编写以下4个函数,完成这个程序:

double* allocate(int n),在动态内存上分配n个元素的double型数组,并返回其首地址。

void input(double* p, int n),向p中输入n个double型数值。

void output(double* p, int n),把长度为n的数组p中符合条件的第m个~第n个元素输出,m和n从标准输入读取。

void release(double* p),释放p所指的动态内存空间。

函数的调用格式见“Append Code”。

Input

输入的第一个整数N(在int范围内),表示后跟N个double类型的浮点数。输入的最后是两个整数m和n,满足m<n,m和n在int范围内,但不一定在1~N之间。

Output

输出第m个~第n个之间的浮点数,每个数占一行。若区间[m,n]和[1,N]没有任何重叠,则输出“no output”。区间[m,n]和[1,N有交集]时,但给出的n超出第N个数的范围,则只需输出到第N个数,m超出范围时同样处理。

Sample Input

20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 15 25

Sample Output

15 16 17 18 19 20

HINT

Append Code

int main()
{
    int len;
    double *array;
    scanf("%d", &len);
    array = allocate(len);
    input(array, len);
    output(array, len);
    release(array);
}

Codes

#include<stdio.h>
#include<stdlib.h>
double* allocate(int n)
{
    double *p = (double*)malloc(sizeof(double)*n);
    return p;
}
void input(double* p, int n)
{
    for(int i=0;i<n;i++)
    {
        scanf("%lf",p+i);
    }
}
void output(double* p, int n)
{
    int x,y;
    scanf("%d %d",&x,&y);
    if(x>n || y<1) printf("no output");
    else
    {
        if(x<1) 
        {
            for(int i=0;i<=y-1;i++)
            {
            printf("%g\n",*(p+i));
            }
        }
        else if(y>n)
        {
            for(int i=x-1;i<=n-1;i++)
            {
            printf("%g\n",*(p+i));
            }
        }
        else
        {
            for(int i=x-1;i<=y-1;i++)
            {
            printf("%g\n",*(p+i));
            }
        }
    }
}
void release(double* p)
{
    free(p);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值