动态的数组

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
append.c, append.cc,

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

AC代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_LEN 11
double* allocate(int n)
{
    double *p;
    p=(double *)malloc(sizeof(double)*n);
    return p;
}
void input(double* p, int n)
{
    int i;
    for(i=0; i<n; i++)
        scanf("%lf",&p[i]);
}
void output(double* p, int n)
{
    int i,j;
    int k;
    scanf("%d %d",&i,&j);
    if(i>n||j<=0)//注意这里i>n而不是i>=n;
        printf("no output\n");
    else if((i>0&&i<=n)&&j>n)
    {
        for(k=i-1; k<=n-1; k++)
            printf("%g\n",p[k]);//为什么这里输出%lf是不对的
    }
    else if((i>0&&i<=n)&&j<=n)
    {
        for(k=i-1; k<=j-1; k++)
            printf("%g\n",p[k]);
    }
    else if(i<=0&&j>n)//这里忘记讨论i比1还小的时候的范围了;
    {
        for(k=0; k<=n-1; k++)
            printf("%g\n",p[k]);
    }
    else if(i<=0&&j<=n)
    {
        for(k=0; k<=j-1; k++)
            printf("%g\n",p[k]);
    }

}
void release(double* p)
{
    free(p);
}

int main()
{
    int len;
    double *array;
    scanf("%d", &len);
    array = allocate(len);
    input(array, len);
    output(array, len);
    release(array);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值