算法求教!

hecong875 2009-11-27 03:33:38
算法求教!
假如:
开始货位 1-1-1-1
结束货位 3-3-3-3

生成货位有:
1-1-1-1
1-1-1-2
1-1-1-3
1-1-2-1
1-1-2-2
1-1-2-3
1-1-3-1
1-1-3-2
1-1-3-3
。。。。
2-1-2-1
。。。。
3-3-3-1
3-3-3-2
3-3-3-3

具体要求是这样的

首先
1-1-1-1 我们把每一位看做是一个输入框 他可以输入 数字(0-9)或者字母(A-Z)每一个输入框内最大是5位

说下自己的思路 和 具体代码 我现在做的 分析 + 组织 + 插入数据库 6W条 需要40分钟!
感觉有点慢 想提高效率! 欢迎大家指点!最好有事例代码!
...全文
359 48 打赏 收藏 转发到动态 举报
写回复
用AI写文章
48 条回复
切换为时间正序
请发表友善的回复…
发表回复
hecong875 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 46 楼 lcl_data 的回复:]
引用 44 楼 hecong875 的回复:
引用 43 楼 lcl_data 的回复:

从代码来看是有问题的 ! 但是实际上去执行的话 !  首先 26个字母 + (0-9) 10个数字都在里面定义了! 这样就不会有问题了!。。。。。

异常情况是一定要考虑的吧
[/Quote]

你所指的异常是什么情况呢? 首先在页面已经做过很多限制了!
比如 只能输入数字或者字母(不能同时存在)
开始货位 比结束货位小

而且这里也只是一个 转义的过程! 并不是主逻辑!

其实还有问题是 现在里面的数字类型是 int 也就是int32 在生成大量的货位的时候 会有异常! 应该改为int64
群龙 2009-11-30
  • 打赏
  • 举报
回复
生猛!
十八道胡同 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 44 楼 hecong875 的回复:]
引用 43 楼 lcl_data 的回复:

从代码来看是有问题的 ! 但是实际上去执行的话 !  首先 26个字母 + (0-9) 10个数字都在里面定义了! 这样就不会有问题了!。。。。。
[/Quote]
异常情况是一定要考虑的吧
tommir3 2009-11-30
  • 打赏
  • 举报
回复
没看明白,记录下以后再研究看吧
hecong875 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 43 楼 lcl_data 的回复:]
private int ConvertToIntByValue(string value)
        {
            for (int i = 0; Strarray.Length > i; i++)
            {
                if (Strarray[i] == value)
                {
                    return i;
                }
            }
            return 0;
        }

你的这段代码有问题吧,当Strarray[0] == value的时候和没找到 return 0;都返回0??
[/Quote]

从代码来看是有问题的 ! 但是实际上去执行的话 ! 首先 26个字母 + (0-9) 10个数字都在里面定义了! 这样就不会有问题了!。。。。。
十八道胡同 2009-11-29
  • 打赏
  • 举报
回复
private int ConvertToIntByValue(string value)
{
for (int i = 0; Strarray.Length > i; i++)
{
if (Strarray[i] == value)
{
return i;
}
}
return 0;
}

你的这段代码有问题吧,当Strarray[0] == value的时候和没找到 return 0;都返回0??
hecong875 2009-11-29
  • 打赏
  • 举报
回复
顶起
zcl24 2009-11-27
  • 打赏
  • 举报
回复
没写完 可以参考

using System;
using System.Collections.Generic;
using System.Text;

