asp.net求3位不同数字的组合数
程序员文章站
2022-05-09 08:47:51
简单的: 复制代码 代码如下:public partial class _default : system.web.ui....
简单的:
public partial class _default : system.web.ui.page
{
string m1 = "";
protected void page_load(object sender, eventargs e)
{
string n = "123";
string m = "";
zuhe(n);
for (int j = 2; j > -1; j--)
{
m = m + n[j];
}
zuhe(m);
}
private void zuhe(string ssel)
{
for (int i = 0; i < ssel.length; i++)
{
m1 = ssel + ssel.substring(i, 1);
response.write(m1.remove(i,1) + "<br>");
}
}
}
csdn的(xiaoshen1127 ):winform版
using system;
using system.collections.generic;
namespace zuheshu
{
class program
{
static void main(string[] args)
{
program p = new program();
console.writeline("******************************组合数***********************************");
console.writeline("请任意输入一个整数:");
string num = console.readline();
try {
long.parse(num);
list<string> als = new list<string>();
als = p.getdata(string.empty, num.trim(), als);
p.display(als);
console.writeline("\ny-enter:继续");
if (console.readline().trim().tolower() == "y")
{
console.clear();
main(args);
}
}
catch {
console.clear();
console.writeline("请输入数字!");
main(args);
}
}
private list<string> getdata(string strbase, string strsel, list<string> alret)
{
if (strsel.length == 1)
{
string temp=strbase + strsel;
if (!alret.contains(temp)&&!temp.startswith("0"))
{
alret.add(temp);
}
}
else
{
for (int i = 0; i < strsel.length; i++)
{
getdata(strbase + strsel.substring(i, 1), strsel.remove(i, 1), alret);
}
}
return alret;
}
public void display(list<string> dals)
{
int i = 1;
int a = dals.count;
if (a == 0)
{
console.writeline("该数没有其他组合情况");
}
else
{
console.writeline("不同的组合数共有" + a + "个,如下");
foreach (string number in dals)
{
console.write(number + "\t");
if (i++ % 5 == 0)
{
console.writeline();
}
}
}
}
}
}
复制代码 代码如下:
public partial class _default : system.web.ui.page
{
string m1 = "";
protected void page_load(object sender, eventargs e)
{
string n = "123";
string m = "";
zuhe(n);
for (int j = 2; j > -1; j--)
{
m = m + n[j];
}
zuhe(m);
}
private void zuhe(string ssel)
{
for (int i = 0; i < ssel.length; i++)
{
m1 = ssel + ssel.substring(i, 1);
response.write(m1.remove(i,1) + "<br>");
}
}
}
csdn的(xiaoshen1127 ):winform版
复制代码 代码如下:
using system;
using system.collections.generic;
namespace zuheshu
{
class program
{
static void main(string[] args)
{
program p = new program();
console.writeline("******************************组合数***********************************");
console.writeline("请任意输入一个整数:");
string num = console.readline();
try {
long.parse(num);
list<string> als = new list<string>();
als = p.getdata(string.empty, num.trim(), als);
p.display(als);
console.writeline("\ny-enter:继续");
if (console.readline().trim().tolower() == "y")
{
console.clear();
main(args);
}
}
catch {
console.clear();
console.writeline("请输入数字!");
main(args);
}
}
private list<string> getdata(string strbase, string strsel, list<string> alret)
{
if (strsel.length == 1)
{
string temp=strbase + strsel;
if (!alret.contains(temp)&&!temp.startswith("0"))
{
alret.add(temp);
}
}
else
{
for (int i = 0; i < strsel.length; i++)
{
getdata(strbase + strsel.substring(i, 1), strsel.remove(i, 1), alret);
}
}
return alret;
}
public void display(list<string> dals)
{
int i = 1;
int a = dals.count;
if (a == 0)
{
console.writeline("该数没有其他组合情况");
}
else
{
console.writeline("不同的组合数共有" + a + "个,如下");
foreach (string number in dals)
{
console.write(number + "\t");
if (i++ % 5 == 0)
{
console.writeline();
}
}
}
}
}
}