有个字符串数组,字符串内只有数字和“.”,比如String[] str={".15", "7..", "402", "..3"},代表有4个字符串,每个元素长度都为 3,现在将数组里面元素进行随机组合成字符串(打乱顺序),
比如将元素按"..3.154027.."的顺序相连。然后截取其中连续的片段"154027"。连续的数字的总和为1+5+4+0+2+7=19。这个19是所有组合里面得到的最大的值。Java怎么实现啊。

解决方案 »

  1.   

    先把.21323 点开头的都匹配出来,找出 和最大的
    比如.12 跟.34 取点.34 如果相等,两个都取放入list中
    在取32.以点结尾的,求出最大值,放入list中
    在取开放性的。求出最大值
    接着就是连接字符串了
    实现技术:
    用正则表达式
    匹配第一种"^\\.+\\d+$"
    匹配第二种"^\\d+\\.+$"
    匹配第三种"^\\d+$"
    突然发现还有一种情况
    就是这种3.4
    这种情况另外找,或者插入到上面去找
    匹配规则"[\\d][\\.][\\d]",匹配出来后,分别和上面考虑
    代码实现:
    楼主看你了
      

  2.   

    第三情况不能这么考虑
    如果有 直接放入list中,不用比较。
      

  3.   


    public static void main(String[] args) {
    String[] strs = { ".15", "7..", "402", "..3" };// 字符串数组,你可以随意组合
    String str = ""; // 把上面的数组拼成一个字符串
    for (String string : strs) {
    str += string;
    }
    System.out.println(str); // 这里可以把字符串输出来看看 // 上面都是废话!
    String[] temp = str.split("\\."); // 去掉所有的.
    int max = 0; // 最大值变量
    for (String string : temp) {
    if (string.length() != 0) {
    int sum = sum(string); // 把所有连续的片段传过去计算和
    max = sum > max ? sum : max; // 与当前最大值比较
    }
    }
    System.out.println(max);
    } static int sum(String num) {
    String[] nums = num.split(""); // 把连续的片段分隔成一个个单一的数字
    int sum = 0;
    for (String string : nums) {
    if (string.length() != 0) {
    int i = Integer.parseInt(string);
    sum += i; //累加后返回和
    }
    }
    return sum;
    }
      

  4.   

    一次字符串扫描就出来了吧:
    public static void main(String args[]) {
    String strs[]={".15", "7..", "402", "..3"};
    List<String> list=Arrays.asList(strs);
    StringBuilder sb=new StringBuilder();
    int max=Integer.MIN_VALUE,index=-1,length=0;
    int tempSum=0,tempL=0; //辅助变量

    Collections.shuffle(list);

    for(String s:strs)
    sb.append(s);

    System.out.println("打乱顺序后生成的字符串:"+sb);

    sb.append('.');

    for(int i=0;i<sb.length();i++) {
    char ch=sb.charAt(i);

    if(ch>0x2f&&ch<0x3a) { //累加相连的数字
    tempSum+=ch-0x30;

    tempL++;
    } else { //对'.'的处理
    if(tempSum>max) {
    index=i-(length=tempL);
    max=tempSum;
    }
    tempSum=0;
    tempL=0;
    }
    }

    System.out.println("和最大的子串:"+sb.substring(index,index+length));
    System.out.println("最大的和:"+max);
    }
      

  5.   


    public static void main(String[] args) {
    String[] strs = { ".15", "7..", "402", "..3" };// 字符串数组,你可以随意组合
    String str = ""; // 把上面的数组拼成一个字符串
    for (String string : strs) {
    str += string;
    }
    System.out.println(str); // 这里可以把字符串输出来看看 // 上面都是废话!
    String[] temp = str.split("\\."); // 去掉所有的.
    int max = 0; // 最大值变量
    for (String string : temp) {
    if (string.length() != 0) {
    int sum = sum(string); // 把所有连续的片段传过去计算和
    max = sum > max ? sum : max; // 与当前最大值比较
    }
    }
    System.out.println(max);
    } static int sum(String num) {
    String[] nums = num.split(""); // 把连续的片段分隔成一个个单一的数字
    int sum = 0;
    for (String string : nums) {
    if (string.length() != 0) {
    int i = Integer.parseInt(string);
    sum += i; //累加后返回和
    }
    }
    return sum;
    }
      

  6.   

     String[] temp = str.split("\\."); // 去掉所有的.
    “\\.”是什么意思啊,怎么用双斜杠?
      

  7.   


    split方法接受的是一个正则表达式,正则表达式的特殊符号都要转义,如 \ | . ( ) [ ] ^ $ - 等。
      

  8.   

    这需求明显的是对字符的运算,而不是对串的,感觉用字符串扫描要好的多哈。split处理串运算时比较方便,但处理单个字符时,不仅速度慢,还要生成个多余的String[]。时间上和空间上都不划算哈。Java的split还是用正则做的,还要对正则的运算符作转义……
      

  9.   

    语言都不一样了好不好。使用转义字符的意义就是避免出现二义性,二义性是所有编程语言都不允许的。我只能说C#对这类操作进行了处理,有些符号就可以不再转义了。也有可能是它重写了split方法,在你看不到的地方进行转义了,只是你看不到而已。
      

  10.   


    import java.util.Random;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test {
    public static String[] random(String[] strs){
    int index = 0;
    for(int i = 0;i<strs.length;i++){
    index =Math.abs(new Random().nextInt()%strs.length);
    String temp = strs[i];
    strs[i] = strs[index];
    strs[index] = temp;
    }
    return strs;
    }
    public static void main(String[] args) {
    String[] str={".15", "7..", "402", "..3"};
    Test.random(str);
    StringBuffer sb = new StringBuffer();
    for(String s:str){
    sb.append(s);
    }
    String temp = sb.toString();
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher(temp);
    int sum = 0;
    int Maxsum = 0;
    String maxSumStr ="";
    System.out.println(sb);
    while(m.find()){
    String s = m.group();
    for(int i=0;i<s.length();i++){
    sum+=Integer.parseInt(String.valueOf(s.charAt(i)));
    }
    if(sum>Maxsum){
    Maxsum = sum;
    maxSumStr = s;
    sum = 0;
    }
    }
    System.out.println("最大值:"+Maxsum);
    System.out.println("最大子串"+maxSumStr);
    }
    }
      

  11.   

    index =Math.abs(new Random().nextInt()%strs.length);
    这句话什么意思?
      

  12.   

    可不可以先随机组合字符串,然后再将重组的字符窜放入list中,去掉重复的后,在对这些list中的元素进行计算。
      

  13.   


    import java.util.Random;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test {
    public static String[] random(String[] strs){
    int index = 0;
    for(int i = 0;i<strs.length;i++){
    index =Math.abs(new Random().nextInt()%strs.length);
    String temp = strs[i];
    strs[i] = strs[index];
    strs[index] = temp;
    }
    return strs;
    }
    public static void main(String[] args) {
    String[] str={".15", "7..", "402", "..3",".6.","9.8"};
    Test.random(str);
    StringBuffer sb = new StringBuffer();
    for(String s:str){
    sb.append(s);
    }
    String temp = sb.toString();
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher(temp);
    int sum = 0;
    int Maxsum = 0;
    String maxSumStr ="";
    System.out.println(sb);
    while(m.find()){
    String s = m.group(0);
    for(int i=0;i<s.length();i++){
    sum+=Integer.parseInt(String.valueOf((s.charAt(i))));
    }
    if(sum > Maxsum){
    Maxsum = sum;
    maxSumStr = s;
    sum = 0;
    }
    else{
    sum=0;//刚才漏掉这句。
    }
    }
    System.out.println("最大值:"+Maxsum);
    System.out.println("最大子串"+maxSumStr);
    }
    }
      

  14.   

    这个……  不是这原因啦。
    那是因为C#的split根本不是用正则表达式做的。C#里的函数基本上是继承STL的,处理字符串一般都是做字符串扫描。
      

  15.   

    这个意思就是 在strs.length-1数以内的随即数字。包括0.然后交换顺序,随即打乱。
    要去重复?我这个不重复啊。不知道要表达什么,那里重复了。
      

  16.   


    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;public class Test3 { String[] strArray = { ".15", "7..", "402", "..3" }; public String RandomCombination() {
    String str = "";
    List<String> list = Arrays.asList(strArray);
    Collections.shuffle(list);
    for (String s : list) {
    str += s;
    }
    System.out.println("str = " + str);
    System.out.println("=============================");
    return str;
    } public void printResult() {
    String str = RandomCombination();
    String[] strArray = str.split("\\.");
    int maxValue = 0;
    for (String s : strArray) {
    if (s != null && s.length() > 1) {
    System.out.print("str = " + s);
    int num = compute(s);
    if (num > maxValue) {
    maxValue = num;
    }
    System.out.println("   value  = " + num);
    }
    }
    System.out.println("========= MAX VALUE IS ===========");
    System.out.println("   maxValue  = " + maxValue);
    } public int compute(String str) {
    int num = 0;
    for (int i = 0, j = str.length(); i < j; i++) {
    num += Integer.parseInt(String.valueOf(str.charAt(i)));
    }
    return num;
    } public static void main(String[] args) {
    Test3 test = new Test3();
    test.printResult();
    }
    }
      

  17.   

    0 ,明白你的交换意思了。我之前用了Random会出现重复的情况。然后就有后面的去重复了。
      

  18.   

    估计你差不多是说这个意思,因为你是随即从里面取,你要是随即取你就把你取到的remove掉,在取就不会重复了,不然不好控制,会多出很多循环。这种是直接交换的,不重复。
      

  19.   

            List<String> list = Arrays.asList(strArray);
            Collections.shuffle(list);
    这样就可以打乱了,各位英雄。
      

  20.   

    还有一个就是,我循环让得到很多maxValue,然后我再进行比较。不就可以得到最大的值了,只要把循环次数弄多点
      

  21.   


    真不知道你要很多maxValue做什么,我觉得26楼写得已经够好了,唯一需要修改的地方就是在printResult方法的for循环里,s.length() >= 1 。
    str = 402.15..37..
    =============================
    str = 402   value  = 6
    str = 15   value  = 6
    str = 37   value  = 10
    ========= MAX VALUE IS ===========
       maxValue  = 10这样还没有满足你的需求吗?
      

  22.   

    其实我在38楼打了很多字的 ,但现在不想说了。
    CSDN回帖机制还有待加强啊!
      

  23.   

    还有一个就是,我循环让得到很多maxValue,然后我再进行比较。不就可以得到最大的值了,只要把循环次数弄多点。这个行不
      

  24.   

    我试了半天,没反应。我把它放到list里面了,循环完后再来比较,没反应
      

  25.   

    讨论了这么久,其实题目本质上是个排列组合求最大值的问题,跟随机没啥关系。但我认为,就这个题目而言,根本无需这么复杂的去组合它,从需求而言,其实可以考虑一次循环就搞定了。这个题目中的最大数实际上基本是 ..xxx + yyy + zzz.. 这种组合方式,计算最大值的时候也是按照单个数字相加的最大值,所以也不需要去考虑拼接方式差异所导致数值大小不一样。所以在一次循环之内,得到最大的 yyy,xxx,zzz,循环结束后,3个值相加,这题目也就算结束了。
      

  26.   

    public class SingleMaxSum {    public static void main(String[] args) {
            String[] str = { ".15", "7..", "402", "..3", "302", "32044...", "...234" };
            int maxLeft = 0, posLeft = -1, maxRight = 0, posRight = -1, middle = 0; // 记录中间计算信息
            StringBuffer sb = new StringBuffer(); // 记录找到的顺序
            // 一次循环遍历即可结束任务        
            for (int i = 0; i < str.length; i++) {
                String s = str[i];
                int tmp = sum(s);
                if (isNumber(s.charAt(0))) {
                    // 左边第一个是数字
                    if (isNumber(s.charAt(s.length() - 1))) {
                        // 说明整个都是数字,可以直接拼接入middle
                        sb.append(s);
                        middle += tmp;
                    } else {
                        // 可以作为right进行拼接
                        if (tmp > maxRight) {
                            maxRight = tmp;
                            posRight = i;
                        }
                    }
                } else if (isNumber(s.charAt(s.length() - 1))) {
                    // 右边第一个是数字,可以作为left进行拼接
                    if (tmp > maxLeft) {
                        maxLeft = tmp;
                        posLeft = i;
                    }
                }
            }
            if (posLeft >= 0) sb.insert(0, str[posLeft]); // 拼左边
            if (posRight >= 0) sb.append(str[posRight]); // 拼右边
            System.out.println("Line: " + sb + "\t\tSum: " + (maxLeft + middle + maxRight));
        }    public static boolean isNumber(char c) {
            return c >= '0' && c <= '9';
        }    public static int sum(String s) {
            int sum = 0;
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c >= '0' && c <= '9')
                    sum += (c - '0');
            }
            return sum;
        }
    }