C#算法解决萝卜地问题是什么呢?首先让我们来看看题目:
话说你在走路上班时,经过一片种植萝卜的农田。这块田地的形状是一个矩形的网格。
field的第i个元素的第j个字符,表示田地的第i行第j列的格子里包含的萝卜的数目。
我们定义一个格子的特殊程度为它周围所有格子的萝卜个数的和; 它周围的格子包含它上下左右以及对角相邻的格子,
最多有8个,在田地的边界上的格子会少一些。如果一个格子周围没有别的格子,则它的特殊程度为0。
请返回田地中特殊程度在A和B之间的所有格子的数目(包含A,B)。
Definition
Class: NumberField
Method: countSpecialNumbers
Parameters: string[], int, int
Returns: int
Method signature: int countSpecialNumbers(string[] field, int A, int B)
(be sure your method is public)
Constraints
- field 包含1个到50个元素,含1和50。
- field的每个元素包含1个到50个字符,含1和50。
- field的每个元素包含相同的字符数。
- field的每个元素的字符均为’0’到’9’之一。
- A的范围会在0到100之间,含0和100。
- B 的范围会在A到100之间,含A和100。
C#算法的具体实现如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace Field
{
/// <summary>
/// 2009.6.1 with ann by hooyes
/// </summary>
public class NumberField
{
static void Main(string[] args)
{
//Test .
string[] fieldx ={
"1234567890",
"1234567890",
"1234567890",
"1234567890",
"1234567890",
"1234567890",
"1234567890",
"1234567890",
"1234567890",
"1234567890",
"1234567890"};
int r = countSpecialNumbers(fieldx, 3, 18);
Console.WriteLine(r);
Console.Read();
}
/// <summary>
/// 获取第i,j格的萝卜数
/// </summary>
/// <param name="i">行</param>
/// <param name="j">列</param>
/// <param name="fieldx"></param>
/// <returns></returns>
public static int fij(int i, int j,string[] fieldx)
{
int Rvalue = 0;
if (i < 0 || j < 0)
{
return 0;
}
int y = fieldx.Length - 1; //行
int x = fieldx[0].Length - 1; //列
if ((i > y) || (j > x))
{
return 0;
}
string f = fieldx[i];
byte[] hooyesBy = System.Text.Encoding.ASCII.GetBytes(f);
char xB = (char)hooyesBy[j];
Rvalue = int.Parse(xB.ToString());
return Rvalue;
}
/// <summary>
/// 获取第i,j格的特殊度
/// </summary>
/// <param name="i">行</param>
/// <param name="j">列</param>
/// <param name="fieldx"></param>
/// <returns></returns>
public static int fsp(int i, int j, string[] fieldx)
{
int Rvalue = 0;
Rvalue = fij(i - 1, j, fieldx) +
fij(i + 1, j, fieldx) +
fij(i, j - 1, fieldx) +
fij(i, j+1, fieldx) +
fij(i - 1, j - 1, fieldx) +
fij(i + 1, j + 1, fieldx) +
fij(i - 1, j + 1, fieldx) +
fij(i + 1, j - 1, fieldx);
return Rvalue;
}
/// <summary>
/// 返回满足特殊度在A至B之间的格子数
/// </summary>
/// <param name="field"></param>
/// <param name="A">特殊度下限</param>
/// <param name="B">特殊度上限</param>
/// <returns></returns>
public static int countSpecialNumbers(string[] field, int A, int B)
{
int Rvalue = 0;
//int i=0;
//int j=0;
int y = field.Length-1; //行
int x = field[0].Length-1 ; //列
for (int i = 0; i <=y; i++)
{
for (int j = 0; j <=x;j++)
{
int tInt = fsp(i, j, field);
if (tInt >= A && tInt <= B)
{
Rvalue++;
}
}
}
return Rvalue;
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
C#算法C#算法解决萝卜地问题就向你介绍点到这里,希望对你理解C#算法有所帮助。
【编辑推荐】