namespace Test.Test
{
class Test
{ //{
// if (Start.Length != End.Length)
// throw new Exception("Warn start string or end string.");
// //List<String> slist = new List<String>();
// //for (int i=0;i<Start.Length;i++)
// //{
// // StringBuilder sb = new StringBuilder();
// // if (char.IsDigit(Start[i]))
// // {
// // int end = int.Parse(End[i]);

// // for (int a = 0; a < 9; a++)
// // {
// // if (a > end)
// // break;
// // sb.Append(a.ToString());
// // }
// // }
// // else
// // {
// // int end = End[i];
// // for (int a = 0; a < 26; a++)
// // {
// // char c = Start[i] + a;
// // if (c > end)
// // break;
// // sb.Append(c);
// // }
// // }
// // slist.Add(sb.ToString());
// //}
// //List<String> result = new List<string>();

// //foreach (String s in slist)
// //{
// // foreach (char c in s)
// // {

// // }
// //}

// return null;
//}



public interface NoItem
{
/// <summary>
/// 获取所有级
/// </summary>
/// <returns></returns>
String[] GetAllItems();
}

public class NoDigitItem : NoItem
{
public NoDigitItem(int start, int end)
{
Start = start;
End = end;
}
public int Start;
public int End;
#region NoItem Members

public string[] GetAllItems()
{
List<String> result = new List<string>();

for (int i = Start; i <= End; i++)
{
result.Add(i.ToString());
}
return result.ToArray();
}

#endregion
}

public class NoStringItem : NoItem
{
public NoStringItem(String start, String end)
{
Start = start;
End = end;
}
public String Start;
public String End;

#region NoItem Members
private int Getnn(int n)
{
switch (n)
{
case 0:
return 1;
case 1:
return 100;
case 2:
return 10000;
case 3:
return 1000000;
}
return 0;
}
public string[] GetAllItems()
{
long tempStrat = 0;
int n = 0;
for (int i = Start.Length - 1; i >= 0; i--)
{
tempStrat += (Start[i]) * Getnn(n);
n++;
}
long tempEnd = 0;
n = 0;
for (int i = End.Length - 1; i >= 0; i--)
{
tempEnd += (End[i]) * Getnn(n);
n++;
}
int s = 0;
List<String> result = new List<string>();
for (long t = tempStrat; t <= tempEnd; t++)
{
StringBuilder sb = new StringBuilder();
String temps = t.ToString();
for (s = 0; s < temps.Length; s++)
{
int tempc = int.Parse(temps.Substring(s, 2));
if (tempc < 'A' | tempc > 'Z')
break;
sb.Append((char)(tempc));
s++;
}
String rs = sb.ToString();
if (rs.Length == Start.Length)
result.Add(rs);
}
return result.ToArray();
}

#endregion


}

public class NoString
{
public List<NoItem> NoItems = new List<NoItem>();

/// <summary>
/// 获取所有编号
/// </summary>
/// <returns></returns>
public String[] GetAllNos()
{
List<String[]> itList = new List<string[]>();
foreach (NoItem n in NoItems)
{
//foreach (string s in n.GetAllItems())
// Console.WriteLine(s);
itList.Add(n.GetAllItems());
}
//这里穷举itList列表
return null;
}
}


public void DoTest()
{
NoString ns = new NoString();
ns.NoItems.Add(new NoStringItem("ABC", "DCA"));
ns.NoItems.Add(new NoDigitItem(1, 9));
ns.NoItems.Add(new NoStringItem("CCC", "DDD"));
ns.NoItems.Add(new NoDigitItem(23, 54));
ns.GetAllNos();
}
}
}

zcl24 2009-11-27
  • 打赏
  • 举报
回复
在内存中算好 再写入到数据库
abaochan 2009-11-27
  • 打赏
  • 举报
回复
容存入剪贴板
liherun 2009-11-27
  • 打赏
  • 举报
回复
明天来研究研究
hecong875 2009-11-27
  • 打赏
  • 举报
回复
大概就是这样的 ! 两个实体是在前台 根据用户输入组织的! 然后传到SERVICE

望高手指点
hecong875 2009-11-27
  • 打赏
  • 举报
回复

