菜鸟也做有道难题①

重在参与 嘿嘿。。。

思路和我的扫雷程序一样, 找到周围的所有的萝卜总数是否在AB之间,这里我没有考虑AB的大小关系。

每块地由格子(Cell)组成,每个格子(Cell)有三个属性:地标(X,Y)和萝卜数(Count) 。

逻辑很简单,遍历地(Field)里所有的格子(List<Cell>),找到当前格子(CurrentCell)周围的萝卜数是否在AB之间。 

5

9

3

0

0

26

spend 5428 milliseconds


复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string[] test0 = { "111""111""111" };
            
string[] test1 = { "111""141""111" };
            
string[] test2 = { "2309""0239""2314" };
            
string[] test3 = { "924""231""390""910""121" };
            
string[] test4 = { "5" };
            
string[] test5 = { "1234567890""1234567890""1234567890""1234567890""1234567890""1234567890""1234567890""1234567890""1234567890""1234567890""1234567890" };
            Console.WriteLine(countSpecialNumbers(test0, 
48));
            Console.WriteLine(countSpecialNumbers(test1, 
48));
            Console.WriteLine(countSpecialNumbers(test2, 
57));
            Console.WriteLine(countSpecialNumbers(test3, 
3136));
            Console.WriteLine(countSpecialNumbers(test4, 
38));
            Console.WriteLine(countSpecialNumbers(test5, 
318));

            System.Diagnostics.Stopwatch watch 
= new System.Diagnostics.Stopwatch();
            watch.Start();
            
for (int i = 0; i < 10000; i++)
            {
                countSpecialNumbers(test0, 
48);
                countSpecialNumbers(test1, 
48);
                countSpecialNumbers(test2, 
57);
                countSpecialNumbers(test3, 
3136);
                countSpecialNumbers(test4, 
38);
                countSpecialNumbers(test5, 
318);
            }
            watch.Stop();

            Console.WriteLine(
"spend {0} milliseconds", watch.ElapsedMilliseconds);
            Console.Read();
        }

        
public static int countSpecialNumbers(string[] field, int A, int B)
        {
            List
<Cell> list = GetList(field);
            
int Cellcount = 0;
            
foreach (Cell cell in list)
            {
                
int Sumcount = 0;
                
foreach (Cell currentCell in list)
                {
                    
if (Math.Abs(currentCell.X - cell.X) < 2 &&
                        Math.Abs(currentCell.Y 
- cell.Y) < 2 &&
                        
!(currentCell.X == cell.X && currentCell.Y == cell.Y))
                    {
                        Sumcount 
+= currentCell.Count;
                    }
                }
                
if (Sumcount >= A && Sumcount <= B)
                    Cellcount
++;
            }
            
return Cellcount;
        }
        
//把数组转换成List<Field>
        public static List<Cell> GetList(string[] field)
        {
            List
<Cell> list = new List<Cell>();
            
for (int y = 0; y < field.Length; y++)
            {
                Char[] row 
= field[y].ToCharArray();
                
for (int x = 0; x < row.Length; x++)
                {
                    list.Add(
new Cell() { X = x, Y = y, Count = int.Parse(row[x].ToString()) });
                }
            }
            
return list;
        }
    }


    
public class Cell//一个单元格子地
    {
        
public int X { getset; }//地的坐标x
        public int Y { getset; }//y
        public int Count { getset; }//地的胡萝卜数
    }
}
复制代码

posted @   君之蘭  阅读(453)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示