zoukankan      html  css  js  c++  java
  • 求一个已知二维数组的最大子数组和(司宇,廖强)

    小组成员:司宇,廖强

    设计流程:

            

    设计界面:

                 

    程序设计:1.封装一个求二维整数组最大子数组和的子程序;

                      2.设计一个主函数,主函数可以调用子函数;

                      3.在主函数中添加代码,使主函数可以调用一个TXT文件并且得到要求的结果。

    遇到的问题:1.在调用txt文件时,没办法使调用文件前两行分别显示行数和列数;

                          2.在调用子函数的时候,不知道应该赋值给子函数一个什么类型的参数;

                          3.如何将String类型的二维数组转化为int类型。

    解决方案:(当我们遇到问题的时候,我们两人分别独立思考解决方案,然后互相分析对方的解决方案,最后得到结局方案。)

                       1.将Txt文件中的第一行和第二行分别赋值给行和列,计算二维数组时从第三行开始计算。

                       2.将一个二维数组和改数组的行和列一起赋值给子函数,代码如下:maxsum = maxSubArray(intlist, h, l);

                       3.intlist[i-2] = Array.ConvertAll<string, int>(str[i].Split(','), p => { return int.Parse(p); })。

    源代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public int maxSubArray(int[][] a, int n, int m)
            {
                int[][] p = new int[m][];
                int i, j;
                if (m == 0 || n == 0)
                    return 0;
                for (i = 0; i < n; i++)
                {
                    p[i] = new int[m];
                    for (j = 0; j < m; j++)
                    {
                        if (i == 0)
                        {
                            if (j == 0)
                                p[i][j] = a[i][j];
                            else
                                p[i][j] = p[i][j - 1] + a[i][j];
                        }
                        else
                        {
                            if (j == 0)
                                p[i][j] = p[i - 1][j] + a[i][j];
                            else
                                p[i][j] = p[i][j - 1] + p[i - 1][j] - p[i - 1][j - 1] + a[i][j];
                        }
                    }
                }
                int temp;
                int max = a[0][0];
                int ans = a[0][0];
                if (m == 1)
                {
                    for (i = 0; i < n; i++)
                    {
                        for (j = i; j < n; j++)
                        {
                            if (i == 0)
                            {
                                temp = p[j][m - 1];
                            }
                            else
                            {
                                temp = p[j][m - 1] - p[i - 1][m - 1];
                            }
                            if (ans < temp)
                                ans = temp;
                        }
                    }
                }
                else
                {
                    for (i = 0; i < n; i++)
                    {
                        for (j = i; j < n; j++)
                        {
                            if (i == 0)
                            {
                                temp = p[j][m - 1] - p[j][m - 2];
                            }
                            else
                            {
                                temp = p[j][m - 1] - p[j][m - 2] - p[i - 1][m - 1] + p[i - 1][m - 2];
                            }
                            for (int k = m - 2; k >= 0; k--)
                            {
                                if (temp < 0)
                                    temp = 0;
                                if (i == 0)
                                {
                                    if (k == 0)
                                        temp += p[j][k];
                                    else
                                        temp += p[j][k] - p[j][k - 1];
                                }
                                else
                                {
                                    if (k == 0)
                                        temp += p[j][k] - p[i - 1][k];
                                    else
                                        temp += p[j][k] - p[j][k - 1] - p[i - 1][k] + p[i - 1][k - 1];
                                }
                                if (ans < temp)
                                    ans = temp;
                            }
                        }
                    }
                }
                return ans;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog file = new OpenFileDialog();
                file.Filter = "文本文件|*.txt";
                if (file.ShowDialog() == DialogResult.OK)
                {
                    StreamReader sr = new StreamReader(file.FileName, System.Text.Encoding.Default);
                    textBox1.Text = sr.ReadToEnd();
                    sr.Close();
                }
                else
                    return;
                int maxsum;
                /*double hang = Convert.ToDouble(textBox3.Text);
                double lie = Convert.ToDouble(textBox4.Text);*/
                string[] str = textBox1.Text.Split(new string[] { "
    " }, StringSplitOptions.RemoveEmptyEntries);
                int[][] intlist = new int[str.Length][];
                int h = (int)Convert .ToDouble( str[0]);
                int l = (int)Convert.ToDouble(str[1]);
                for (int i = 2; i < str.Length; i++)
                {
                    intlist[i-2] = Array.ConvertAll<string, int>(str[i].Split(','), p => { return int.Parse(p); });
                }
                maxsum = maxSubArray(intlist, h, l);
                textBox2.Text = maxsum.ToString();
                textBox3.Text =Convert.ToString(h);
                textBox4.Text = Convert.ToString(l);
      }       
    

    运行结果:1.TXT文件截图:

           

                       2.运行结果图:

             

    小组合照:

              

  • 相关阅读:
    hdu 1199 Color the Ball 离散线段树
    poj 2623 Sequence Median 堆的灵活运用
    hdu 2251 Dungeon Master bfs
    HDU 1166 敌兵布阵 线段树
    UVALive 4426 Blast the Enemy! 计算几何求重心
    UVALive 4425 Another Brick in the Wall 暴力
    UVALive 4423 String LD 暴力
    UVALive 4872 Underground Cables 最小生成树
    UVALive 4870 Roller Coaster 01背包
    UVALive 4869 Profits DP
  • 原文地址:https://www.cnblogs.com/qiyuea/p/9825357.html
Copyright © 2011-2022 走看看