public string[] Strarray;
/// <summary>
/// 根据货位模板生成货位
/// </summary>
/// <returns></returns>
public string CreatedLocation(CreatedLocationinfo CreateLocation,LocationInfo Location)
{
ILocationDA locationDA = DataAccess.CreateLocationDA();
DataBase dataBase = new DataBase();
DbConnection conn = dataBase.connection;
conn.Open();
DbTransaction tran =conn.BeginTransaction();
try
{
//创建数组
Strarray = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "D", "H", "I", "G", "K", "M", "L", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
List<string> listValue = new List<string>();
List<int> listLength = CreateLocation.LocationValueLength;
string StrStartValue = CreateLocation.LoactionStarteValue;
string StrEndValue = CreateLocation.LoactionEndValue;
int iStartVlaue = 0;
int iEndValue = 0;
int iCount = 0;
iCount = CreateLocation.LocationLength;
iStartVlaue = ConvertToIntByValue(StrStartValue[iCount - 1].ToString());
iEndValue = ConvertToIntByValue(StrEndValue[iCount - 1].ToString());
for (int i = iStartVlaue; iEndValue >= i; i++) //横向坐标 个位
{
listValue = CreateFunction(iCount - 2, StrStartValue, StrEndValue);
for (int w = 0; listValue.Count > w; w++)
{
listValue[w] = listValue[w].Replace("@_@", ConvertToStringByIndex(i));

if (!string.IsNullOrEmpty(CreateLocation.SeperatorChar))
{
int index = 0;
for (int n = 0; listLength.Count > n; n++)
{
if (listLength[n] > 0)
{
listValue[w] = listValue[w].Insert(listLength[n] + index, CreateLocation.SeperatorChar);
index += listLength[n] + 1;
}
else
{
listValue[w] = listValue[w].Substring(0, listValue[w].Length - 1);
break;
}
}
}

LocationInfo locationEntity = Location;
locationEntity.Loc = listValue[w];
//判断locationID是否已存在
if (locationDA.CheckLocationIDUnique(dataBase, tran, locationEntity.Loc, dataBaseID[0]))
{
throw new Exception("货位ID'" + locationEntity.Loc + "'已经存在! 请删除后重新生成!");
}

string[] strLoc = locationEntity.Loc.Split(CreateLocation.SeperatorChar.ToCharArray());

for (int t = 0; strLoc.Length > t; t++)
{
switch (t)
{
case 0:
locationEntity.TemplateField1 = strLoc[t];
break;
case 1:
locationEntity.TemplateField2 = strLoc[t];
break;
case 2:
locationEntity.TemplateField3 = strLoc[t];
break;
case 3:
locationEntity.TemplateField4 = strLoc[t];
break;
case 4:
locationEntity.TemplateField5 = strLoc[t];
break;
}
}
locationEntity.UpdatedBy = locationEntity.CreatedBy;
locationDA.InsertLocation(dataBase, tran, locationEntity, dataBaseID[0]); //插入这里有问题!

}
}
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
CommonErrorHandler.ErrorHandler(ex, LogType.Error);
}
finally
{
conn.Close();
}
return ""; //返回 空 代表成功
}

/// <summary>
/// 创建模板货位
/// </summary>
/// <param name="length">字符长度</param>
/// <param name="StrStartValue">开始字符</param>
/// <param name="StrEndValue">结束字符</param>
/// <returns></returns>
private List<string> CreateFunction(int length, string StrStartValue, string StrEndValue)
{
List<string> listValue = new List<string>();
int iStartvlaue = 0;
int iEndvalue = 0;
iStartvlaue = ConvertToIntByValue(StrStartValue[length].ToString());
iEndvalue = ConvertToIntByValue(StrEndValue[length].ToString());
for (int n = iStartvlaue; iEndvalue >= n; n++) //纵向坐标 十位
{
if (length > 0)
{
List<string> listValue1 = new List<string>();
listValue1 = CreateFunction(length - 1, StrStartValue, StrEndValue);
for (int i = 0; listValue1.Count > i; i++)
{
listValue.Add(listValue1[i].Replace("@_@", ConvertToStringByIndex(n)) + "@_@");
}
}
else
{
listValue.Add(ConvertToStringByIndex(n) + "@_@");
}
}

return listValue;
}
/// <summary>
/// 吧值 根据数组对比 转换成index
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private int ConvertToIntByValue(string value)
{
for (int i = 0; Strarray.Length > i; i++)
{
if (Strarray[i] == value)
{
return i;
}
}
return 0;
}
/// <summary>
/// 传入index 去数组中查找对应的值
/// </summary>
/// <param name="Index"></param>
/// <returns></returns>
private string ConvertToStringByIndex(int Index)
{
return Strarray[Index].ToString();
}

