移除静态字段后,如何在递归求和组合程序中追踪可行解存在性
问题描述
需要编写递归程序,在1到N(N≥2)的数字间插入+、-运算符,找出所有能得到目标值X的表达式组合;无可行解时输出N/A。现有C#代码通过静态字段counter追踪是否存在可行解,但触发JRM003错误(禁止使用静态字段)。示例输入n=6、x=3时需输出对应3条表达式,需调整counter的实现方式,既消除错误又实现追踪功能。
示例输入
- n=6
- x=3
示例输出
- 1 + 2 + 3 - 4 - 5 + 6 = 3
- 1 + 2 - 3 + 4 + 5 - 6 = 3
- 1 - 2 - 3 - 4 + 5 + 6 = 3
现有代码
using System; namespace ConsoleApp1 { class Program { static bool counter; static void Generate(int n, int x, int currentIndex, int result, string expression) { counter = true; if (currentIndex == n + 1) { if (result == x) { Console.WriteLine(expression + " = " + x); } return; } Generate(n, x, currentIndex + 1, result + currentIndex, expression + " + " + currentIndex); Generate(n, x, currentIndex + 1, result - currentIndex, expression + " - " + currentIndex); } static void Main() { int n = Convert.ToInt32(Console.ReadLine()); int x = Convert.ToInt32(Console.ReadLine()); const int doi = 2; Generate(n, x, doi, 1, "1"); if (!counter) { Console.WriteLine("N/A"); } Console.ReadLine(); } } }
错误信息
JRM003 (错误) : 禁止使用静态字段。(行: 7, 字符: 7)
解决方案
以下几种方法可替换静态字段counter,避开静态字段限制的同时实现可行解追踪:
方法1:使用引用类型传递状态
利用引用类型(如长度为1的bool数组)在递归中传递状态,因为引用类型的修改会作用到外部实例:
using System; namespace ConsoleApp1 { class Program { static void Generate(int n, int x, int currentIndex, int result, string expression, bool[] hasSolution) { if (currentIndex == n + 1) { if (result == x) { Console.WriteLine(expression + " = " + x); hasSolution[0] = true; // 找到可行解时标记状态 } return; } Generate(n, x, currentIndex + 1, result + currentIndex, expression + " + " + currentIndex, hasSolution); Generate(n, x, currentIndex + 1, result - currentIndex, expression + " - " + currentIndex, hasSolution); } static void Main() { int n = Convert.ToInt32(Console.ReadLine()); int x = Convert.ToInt32(Console.ReadLine()); const int doi = 2; bool[] hasSolution = { false }; // 初始化状态为未找到解 Generate(n, x, doi, 1, "1", hasSolution); if (!hasSolution[0]) { Console.WriteLine("N/A"); } Console.ReadLine(); } } }
方法2:让递归方法返回bool值汇总结果
递归方法返回当前分支是否找到可行解,主方法汇总所有分支的结果:
using System; namespace ConsoleApp1 { class Program { static bool Generate(int n, int x, int currentIndex, int result, string expression) { bool found = false; if (currentIndex == n + 1) { if (result == x) { Console.WriteLine(expression + " = " + x); found = true; } return found; } // 只要任意分支找到解,就标记为true found |= Generate(n, x, currentIndex + 1, result + currentIndex, expression + " + " + currentIndex); found |= Generate(n, x, currentIndex + 1, result - currentIndex, expression + " - " + currentIndex); return found; } static void Main() { int n = Convert.ToInt32(Console.ReadLine()); int x = Convert.ToInt32(Console.ReadLine()); const int doi = 2; bool hasSolution = Generate(n, x, doi, 1, "1"); if (!hasSolution) { Console.WriteLine("N/A"); } Console.ReadLine(); } } }
方法3:使用类的实例字段
将Program类的方法改为非静态,用实例字段替代静态字段:
using System; namespace ConsoleApp1 { class Program { private bool _hasSolution; void Generate(int n, int x, int currentIndex, int result, string expression) { if (currentIndex == n + 1) { if (result == x) { Console.WriteLine(expression + " = " + x); _hasSolution = true; } return; } Generate(n, x, currentIndex + 1, result + currentIndex, expression + " + " + currentIndex); Generate(n, x, currentIndex + 1, result - currentIndex, expression + " - " + currentIndex); } static void Main() { int n = Convert.ToInt32(Console.ReadLine()); int x = Convert.ToInt32(Console.ReadLine()); const int doi = 2; Program program = new Program(); program.Generate(n, x, doi, 1, "1"); if (!program._hasSolution) { Console.WriteLine("N/A"); } Console.ReadLine(); } } }
内容的提问来源于stack exchange,提问作者Raluca123
相关产品推荐
相关产品推荐

