夜的咖啡

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

夜的咖啡 的算法 839毫秒

eaglet 的算法 1919毫秒

原帖:http://www.cnblogs.com/eaglet/archive/2009/06/01/1493801.html

刚刚学C#……算法也没怎么去看……就凭着自己的想法写了……代码看起来很简单哈

 

 

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleEx
{
    
public class Run
    {
        
public static void Main()
        {
            Stopwatch sw 
= new Stopwatch();

            
string[] testField = {
                
"1234567890",
                 
"1234567890",
                 
"1234567890",
                 
"1234567890",
                 
"1234567890",
                 
"1234567890",
                 
"1234567890",
                 
"1234567890",
                 
"1234567890",
                 
"1234567890",
                 
"1234567890"};

            
//夜的咖啡 的算法
            Console.WriteLine("夜的咖啡 的算法");
            Console.WriteLine(T2.countSpecialNumbers(
new string[] { "111""111""111" }, 48));
            Console.WriteLine(T2.countSpecialNumbers(
new string[] { "111""141""111" }, 48));
            Console.WriteLine(T2.countSpecialNumbers(
new string[] { "2309""0239""2314" }, 57));
            Console.WriteLine(T2.countSpecialNumbers(
new string[] { "924""231""390""910""121" }, 3136));
            Console.WriteLine(T2.countSpecialNumbers(
new string[] { "5" }, 38));
            Console.WriteLine(T2.countSpecialNumbers(testField, 
318));


            sw.Start();
            
for (int i = 0; i < 100000; i++)
            {
                T2.countSpecialNumbers(testField, 
318);
            }

            sw.Stop();

            Console.WriteLine(
"用时" + sw.ElapsedMilliseconds.ToString() + "毫秒");

            Console.WriteLine(
"eaglet 的算法");

            Console.WriteLine(T1.countSpecialNumbers(
new string[] { "111""111""111" }, 48));
            Console.WriteLine(T1.countSpecialNumbers(
new string[] { "111""141""111" }, 48));
            Console.WriteLine(T1.countSpecialNumbers(
new string[] { "2309""0239""2314" }, 57));
            Console.WriteLine(T1.countSpecialNumbers(
new string[] { "924""231""390""910""121" }, 3136));
            Console.WriteLine(T1.countSpecialNumbers(
new string[] { "5" }, 38));
            Console.WriteLine(T1.countSpecialNumbers(testField, 
318));

            sw.Reset();
            sw.Start();


            
for (int i = 0; i < 100000; i++)
            {
                T1.countSpecialNumbers(testField, 
318);
            }

            sw.Stop();

            Console.WriteLine(
"用时" + sw.ElapsedMilliseconds.ToString() + "毫秒");


            Console.ReadKey();
        }


        
class T2
        {
            
/**/
            
/// <summary>
            
/// 获取第i,j格的特殊程度
            
/// </summary>
            
/// <param name="field"></param>
            
/// <param name="i"></param>
            
/// <param name="j"></param>
            
/// <returns></returns>
            
/// <author>NightCoffee</author>
            static private int GetSpecialNumbers(string[] field, int width, int i, int j)
            {
                
                
int sum = 0;
                
if (i - 1 >= 0)//上面有格子
                {
                    sum 
+= field[i-1][j] - '0';
                    
if (j - 1 >= 0)
                    {
                        sum 
+= field[i - 1][j-1- '0';
                    }
                    
if (j + 1 <= width - 1)
                    {
                        sum 
+= field[i - 1][j+1- '0';
                    }
                }
                
if (i + 1 <= field.Length - 1//下面有格子
                {
                    sum 
+= field[i+1][j] - '0';
                    
if (j - 1 >= 0)
                    {
                        sum 
+= field[i + 1][j - 1- '0';
                    }
                    
if (j + 1 <= width - 1)
                    {
                        sum 
+= field[i + 1][j + 1- '0';
                    }
                }
                
if (j - 1 >= 0//左边有格子
                {
                    sum 
+= field[i][j - 1- '0';
                }
                
if (j + 1 <= width - 1//右边有格子
                {
                    sum 
+= field[i][j + 1- '0';
                }
                
return sum;
            }

            
/**/
            
/// <summary>
            
/// 
            
/// </summary>
            
/// <param name="field"></param>
            
/// <param name="A"></param>
            
/// <param name="B"></param>
            
/// <returns></returns>
            
/// <author>NightCoffee Base on eaglet</author>
            static public int countSpecialNumbers(string[] field, int A, int B)
            {
                
//Check paramaters
                if (field == null)
                {
                    
return 0;
                }

                
if (field.Length == 0)
                {
                    
return 0;
                }

                
int width = field[0].Length;

                
if (width == 0)
                {
                    
return 0;
                }

                
if (A > B)
                {
                    
throw new ArgumentException("A > B");
                }

                
//Begin calculate
                int count = 0;

                
for (int i = 0; i < field.Length; i++)
                {
                    
for (int j = 0; j < field[i].Length; j++)
                    {
                        
int number = GetSpecialNumbers(field, width, i, j);

                        
if (number >= A && number <= B)
                        {
                            count
++;
                        }
                    }
                }

                
return count;
            }
        }
        
class T1
        {
            
/**/
            
/// <summary>
            
/// 获取第i,j格的特殊程度
            
/// </summary>
            
/// <param name="field"></param>
            
/// <param name="i"></param>
            
/// <param name="j"></param>
            
/// <returns></returns>
            
/// <author>eaglet</author>
            static private int GetSpecialNumbers(string[] field, int width, int i, int j)
            {
                
int y = i - 1 >= 0 ? i - 1 : 0;
                
int sum = 0;

                
while (y <= i + 1 && y < field.Length)
                {
                    
int x = j - 1 >= 0 ? j - 1 : 0;

                    
while (x <= j + 1 && x < width)
                    {
                        
if (x != j || y != i)
                        {
                            sum 
+= field[y][x] - '0';
                        }

                        x
++;
                    }

                    y
++;
                }

                
return sum;
            }

            
/**/
            
/// <summary>
            
/// 
            
/// </summary>
            
/// <param name="field"></param>
            
/// <param name="A"></param>
            
/// <param name="B"></param>
            
/// <returns></returns>
            
/// <author>eaglet</author>
            static public int countSpecialNumbers(string[] field, int A, int B)
            {
                
//Check paramaters
                if (field == null)
                {
                    
return 0;
                }

                
if (field.Length == 0)
                {
                    
return 0;
                }

                
int width = field[0].Length;

                
if (width == 0)
                {
                    
return 0;
                }

                
if (A > B)
                {
                    
throw new ArgumentException("A > B");
                }

                
//Begin calculate
                int count = 0;

                
for (int i = 0; i < field.Length; i++)
                {
                    
for (int j = 0; j < field[i].Length; j++)
                    {
                        
int number = GetSpecialNumbers(field, width, i, j);

                        
if (number >= A && number <= B)
                        {
                            count
++;
                        }
                    }
                }

                
return count;
            }
        }
    }

}
posted on 2009-06-01 20:14  夜的咖啡  阅读(1716)  评论(4编辑  收藏  举报