在C#中使用递归在水平和垂直方向上搜索特定单词的平方2D数组 [英] In C# search through a squared 2D array horizontally and vertically for specific words using recursion

查看:79
本文介绍了在C#中使用递归在水平和垂直方向上搜索特定单词的平方2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个2D数组,其尺寸为[9,9],格式如下:



 Berzas,su 
la ;; sula;
; klevu sa
ldial lap
asula a
aula,
r suart
zemes vai
kai du





我的起始文本文件已格式化为上面的数组:



 Berzas,sula ;; sula ;; klevu saldial lapasula a aila,ar suart zemes vaikai du 





我需要找到的金额这些单词使用递归出现在数组中:



 Sula 
Alus





输出应该是:



 Sula 3 
Alus 2





我的尝试:



我是只读取文本文件并将其格式化为数组。



  string  file = File.ReadAllText( @  C:\ Users \Justas\Desktop\L1 \Rekursija \Trecias.txt); 


string [,] array = new string [n,n];

var list = Enumerable
.Range( 0 ,file .Length / n)
。选择(i = > file.Substring(i * n,n))
.ToList();

var res = string .Join(Environment.NewLine,list);
for int i = 0 ; i < n; i ++)
{
char [] row = list [i] .ToCharArray();
for int j = 0 ; j < n; j ++)
{
array [i,j] = row [j] .ToString();
}
}

int rowLength = array.GetLength( 0 );
int colLength = array.GetLength( 1 );





我不知道如何解决这个问题。任何帮助将不胜感激

解决方案

我们非常愿意帮助那些陷入困境的人:但这并不意味着我们在这里全力以赴您!我们不能做所有的工作,你要么得到报酬,要么是你的成绩的一部分,我们为你做这一切都不公平。



所以我们需要你做这项工作,当你遇到困难时我们会帮助你。这并不意味着我们会为您提供一步一步的解决方案,您可以提交!



首先考虑一下如何手动完成它并将其解决小心。

如果你确定你的方法每次都有效,请记下来。

现在再按照你自己的指示再做一次。如果它工作,太棒了!如果没有,请修改说明直到它为止。



现在你有一个算法,并且可以考虑如何将其转换为设计,并且然后是代码。

然后测试一下。并使用不同的数据再次测试它。使用调试器找出它失败时无效的原因。


这是一个思考和计划而不是编码的练习 - 尽管编码也很重要 - 它是为了让你开始我该怎么做?而不只是盲目地编码你所喂养的设计。



你会到达那里 - 你只需要花更多的时间思考,而不是编码!

Hello, i have a 2D array which has the dimensions of [9, 9] it is formatted like this:

Berzas,su
la;;sula;
;klevu sa
ldial lap
asula  a 
  aula, a
r  suart 
zemes vai
kai du   



My starting text file which i have formatted into the array above:

Berzas,sula;;sula;;klevu saldial lapasula  a   aila, ar  suart zemes vaikai du   



I need to find the amount of times these words appear in the array using recursion:

Sula
Alus



The output should be:

Sula 3
Alus 2



What I have tried:

I've only read the text file and formatted it into a array.

string file = File.ReadAllText(@"C:\Users\Justas\Desktop\L1\Rekursija\Trecias.txt");


         string[,] array = new string[n, n];

           var list = Enumerable
           .Range(0, file.Length / n)
           .Select(i => file.Substring(i * n, n))
           .ToList();

           var res = string.Join(Environment.NewLine, list);
           for (int i = 0; i < n; i++)
           {
               char[] row = list[i].ToCharArray();
               for (int j = 0; j < n; j++)
               {
                   array[i, j] = row[j].ToString();
               }
           }

           int rowLength = array.GetLength(0);
           int colLength = array.GetLength(1);



I am out of ideas on how to go about this problem. Any help would be greatly appreciated

解决方案

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!

Start by thinking about how you would go about it manually and work that out carefully.
When you are sure that your method works each time, write it down.
Now do it again, following your own instructions absolutely. If it works, great! If it doesn't, modify the instructions until it does.

Now you have an algorithm, and can start by think about how to translate that into a design, and then code.
Then test it. And test it again with different data. Use the debugger to find out why it doesn't work when it fails.

This is an exercise in thinking and planning more than coding - though the coding is important too - it's there to get you started on "how do I do this?" rather than just blindly coding a design that you are fed with.

You'll get there - you just need to spend more time thinking, and a lot less coding!


这篇关于在C#中使用递归在水平和垂直方向上搜索特定单词的平方2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