qq_62233822 2021-11-04 02:01 采纳率: 85.7%
浏览 83
已结题

.txt 数组计算局部最大值

在文本文件中,第一行包含一个整数(不超过 10000 ) - 数组中元素的数量,在接下来的行中 - 每行一个整数 - 此数组的元素。计算数组中局部最大值的个数(局部最大值严格大于相邻数组元素,第一个和最后一个数组元素肯定不是局部最大值)。所有数字都在 -10 ^ 9 到 10 ^ 9 的范围内。
输出数据格式
写出结果如下:在第一行 - 由单个空格分隔的数组元素,在第二行 - 定义了局部最大值的那些数组元素的索引,按降序排列(元素的索引从一个开始) .如果数组为空,则创建一个空的输出文件。输出文件中的行首和行尾不允许有空格。

img

  • 写回答

2条回答 默认 最新

  • 广大菜鸟 2021-11-04 11:55
    关注
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<stdio.h>
    int main(){
        char IndexResult[10001][100];
        FILE *infile,*outfile;
        infile = fopen("E:/C/Test/1.txt","r");
        outfile = fopen("E:/C/Test/2.txt","w+");
        if(!infile||!outfile){
            printf("file couldn't be open!");
            return -1;
        }
        int num,i,length=0;
        int grand,father,son;
        fscanf(infile,"%d",&num); 
        fscanf(infile,"%d",&grand);
        fscanf(infile,"%d",&father);
        fscanf(infile,"%d",&son);
        if(father>grand&&father>son){
            sprintf(IndexResult[length++],"%d",2);
        }
        fprintf(outfile,"%d %d %d ",grand,father,son);
        for(i=4;i<=num;i++){
            grand=father;father=son;
            fscanf(infile,"%d",&son);
            fprintf(outfile, "%d ",son);
            if(father>grand&&father>son){
                sprintf(IndexResult[length++],"%d",(i-1));
            }
        }
        fputc('\n',outfile);
        for(i=length-1;i>=0;i--){
             fprintf(outfile, "%s ",IndexResult[i]);
        }
        fclose(outfile);
        fclose(infile);
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月13日
  • 已采纳回答 11月5日
  • 创建了问题 11月4日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?