80分求一个高效率算法,要高手解决!

luohuayh 2009-12-02 04:43:36
现有两个数组AA[][];BB[][];如下定义:
AA[][]:
AA[0]=货品编号1;
AA[1]=货品编号2;
....
....
AA[i]=货品编号i;

AA[0][0]=数量1;
AA[1][1]=数量2;
.....
AA[j][j]=数量i

BB[][]:

BB[0]=货品编号1;
BB[1]=货品编号2;
....
....
BB[i]=货品编号j;

BB[0][0]=数量1;
BB[1][1]=数量2;
.....
BB[j][j]=数量j;
先要求把两个数组中货品编号相同的货品数量相加,并把他和其他不同的货品信息组成一个新的数组CC[][];
要求:算法要高效率。
...全文
371 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
安特矮油 2009-12-11
  • 打赏
  • 举报
回复
先用二分发排序,然后用一个中间变量,只需要对所有商品进行一次遍历就OK 开始时吧A[0]赋值给中间变量,然后用C[0]记录下A[0] C[0][0]记录下A[0][0] 用A[1]跟中间变量比较是否相同 不同就吧A[1]赋值给中间变量 然后用C[1]和C[1][1]记录 如此循环下去就OK了 只是建议 不知道效率如何
qq348852348aa 2009-12-10
  • 打赏
  • 举报
回复
太难了都,我们不是神仙
yao0220 2009-12-10
  • 打赏
  • 举报
回复
public static String[][] find(String[][] c,String[][] d,int area){
//m为编码未在c数组出现时存储的位置
//k为编码在c数组里出现时的位置
int k = -1, m = -1;
String bh = "";
for(int i=0,num=c.length;i<num;i++){
bh = c[i][0];
if(bh==null){//表明该位置无数据,传入参数看覆盖此位置数据
m = i;
i = num;
}
if(bh!=null&&bh.equals(d[area][0])){//表名传入编码已存在
k = i;
i = num;
}
}
if(k!=-1)//将同编码的数量相加,覆盖源数据
c[k][1] = (Integer.parseInt(c[k][1])+Integer.parseInt(d[area][1]))+"";
else if(m!=-1){//将空数据用传入数据覆盖
c[m][0] = d[area][0];
c[m][1] = d[area][1];
}
return c;
}

public static void main(String[] args){
//生成测试数据,获得a和b两个数组
String[][] a = new String[10][2];
String[][] b = new String[8][2];
String[] aa = {"3","6","1","7","9","8","2","3","6","5"};
String[] bb = {"7","8","2","1","4","5","8","9"};
for(int i=0;i<10;i++){
a[i][0] = "bianhao"+aa[i];
a[i][1] = (int)Math.floor(Math.random() * 100)+"";
}
for(int i=0;i<8;i++){
b[i][0] = "bianhao"+bb[i];
b[i][1] = (int)Math.floor(Math.random() * 100)+"";
}
//显示a和b两个数组的数据,供结果查看
for(int i=0,num=a.length;i<num;i++){
System.out.print(a[i][0]+":"+a[i][1]+"__");
}
System.out.println("");
for(int i=0,num=b.length;i<num;i++){
System.out.print(b[i][0]+":"+b[i][1]+"__");
}
System.out.println("");
//此处对数据进行处理
String[][] c = new String[a.length+b.length][2];
for(int i=0,num=a.length;i<num;i++){
c = Test3.find(c, a, i);
}
for(int i=0,num=b.length;i<num;i++){
c = Test3.find(c, b, i);
}
//显示最好合并后的数据
for(int i=0,num=c.length;i<num;i++){
if(c[i][0]!=null)
System.out.println(c[i][0]+":"+c[i][1]);
}
}
luohuayh 2009-12-07
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 cgh45 的回复:]
建议使用HashMap,如果非要用数组的话,可以这样:
public static String[][] getNewArray(String[][] a,String [][] b){
List <Map> tmpList=new ArrayList();
List <Map> bIdList=new ArrayList();
for(int i=0; i <b.length; i++){
Map tmpMap=new HashMap();
tmpMap.put("id",b[i][0]);
tmpMap.put("count",b[i][1]);
bIdList.add(tmpMap);
}
for(int i=0; i <a.length; i++){
int j=0;
Boolean coFlag=false;
while(j <bIdList.size()){
Map bMap=new HashMap();
bMap =(HashMap)bIdList.get(j);
if(a[i][0].equals((String)bMap.get("id"))){
int tmpInt=Integer.parseInt(a[i][1])+Integer.parseInt((String)bMap.get("count"));
Map tmpMap=new HashMap();
tmpMap.put("id",a[i][0]);
tmpMap.put("count",String.valueOf(tmpInt));
tmpList.add(tmpMap);
bIdList.remove(j);
coFlag=true;
break;
}else{
j++;
}
}
if(coFlag) continue;
Map tmpMap=new HashMap();
tmpMap.put("id",a[i][0]);
tmpMap.put("count",a[i][1]);
tmpList.add(tmpMap);
}
for(int i=0; i <bIdList.size(); i++){
tmpList.add(bIdList.get(i));
}
String[][] c=new String[tmpList.size()][2];
System.out.println("tmpList.size()2="+tmpList.size());
try{
for(int i=0; i <tmpList.size(); i++){
Map cMap=new HashMap();
cMap=(HashMap)tmpList.get(i);
c[i][0]=(String)cMap.get("id");
c[i][1]=(String)cMap.get("count");
}
}catch(Exception ex){
System.out.println(ex.toString());
}
return c;
}
[/Quote]
代码效率高吗?
luohuayh 2009-12-03
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 lingar02 的回复:]
而且数组应该定义为
AA[0][0]=货品编号1;
AA[0][1]=数量1;
AA[1][0]=货品编号2;
AA[1][1]=数量2;
这样才是正确的吧

