求助! 看图求一循环。

pengwu666 2014-12-09 06:30:12
...全文
364 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
{"5", 2, 2} 写错了.
  • 打赏
  • 举报
回复
现在的人啊,只会用现成的代码啦,给你C#版的
static void Main(string[] args)
        {
            var arr = new object[9, 3]{
                 {"1", 0, 0},
                 {"2", 0, 0},
                 {"3", 1, 0},
                 {"4", 0, 0},
                 {"5", 2, 2},
                 {"6", 1, 1},
                 {"7", 0, 0},
                 {"8", 2, 2},
                 {"9", 0, 0}
            };

            fx(arr, 0, 9);
        }
        static void fx(object[,] arr, int x, int y)
        {
            for (; x < y; x++)
            {
                Console.WriteLine(arr[x,0]);
                for (var i = 0; i < (int)arr[x, 1]; i++)
                {
                    fx(arr, x - (int)arr[x, 2], x);
                    Console.WriteLine(arr[x, 0]);
                }
            }
        }
lkhuge 2014-12-11
  • 打赏
  • 举报
回复
引用 14 楼 pengwu666 的回复:
[quote=引用 7 楼 lkhuge 的回复:] 我这里就不用WinForm了 直接用控制台代码吧
string[] textList   = {"1","2","3","4","5","6","7","8","9"};
//int[]    countList  = { 0 , 0 , 1 , 0 , 2 , 1 , 0 , 0 , 0 };
//int[]    jumpList   = { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 };

int[]    countList =  { 0 , 0 , 1 , 0 , 2 , 1 , 0 , 2 , 0 };
int[]    jumpList   = { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 2 , 0 };

List<List<string>> logList = new List<List<string>>();
for (int i = 0; i < textList.Length; i++)
{
    List<string> tempList = new List<string>();
    logList.Add(tempList);
    for (int c = 0; c <= countList[i]; c++)
    {
        tempList.Add(textList[i]);
        Console.WriteLine(textList[i]);
        if (c == countList[i]) continue;
        for (int j = i - jumpList[i]; j < i; j++)
        {
            tempList.AddRange(logList[j]);
            foreach (string v in logList[j])
            {
                Console.WriteLine(v);
            }
        }
    }
}
这个测试可以,我要仔细看看。[/quote] 写的比较仓促 其实代码还可以在调整优化 反正凑合着大概看看吧~~
pengwu666 2014-12-11
  • 打赏
  • 举报
回复
引用 7 楼 lkhuge 的回复:
我这里就不用WinForm了 直接用控制台代码吧
string[] textList   = {"1","2","3","4","5","6","7","8","9"};
//int[]    countList  = { 0 , 0 , 1 , 0 , 2 , 1 , 0 , 0 , 0 };
//int[]    jumpList   = { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 };

int[]    countList =  { 0 , 0 , 1 , 0 , 2 , 1 , 0 , 2 , 0 };
int[]    jumpList   = { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 2 , 0 };

List<List<string>> logList = new List<List<string>>();
for (int i = 0; i < textList.Length; i++)
{
    List<string> tempList = new List<string>();
    logList.Add(tempList);
    for (int c = 0; c <= countList[i]; c++)
    {
        tempList.Add(textList[i]);
        Console.WriteLine(textList[i]);
        if (c == countList[i]) continue;
        for (int j = i - jumpList[i]; j < i; j++)
        {
            tempList.AddRange(logList[j]);
            foreach (string v in logList[j])
            {
                Console.WriteLine(v);
            }
        }
    }
}
这个测试可以,我要仔细看看。
pengwu666 2014-12-11
  • 打赏
  • 举报
回复
引用 8 楼 Z65443344 的回复:
看不懂你这是怎么跳的,规则写的太少了 按你的规则,6如果写了1就跳转到5的话,不是应该死循环了吗
第一张图的第三行就写了1,结果并没有23232323...的死循环啊,对吧
pengwu666 2014-12-11
  • 打赏
  • 举报
