日期:2014-05-18  浏览次数:20772 次

求一个小算法
s[]={ "0 ", "1 ", "4 ", "10 ", "12 "};

得结果
1100100000101

结果的数组长度是13位
s数组里的值在结果数组中的位置的值都是1,其余都为0

------解决方案--------------------
不懂
------解决方案--------------------
不懂


你是不是经常不结帖? 性欲很低啊
------解决方案--------------------
你那段2进制是怎么得的?看不懂
------解决方案--------------------
不好意思啊,偶理解能比较差劲,hoho
------解决方案--------------------
这样应该可以

public class Temp {
public static void main( String[] args ){
String s[] = { "0 ", "1 ", "4 ", "10 ", "12 "};
int result[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int sInt[] = strToInt(s);
for(int j = 0; j < sInt.length; j++){
result[ sInt[j] ] = 1;
}
print(result);
}

private static int[] strToInt(String s[]){
int re[] = new int[ s.length ];
for(int i = 0; i < s.length; i++){
re[i] = Integer.parseInt( s[i] );
}
return re;
}

private static void print(int[] data){
for( int i = 0; i < data.length; i++ ){
System.out.print(data[i]);
}
System.out.println();
}
}
------解决方案--------------------
如果结果数组的长度根据s[]中的最大数据所得的话。那就要看你的s[]里面的数据是不是需要排序了,如果时排好的从小到大。那就取最后一个数(最大的)+ 1 作为结果数组的长度,并且全部预置零就行了

刚发现的问题,没办法,我理解力不太好
------解决方案--------------------
麻利儿的结贴。。。