zoukankan      html  css  js  c++  java
  • 贪心算法

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 using System.IO;
      7 
      8 namespace test
      9 {
     10     class Program
     11     {
     12         const int COUNT = 10;
     13 
     14         const int MIDDLE = 0;
     15         static void Main(string[] args)
     16         {
     17             //  将数组赋值(-100 -- 100)
     18             int[] iArray = new int[COUNT];
     19             Random random = new Random(DateTime.Now.Millisecond);
     20             for (var i = 0; i < COUNT;i++ )
     21             {
     22                 iArray[i] = random.Next(-COUNT, COUNT);
     23             }
     24 
     25             //  下面是算法
     26             List<Caculation> cache = new List<Caculation>();
     27             var data = new Caculation();
     28             int add = 0;
     29 
     30             for (int i = 0; i < COUNT ; i++)
     31             {
     32                 add += iArray[i];
     33 
     34                 if (add > 0)
     35                 {
     36                     if (add >= data.max)
     37                     {
     38                         data.end = i;
     39                         data.max = add;
     40                         if (i == COUNT - 1)
     41                         {
     42                             cache.Add(data);
     43                         }
     44                     }
     45                     else
     46                     {
     47                         cache.Add(data);    //  提交上一个
     48                         var temp = new Caculation(data);
     49                         temp.end = i;       //  下一个不归零
     50                         temp.max = add;
     51                         data = temp;
     52                     }
     53                 }
     54                 else
     55                 {
     56                     data.max = add;
     57                     data.end = i;
     58                     cache.Add(data);        //  提交这一个
     59                     add = 0;                //  下一个归零
     60                     var temp = new Caculation();
     61                     temp.start = i + 1;
     62                     data = new Caculation(temp);
     63                 }
     64             }
     65             
     66 
     67             int max = 0;
     68             Caculation c = null;
     69             for (var i = 0; i < cache.Count; i++)
     70             {
     71                 Console.WriteLine(cache[i].ToString());
     72                 if (cache[i].max > max)
     73                 {
     74                     c = cache[i];
     75                     max = c.max;
     76                 }
     77             }
     78 
     79 
     80 
     81             //显示结果
     82             int s = 0;
     83             foreach (var i in iArray)
     84             {
     85                 Console.Write(i+" , ");
     86                 s += i;
     87             }
     88             Console.WriteLine("
    The Max is" + c);
     89 
     90             Console.ReadLine();
     91         }
     92         class Caculation
     93         {
     94             public int max;
     95             public int start, end;
     96             public Caculation()
     97             {
     98                 this.max = 0;
     99                 this.start = 0;
    100                 this.end = 0;
    101             }
    102             public Caculation(Caculation c)
    103             {
    104                 this.max = c.max;
    105                 this.start = c.start;
    106                 this.end = c.end;
    107             }
    108             public override string ToString()
    109             {
    110                 return "This Caculation is Max: " + max + " Start: " + start + " end: " + end;
    111             }
    112         }
    113     }
    114 }
  • 相关阅读:
    BasKet Note Pads-运用软件作笔记
    K3b-全功用的光盘烧录器材
    GShutDown:自动化关机小工具
    Xournal-条记抄写软件
    Metisse:相称酷的窗口操持器
    Realtek ALC268集成声卡驱动成绩在ubuntu下的处置责罚方案
    Wixi-桌面端 Wiki 运用
    Avant Window Navigator:Dock 类的窗口导航要领
    DDM:剪贴板办理及截取屏幕
    快速搞定Windows Xp Pro繁体中文版的简体支持
  • 原文地址:https://www.cnblogs.com/c233/p/8108390.html
Copyright © 2011-2022 走看看