回复
引用 10 楼 Z65443344 的回复:
而且不仅规则少,规则之间还互相矛盾 2明明写的是输出跟文本没关系,3里又有关系了 好歹写个明确的规则出来啊
跳转只会执行一次,4楼的那位说的没有错。不会有死循环。 第一列的文本内容与流程是没有关系。只是为了直观改为1-9,改成其他如何内容都可以,只是按顺序输出。
ajaxfeifei 2014-12-11
  • 打赏
  • 举报
回复

public class mainclass
{
        public void main()
{
var writeobjects=new List<WriteObject>(){new WriteObject("1",0,0)};
WriteArry(writeobjects);
}
        public void Write(StringBuilder sb, WriteObject writeObject)
        {
            for (int i = 0; i <= writeObject.Times; i++)
            {
                sb.AppendLine(writeObject.Text);
            }
        }

        public void WriteArry(List<WriteObject> objects)
        {
            var sb=new StringBuilder();
            for (int i = 0; i < objects.Count; i++)
            {
                var writeObject = objects[i];
                Write(sb, writeObject);
                if (writeObject.JumpIndex > 0)
                {
                    Write(sb, objects[i-writeObject.Times]);
                }
            }
        }
    }

    public class WriteObject
    {
        public string Text { get; set; }
        public int Times { get; set; }
        public int JumpIndex { get; set; }
        public WriteObject(string text,int times,int jumpindex)
{
Text=text;
Times=times;
JumpIndex=jumpindex;
}
    }
effun 2014-12-11
  • 打赏
  • 举报
回复
给你来个复杂的版本

    class Program
    {
        static void Main(string[] args)
        {
            RuleExecuter exec = new RuleExecuter();

            exec.AddRule("1", 0, 0);
            exec.AddRule("2", 0, 0);
            exec.AddRule("3", 1, 0);
            exec.AddRule("4", 0, 0);
            exec.AddRule("5", 2, 0);
            exec.AddRule("6", 1, 1);
            exec.AddRule("7", 0, 0);
            exec.AddRule("8", 2, 2);
            exec.AddRule("9", 0, 0);

            exec.Execute(Console.Out);

            Console.ReadKey();
        }
    }

    class Rule
    {
        public Rule(string content, int max, int jump)
        {
            Content = content;
            MaxOutput = max;
            Jump = jump;
        }

        /// <summary>
        /// 文本
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 输出次数
        /// </summary>
        public int MaxOutput { get; set; }

        /// <summary>
        /// 跳转
        /// </summary>
        public int Jump { get; set; }

        int _count;

        public void Execute(TextWriter output)
        {
            output.WriteLine(Content);
            _count++;
        }

        public bool HasFinished
        {
            get { return _count > MaxOutput; }
        }

        public void Reset()
        {
            _count = 0;
        }
    }

    class RuleExecuter
    {

        public RuleExecuter()
        {
            _rules = new List<Rule>();
        }

        private List<Rule> _rules;

        public List<Rule> Rules
        {
            get { return _rules; }
        }

        public Rule AddRule(string content, int max, int jump)
        {
            Rule rule = new Rule(content, max, jump);
            _rules.Add(rule);
            return rule;
        }

        public void Execute(TextWriter output)
        {
            Rule[] array = _rules.ToArray();
            int index = 0, length = array.Length;

            while (index < length)
            {
                Rule rule = array[index];

                if (rule.HasFinished)
                    rule.Reset();

                rule.Execute(output);

                if (!rule.HasFinished)
                {
                    index -= rule.Jump;
                }
                else
                    index++;
            }
        }
    }
FTD_Fred 2014-12-10
  • 打赏
  • 举报
回复
这个东西不是叫递归么
pengwu666 2014-12-10
  • 打赏
  • 举报