先把问题描述清晰


[/Quote]
请看#7的问题描述
lingar02 2009-12-03
  • 打赏
  • 举报
回复
而且数组应该定义为
AA[0][0]=货品编号1;
AA[0][1]=数量1;
AA[1][0]=货品编号2;
AA[1][1]=数量2;
这样才是正确的吧

先把问题描述清晰

lingar02 2009-12-03
  • 打赏
  • 举报
回复
首先确认一下
一个货品编号 对应的 数量是一个 还是不确定个?
shan1119 2009-12-03
  • 打赏
  • 举报
回复
insert into db then use group by
luohuayh 2009-12-03
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 zhoupuyue 的回复:]
Java codepublicstaticvoid main(String[] args) {
String[][] a= {
{"货品编号1","数量20","数量15","数量18"},
{"货品编号2","数量48","数量30","数量27"}
};
String[][] b= {
{"货品编号1","数量40","数?-
[/Quote]

嵌套两个for循环,执行次数是i!次,这种算法我自己之前就写了一个,但是效率实在在低了,如果数据量庞大的话,那将是一个很痛苦的等待过程
liuym200212 2009-12-03
  • 打赏
  • 举报
回复
要效率,建议用HashMap
阿_布 2009-12-03
  • 打赏
  • 举报
回复
13L的算法效率不好,8L,11L的方法应该好一些。
shuangyuwen 2009-12-03
  • 打赏
  • 举报
回复
sss
xuexijava 2009-12-03
  • 打赏
  • 举报
回复
mark up
阿_布 2009-12-03
  • 打赏
  • 举报
回复

public static void main(String[] args) {
String[][] a = {
{"货品编号1","数量20","数量15","数量18"},
{"货品编号2","数量48","数量30","数量27"}
};
String[][] b = {
{"货品编号1","数量40","数量25","数量36"},
{"货品编号2","数量20","数量17","数量73","数量14","数量22"}
};
int length = a.length>b.length?a.length:b.length;
String[][] c = new String[length][];
for(int i=0;i<length;i++){
c[i] = new String[i<a.length?(i<b.length?Math.max(b[i].length, a[i].length):a[i].length):b[i].length];
for(int j=0;j<c[i].length;j++){
c[i][j] = j==0?"货品编号"+(i+1):j<a[i].length?j<b[i].length?"数量"+(Integer.parseInt(a[i][j].substring(2))+Integer.parseInt(b[i][j].substring(2))):"数量"+(Integer.parseInt(a[i][j].substring(2))):"数量"+(Integer.parseInt(b[i][j].substring(2)));
}
}
for(int n=0;n<c.length;n++){
for(int m=0;m<c[n].length;m++){
System.out.print(c[n][m]+",");
}
System.out.println();
}
}
luohuayh 2009-12-03
  • 打赏
  • 举报
回复
没人写么?
cgh45 2009-12-03
  • 打赏
  • 举报
回复
建议使用HashMap,如果非要用数组的话,可以这样:
public static String[][] getNewArray(String[][] a,String [][] b){
List<Map> tmpList=new ArrayList();
List<Map> bIdList=new ArrayList();
for(int i=0; i<b.length; i++){
Map tmpMap=new HashMap();
tmpMap.put("id",b[i][0]);
tmpMap.put("count",b[i][1]);
bIdList.add(tmpMap);
}
for(int i=0; i<a.length; i++){
int j=0;
Boolean coFlag=false;
while(j<bIdList.size()){
Map bMap=new HashMap();
bMap =(HashMap)bIdList.get(j);
if(a[i][0].equals((String)bMap.get("id"))){
int tmpInt=Integer.parseInt(a[i][1])+Integer.parseInt((String)bMap.get("count"));
Map tmpMap=new HashMap();
tmpMap.put("id",a[i][0]);
tmpMap.put("count",String.valueOf(tmpInt));
tmpList.add(tmpMap);
bIdList.remove(j);
coFlag=true;
break;
}else{
j++;
}
}
if(coFlag) continue;
Map tmpMap=new HashMap();
tmpMap.put("id",a[i][0]);
tmpMap.put("count",a[i][1]);
tmpList.add(tmpMap);
}
for(int i=0; i<bIdList.size(); i++){
tmpList.add(bIdList.get(i));
}
String[][] c=new String[tmpList.size()][2];
System.out.println("tmpList.size()2="+tmpList.size());
try{
for(int i=0; i<tmpList.size(); i++){
Map cMap=new HashMap();
cMap=(HashMap)tmpList.get(i);
c[i][0]=(String)cMap.get("id");
c[i][1]=(String)cMap.get("count");
}
}catch(Exception ex){
System.out.println(ex.toString());
}
return c;
}
robyjeffding 2009-12-03
  • 打赏
  • 举报
回复
路过,学习了
liuym200212 2009-12-03
  • 打赏
  • 举报
回复
楼主,用HashMap效率非常好,而且还是产品越多,越能体现出优势
把两个数组中产品都加到HashMap中就行了
把产品累加的示例:

HashMap<String, Integer> name = new HashMap<String, Integer>();

int oldNum = 0;

try
{
oldNum = name.get("No3");
}
catch (Exception e)
{
// hashMap中没有此产品
}

int newNum = oldNum + 3;
name.put("No1", newNum);
guoquanhua007 2009-12-03
  • 打赏
  • 举报
回复
要效率的话请使用
Map
speed9 2009-12-03
  • 打赏
  • 举报
回复
算法多了,想简单点都难。
加载更多回复(12)
掌握数据结构和算法,可称为算法工程师!这是成为架构师的基础,有Google算法大神亲授。我保证你可以写出时空复杂度都很优的架构。有人专门答疑哟!你想要成为架构工程师吗?立即点击报名按钮吧!北上广容不下肉身,三四线放不下灵魂,程序员里没有穷人,有一种土豪叫 算法工程师。程序 = 数据结构 + 算法程序是为了解决实际问题而存在的。然而为了解决问题,必定会使用到某些数据结构以及设计一个解决这种数据结构的算法。如果说各种编程语言是程序员的招式,那么数据结构和算法就相当于程序员的内功。编程实战算法,不是念PPT,我们讲的就是实战与代码实现与企业应用。程序 = 数据结构 + 算法           ——图灵奖得主,计算机科学家N.Wirth(沃斯)作为程序员,我们做机器学习也好,做python开发也好,java开发也好。有一种对所有程序员无一例外的刚需 —— 算法与数据结构日常增删改查 + 粘贴复制 + 搜索引擎可以实现很多东西。同样,这样也是没有任何竞争力的。我们只可以粘贴复制相似度极高的功能,稍复杂的逻辑没有任何办法。语言有很多,开发框架更是日新月异3个月不学就落后我们可以学习很多语言,很多框架,但招聘不会考你用5种语言10种框架实现同一个功能。真正让程序员有区分度,企业招聘万年不变的重点 —— 算法与数据结构。算法代表程序员水平的珠穆朗玛。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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