C#完成完小数学题的编程,运算符存为变量
C#完成小学数学题的编程,运算符存为变量?
9 8 7 6 5 4 3 2 1=90在各个数字中间添加+,-运算符使得等式成立。
这里的算法是不是穷举就可以了,关键是程序实现时,用什么变量来表达这些+,-符号?又如何循环一一列举出来呢?
求指点!
------解决思路----------------------
这个如何填充和如何输出,两者不冲突啊,如何填充就是算法,至于填充的值如何显示,你可以用枚举来对应加减乘除,当然也可以用struct或者class,然后重写string方法,比如
interface IOperate{
int Operate(int a,int b);
}
class Add:IOperate
{
//Operate方法实现
public override string ToString()
{return "+";
}
}
------解决思路----------------------
给你写了一个打印“=9”运算符成立的demo:
------解决思路----------------------
让我想起首页上这题,就是怎么放隔板,思路应该是一样的
http://bbs.****.net/topics/391836433
+ -如果没括号还好办
可以用Split拆分然后从左到右进行运算
------解决思路----------------------
每一个数字之间都要添加+或者-号。实际上考卷上一定会写 9( )8( )7( )6( )5( )4( )3( )2( )1=9,有括号,而不是仅仅留空。
如果要使用一次性的查询语句而得到输出字符串,可以修改一下:
------解决思路----------------------
总共能找出20种解
9 8 7 6 5 4 3 2 1=90在各个数字中间添加+,-运算符使得等式成立。
这里的算法是不是穷举就可以了,关键是程序实现时,用什么变量来表达这些+,-符号?又如何循环一一列举出来呢?
求指点!
------解决思路----------------------
这个如何填充和如何输出,两者不冲突啊,如何填充就是算法,至于填充的值如何显示,你可以用枚举来对应加减乘除,当然也可以用struct或者class,然后重写string方法,比如
interface IOperate{
int Operate(int a,int b);
}
class Add:IOperate
{
//Operate方法实现
public override string ToString()
{return "+";
}
}
------解决思路----------------------
给你写了一个打印“=9”运算符成立的demo:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var l = 枚举数字列表(9).ToList();
var k = l.Select(x => x.Sum()).ToList();
var query = from x in 枚举数字列表(9)
where x.Sum() == 9
select x;
foreach (var r in query)
{
var firstN = true;
foreach (var n in r)
{
if (n > 0 && !firstN)
Console.Write("+");
Console.Write(n);
if (firstN)
firstN = false;
}
Console.WriteLine("={0}", r.Sum());
}
}
private static IEnumerable<IEnumerable<int>> 枚举数字列表(int max)
{
if (max < 1)
yield return new int[0];
else if (max <= 9)
{
var x = 枚举数字列表(max - 1);
foreach (var r in x)
{
yield return new int[] { max }.Concat(r);
yield return new int[] { -max }.Concat(r);
}
}
}
}
}
------解决思路----------------------
让我想起首页上这题,就是怎么放隔板,思路应该是一样的
http://bbs.****.net/topics/391836433
+ -如果没括号还好办
可以用Split拆分然后从左到右进行运算
------解决思路----------------------
每一个数字之间都要添加+或者-号。实际上考卷上一定会写 9( )8( )7( )6( )5( )4( )3( )2( )1=9,有括号,而不是仅仅留空。
如果要使用一次性的查询语句而得到输出字符串,可以修改一下:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var query = from x in 枚举数字列表(9)
where x.Sum() == 9
let 字符列表 = x.Select(y => (y > 0 ? "+" : "") + y)
let str = string.Join("", 字符列表)
let str2 = str.StartsWith("+") ? str.Substring(1) : str
select str2;
foreach (var r in query)
Console.WriteLine("{0}=9", r);
}
private static IEnumerable<IEnumerable<int>> 枚举数字列表(int max)
{
if (max < 1)
yield return new int[0];
else
{
var x = 枚举数字列表(max - 1);
foreach (var r in x)
{
yield return new int[] { max }.Concat(r);
yield return new int[] { -max }.Concat(r);
}
}
}
}
}
------解决思路----------------------
总共能找出20种解
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
foreach (var item in splitX(0, 987654321, ""))
{
if (item.Value == 90)
Console.WriteLine(item.Expr);
}
}
struct Result
{
public int Value;
public string Expr;
}
static IEnumerable<int[]> split2(int num)
{
int x = 10;
while (true)
{
int numLeft = num / x;
if (numLeft == 0)
yield break;
int numRight = num % x;
yield return new int[] { numLeft, numRight };
x *= 10;
}
}
static IEnumerable<Result> splitX(int numLeft, int numRight, string expr)
{
yield return new Result
{
Value = numLeft + numRight,
Expr = string.Format("{0}+{1}={2}", expr, numRight, numLeft + numRight),
};
yield return new Result
{
Value = numLeft - numRight,
Expr = string.Format("{0}-{1}={2}", expr, numRight, numLeft - numRight),
};
foreach (var arr in split2(numRight))
{
foreach (var item in splitX(numLeft + arr[0], arr[1], string.Format("{0}+{1}", expr, arr[0])))
yield return item;
foreach (var item in splitX(numLeft - arr[0], arr[1], string.Format("{0}-{1}", expr, arr[0])))
yield return item;
}
}
}
}