回复
引用 4 楼 qldsrx 的回复:
[quote=引用 2 楼 pengwu666 的回复:] 输出次数应该是没问题的。但是要加跳转的就搞不懂了。
说明文字看了吗?第三条规则,如果跳转次数1,就执行上面一条输出规则,2则执行上面第二条执行规则,跳转是跳到某条规则的意思,至于那条规则会输出多少次,或者那条规则本身是否还存在跳转,都没关系,照着那条规则执行就可以了。不过被跳转的规则执行是一次性的,例如第二个图中,第8条规则执行后,再执行它的跳转——第6条规则,第6条规则执行后,又去执行它的跳转第5条规则,第5条规则没有跳转,执行后就回到第8条规则,而不是停留在第5条规则,之后第8条规则结束,执行第9条规则。[/quote] 流程的确是这样的,我的问题是怎样实现这个循环。
  • 打赏
  • 举报
回复
<!DOCTYPE html>
<html>
    <head>
        <title>结贴啦</title>
    </head>
    <body></body>
    <script type="text/javascript">
        var arr = [
            ['1', 0, 0],
            ['2', 0, 0],
            ['3', 1, 0],
            ['4', 0, 0],
            ['5', 2, 0],
            ['6', 1, 1],
            ['7', 0, 0],
            ['8', 2, 2],
            ['9', 0, 0]
        ], str = [];

        (function fx(x, y) {
            for(;x < y; x++) {
                str.push(arr[x][0]);
                for(var i = 0; i < arr[x][1]; i++) {
                    fx(x - arr[x][2], x);
                    str.push(arr[x][0]);
                }
            }
        })(0, arr.length);

        document.body.innerHTML = str.join('<br/>');
</script>
</html>
於黾 2014-12-10
  • 打赏
  • 举报
回复
而且不仅规则少,规则之间还互相矛盾 2明明写的是输出跟文本没关系,3里又有关系了 好歹写个明确的规则出来啊
於黾 2014-12-10
  • 打赏
  • 举报
回复
看不懂你这是怎么跳的,规则写的太少了 按你的规则,6如果写了1就跳转到5的话,不是应该死循环了吗
lkhuge 2014-12-10
  • 打赏
  • 举报
回复
我这里就不用WinForm了 直接用控制台代码吧
string[] textList   = {"1","2","3","4","5","6","7","8","9"};
//int[]    countList  = { 0 , 0 , 1 , 0 , 2 , 1 , 0 , 0 , 0 };
//int[]    jumpList   = { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 };

int[]    countList =  { 0 , 0 , 1 , 0 , 2 , 1 , 0 , 2 , 0 };
int[]    jumpList   = { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 2 , 0 };

List<List<string>> logList = new List<List<string>>();
for (int i = 0; i < textList.Length; i++)
{
    List<string> tempList = new List<string>();
    logList.Add(tempList);
    for (int c = 0; c <= countList[i]; c++)
    {
        tempList.Add(textList[i]);
        Console.WriteLine(textList[i]);
        if (c == countList[i]) continue;
        for (int j = i - jumpList[i]; j < i; j++)
        {
            tempList.AddRange(logList[j]);
            foreach (string v in logList[j])
            {
                Console.WriteLine(v);
            }
        }
    }
}
qldsrx 2014-12-09
  • 打赏
  • 举报
回复
引用 2 楼 pengwu666 的回复:
输出次数应该是没问题的。但是要加跳转的就搞不懂了。
说明文字看了吗?第三条规则,如果跳转次数1,就执行上面一条输出规则,2则执行上面第二条执行规则,跳转是跳到某条规则的意思,至于那条规则会输出多少次,或者那条规则本身是否还存在跳转,都没关系,照着那条规则执行就可以了。不过被跳转的规则执行是一次性的,例如第二个图中,第8条规则执行后,再执行它的跳转——第6条规则,第6条规则执行后,又去执行它的跳转第5条规则,第5条规则没有跳转,执行后就回到第8条规则,而不是停留在第5条规则,之后第8条规则结束,执行第9条规则。
qldsrx 2014-12-09
  • 打赏
  • 举报
回复
问题在哪里?
pengwu666 2014-12-09
  • 打赏
  • 举报
回复
输出次数应该是没问题的。但是要加跳转的就搞不懂了。
pengwu666 2014-12-09
  • 打赏
  • 举报
回复
在来一张,复杂点的。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