zoukankan      html  css  js  c++  java
  • 结对开发2__最大子数组

     

    /*======================================================================
                # Author: TianYongTao&&ZhangYaPeng
                # E-Mail: 714251829@qq.com
                # Last modified: 2015-03-25 22:13
                # Filename: Demo.cpp
                # Description: 
    ======================================================================*/
    # include <iostream>
    # include <stdio.h>
    # include <fstream>
    # include <string>
    # define MaxRow 20
    # define MaxCol 20
    using namespace std;
    int Arr[MaxRow][MaxCol];
    
    int SumArr(int x1,int y1,int x2,int y2)                    //子数组求和
    {
        int sum=0;
        for(int i=x1;i<x2;i++)
        {
            for(int j=y1;j<y2;j++)
            {
                sum+=Arr[i][j];
            }
        }
        return sum;
    }
    
    void LoadFile(int Arr[][MaxCol],int & Row,int & Col)            //读取文件到数组
    {
        FILE * infile = fopen("D:\input.txt","r");
        int num[100];
        int count=0;
        if(!infile)
        {
            cout<<"文件读取失败!"<<endl;
            exit(-1);
        }
        else
        {
            char str[10];
            fscanf(infile,"%[^,]%*c",str);                        //使用正则表达式过滤掉','
            Row = atof(str);
            fscanf(infile,"%[^,]%*c",str);
            Col = atof(str);
            while(!feof(infile))                                    //此时读取的是数组的内容  先将其放入一个整型数组
            {
                fscanf(infile,"%[^,]%*c",str);
                num[count++]=atof(str);
            }
        }
        for(int i=Row-1;i>=0;i--)                                //将整型数组内容放入Arr数组中
        {
            for(int j=Col-1;j>=0;j--)
            {
                Arr[i][j]=num[--count];
            }
        }
    }
    
    //测试函数
    int main()
    {
        int Row=0;
        int Col=0;
        LoadFile(Arr,Row,Col);
        int x1,y1;                                            //代表左上角点的横纵坐标
        int x2,y2;                                            //代表右上角点的横纵坐标
        int max = Arr[0][0];                            //max存储比较过程中的最大值
        for(x1=0;x1<Row;x1++)
        {
            for(y1=0;y1<Col;y1++)
            {
                for(x2=x1+1;x2<=Row;x2++)
                {
                    for(y2=y1+1;y2<=Col;y2++)
                    {
                        if(SumArr(x1,y1,x2,y2)>max)
                        {
                            max = SumArr(x1,y1,x2,y2);
                        }
                    }
                }
            }
        }
        cout<<"该数组的最大子数组的和为:"<<max<<endl;
        return 0;
    }
  • 相关阅读:
    [WinAPI] API 9 [文件的删除、复制和移动功能]
    2014-3-7 星期五 [困]
    2014-3-6 星期四 [第一天执行分析]
    [WinAPI] API 8 [获取磁盘空间信息]
    [WinAPI] API 7 [判断光驱内是否有光盘]
    [WinAPI] API 6 [操作驱动器挂载点]
    Eclipse 生成WebService客户端代码
    Windows7下安装与破解IntelliJ IDEA2017(转载)
    Tomcat的安装
    IntelliJ IDEA的几种常见的快捷键
  • 原文地址:https://www.cnblogs.com/littlechar/p/4368356.html
Copyright © 2011-2022 走看看