【问题标题】:C# - Calculate Sum of Surrounding Characters in a list of StringsC# - 计算字符串列表中周围字符的总和
【发布时间】:2019-03-25 01:37:07
【问题描述】:

假设我在 List<string> 中有 4 个字符串。我在这里以 x y 和 z 为例,但可以是任何字符。

"xyyy",
"yyyz",
"yyxz",
"yyyy"

在这个字符串列表中,我将如何根据 x 计算环绕 Y 的位置?所以定位 X 并计算所有接触的 Y 点,注意我希望忽略 z 并且在这种情况下只定位 y。在左上角的 x 的情况下,它将是 3 个接触 y 点

我开始使用ToCharArray() 遍历字符串,并跟踪我在列表中位置的上下限,但它很快就变成了递归地狱。我正在寻找更好的解决方案来遍历每个列表并检查它是否有上下界,然后遍历所有字符。

为了澄清起见:

让我们放大列表中的前两个字符串

"xyyy",
"yyyz"

我们可以清楚地看到,x 与 y 向右,对角线和下方 1 个接触,这意味着总共有 3 个接触 y。

然后我必须输出到控制台 x 触摸列表中 y PER 字符串的次数 - 换句话说:每行

【问题讨论】:

  • 不止一个 x。那你想做什么,还是只想抛出一个异常,然后在这种情况下删除C盘
  • 第一个"xyyy" 和第三个"yyxz" 不是两个接触y 点吗?
  • 是的,当我点击 char x @TheGeneral 时,我需要遍历字符串列表中的每个 char 并找到所有周围的 ys
  • 很难提供答案,因为我在这里无法理解您的问题。因此,如果您能提供更多详细信息,我们将能够为您提供帮助
  • @AhmedSherien 没有 x 触及 y 右下角和对角线

标签: c# arrays string char


【解决方案1】:

如果你知道行长,并且你知道目标的索引(通过扫描整个数组找到x,那么计算相邻方块的索引很简单

int i= //some index;
int l= //row length;
int y = i/l;//the floored quotient is the y offset
int x = i % l;//the remainder is the x offset

//Note that a negative result or a number greater than the whole array length is illegal so you have to catch that in your logic
int n= i-w;// skip a row back
int s = i+w;// skip a row forward
//if we hit the end then illegal else adjacent
int e= x+1 ==l? -1 : i+1;  
int w= x-1 <0? -1: i-1;

//如果您不想将字符串列表转换为二维数组,只需如上所述逐行执行,但现在 y 只是成为 Lst 的索引,而 x 是字符串的扫描。当您找到目标字符时,其相邻值将是:

char n = list[y-1][x];
char s = list[y+1][x];
char e = list[y][x+1];
char w = list[y][x-1];
char sw= list[y+1][x-1];
char se= list[y+1][x+1];
//etc.

请记住检查边界,只需catch IndexOutOfRange,然后继续循环,以避免大量乏味的专门检查逻辑。

这些是我的餐巾纸计算结果,您可能需要查看图片。另外,如果您想要对角线,我将其留作练习。提示:对新生成的索引使用相同的逻辑。

如果你有字符值,有很多求和它们的例子,我认为困难的部分是找到邻接。

【讨论】:

  • 你说的很对,找到相邻的不是主要问题,但是遍历前后的字符串并找到对角线是我面临的问题。
  • 也许我误解了您如何转换为 char 数组,我将转换我的代码以适应不将列表转换为二维数组。
  • 我一直在修补他的 .这正是我需要的谢谢。
【解决方案2】:

这是您所描述的您想要的粗略实现。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Touching {

    class CharFinder {

        public static List<Tuple<int, int>> counted = new List<Tuple<int, int>>();

        public static void Main(string[] args) {
            getInputs(out char target, out char indexed, out List<string> lines);
            List<Tuple<int, int>> indexes = new List<Tuple<int, int>>();
            for (int i = 0; i != lines.Count; i++) {
                for (int j = 0; j != lines[i].Length; j++) {
                    if (lines[i][j] == indexed) {
                        indexes.Add(new Tuple<int, int>(i, j));
                    }
                }
            } int tCount = countNeighbor(lines, indexes[0], target);
            for (int i = 0; i != indexes.Count; i++) {
                tCount += countNeighbor(lines, indexes[i], target);
            } Console.WriteLine(tCount.ToString()); 
            Console.ReadLine();
        }

        public static int countNeighbor(List<string> grid, Tuple<int, int> ind, char target) {
            int count = 0;
            for (int i = ind.Item1 - 1; i < ind.Item1 + 2; i++) {
                if (i == -1 || i >= grid.Count) { continue; } 
                for (int j = ind.Item2 - 1; j < ind.Item2 + 2; j++) {
                    if (j == -1 || j >= grid[i].Length) { continue; }
                    if (grid[i][j] == target && !counted.Contains(new Tuple<int, int>(i, j))) { 
                        counted.Add(new Tuple<int, int>(i, j));
                        count++; 
                    }
                }
            } return count;
        }

        public static void getInputs(out char target, out char indexed, out List<string> strs) {
            int lines = 0;
            strs = new List<string>();
            while (true) {
                Console.Clear();
                Console.Write("Number of lines?: ");
                try { lines = Convert.ToInt32(Console.ReadLine()); if (lines < 1) { throw new Exception(); } break; }
                catch { Console.WriteLine("ERROR: Must be a positive integer."); Console.ReadLine(); }
            } Console.Clear();
            Console.Write("Target?: ");
            target = Console.ReadLine()[0];
            Console.Clear();
            Console.Write("Indexed?: ");
            indexed = Console.ReadLine()[0];
            for (int i = 0; i < lines; i++) {
                strs.Add(Console.ReadLine());
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2018-05-12
    相关资源
    最近更新 更多