tanlianguang 2009-11-27
  • 打赏
  • 举报
回复
up
liherun 2009-11-27
  • 打赏
  • 举报
回复
能分享下你的生成货位的代码吗
liherun 2009-11-27
  • 打赏
  • 举报
回复
挺牛的
liherun 2009-11-27
  • 打赏
  • 举报
回复
sqlBulkCopy
hecong875 2009-11-27
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 lcl_data 的回复:]
引用 28 楼 hecong875 的回复:
最终测试!  我现在的方法没有问题 6W条数据 也才

生成开始时间2009/10/2 16:27:37
生成结束时间2009/10/2 16:28:11

最后朋友告诉我是因为 数据库插入的问题! 我现在是 生成一条插入一条! 他建议批量插入!

可不可以分享下你的插入代码
[/Quote]
是要生成的还是插入的?
插入System.Data.SqlClient.SqlBulkCopy 具体的我也还在看! 呵呵 !

藍風鈴 2009-11-27
  • 打赏
  • 举报
回复

<html>
<title>算法</title>
</head>
<body>
<input type="text" id="a" maxlength="5" size="5" value="1" />-
<input type="text" id="b" maxlength="5" size="5" value="1" />-
<input type="text" id="c" maxlength="5" size="5" value="1" />-
<input type="text" id="d" maxlength="5" size="5" value="1" /><br />
<input type="text" id="e" maxlength="5" size="5" value="3" />-
<input type="text" id="f" maxlength="5" size="5" value="3" />-
<input type="text" id="g" maxlength="5" size="5" value="3" />-
<input type="text" id="h" maxlength="5" size="5" value="3" /><br />
<input type="button" id="bu1" value="计算" onclick="javascript:printit();">
<input type="button" id="bu2" value="测试" onclick="javascript:printtest();">
<div id="d1"></div>
<script type="text/javascript">
function printit(){
var a,b,c,d;
var ina=document.getElementById('a').value;
var inb=document.getElementById('b').value;
var inc=document.getElementById('c').value;
var ind=document.getElementById('d').value;
var ine=document.getElementById('e').value;
var inf=document.getElementById('f').value;
var ing=document.getElementById('g').value;
var inh=document.getElementById('h').value;
var a1=ina.length;
var b1=inb.length;
var c1=inc.length;
var d1=ind.length;
document.getElementById('d1').innerHTML='';
for(a=ina.charCodeAt(0);a<=ine.charCodeAt(0);a++){
for(b=inb.charCodeAt(0);b<=inf.charCodeAt(0);b++){
for(c=inc.charCodeAt(0);c<=ing.charCodeAt(0);c++){
for(d=ind.charCodeAt(0);d<=inh.charCodeAt(0);d++){
for(a2=0;a2<a1;a2++)document.getElementById('d1').innerHTML+=(String.fromCharCode(a));
document.getElementById('d1').innerHTML+=('-');
for(b2=0;b2<b1;b2++)document.getElementById('d1').innerHTML+=(String.fromCharCode(b));
document.getElementById('d1').innerHTML+=('-');
for(c2=0;c2<c1;c2++)document.getElementById('d1').innerHTML+=(String.fromCharCode(c));
document.getElementById('d1').innerHTML+=('-');
for(d2=0;d2<d1;d2++)document.getElementById('d1').innerHTML+=(String.fromCharCode(d));
document.getElementById('d1').innerHTML+=('<br />');
}
}
}
}
}
function printtest(){
alert(String.fromCharCode(97));
alert("a".charCodeAt(0));
}
</script>
</body>
</html>


我已经头晕了。。。
十八道胡同 2009-11-27
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 hecong875 的回复:]
最终测试!  我现在的方法没有问题 6W条数据 也才

生成开始时间2009/10/2 16:27:37
生成结束时间2009/10/2 16:28:11

最后朋友告诉我是因为 数据库插入的问题! 我现在是 生成一条插入一条! 他建议批量插入!
[/Quote]
可不可以分享下你的插入代码
加载更多回复(28)

110,533

社区成员

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

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

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