「已注销」 2014-12-12 13:47 采纳率: 100%
浏览 2155

蓝桥杯 出现次数最多的 数字串统计 错误

直接上代码,出现次数最多的数,提交显示 运行错误

字串统计,提交直接显示错误。

public class Main

public static void main(String[] args)
{
    java.util.Scanner cin = new java.util.Scanner(System.in);
    int   n     = cin.nextInt();
    int[] array = new int[n];
    int[] count = new int[n];
    boolean flag = false;
    int   index = 0;
    int   num   = 0;
    for(int i = 0;i<n;i++)
    {
        flag = false;

        num = cin.nextInt();

        for(int j = 0;j<i;j++)
        {
            if(num == array[j])
            {
                flag = true;
                count[j]++;
                break;
            }
        }
        if(!flag)
        {
            array[index++] = num;
        }
    }

    int max = 0;
    for(int i = 1;i<index;i++)
    {
        if(count[i]>count[max])
            max = i;
    }
    System.out.print(array[max]);

}

}

字串统计的

public class Main
{
public static void main(String[] args)
{
java.util.Scanner cin = new java.util.Scanner(System.in);
int L = cin.nextInt();
String s = cin.next();
String temp = "";
int first = 0;
int last = first + L;
int length = s.length();

    java.util.Map<String,Integer>  map = new  java.util.TreeMap<String,Integer>();

    while(length>=L)
    {
        first = 0;
        last = length;
        while(last<=s.length())
        {
            temp = s.substring(first,last);
            if(map.containsKey(temp))
            {
                Integer tempCount = map.get(temp);
                tempCount++;
                map.put(temp,tempCount);
            }
            else
                map.put(temp,1);
            first++;
            last++;
        }
        length--;
    }

    java.util.Collection<Integer> setmap = map.values();
    java.util.Set<String> setstr = map.keySet();

    java.util.Iterator it = setmap.iterator();
    java.util.Iterator its = setstr.iterator();

    int max = (int)it.next();
    String str = its.next().toString();
    String tempstr = str;
    int tempMax = max;

    while(it.hasNext())
    {
        tempMax = (int)it.next();
        tempstr = its.next().toString();
        if(tempMax>max)
        {
            max = tempMax;
            str = tempstr;
        }

    }
    System.out.println(str);

}

}

纠结啊,本机运行都没错,提交就是不对,全是80分,不知道哪错

  • 写回答

1条回答

  • devmiao 2014-12-12 16:34
    关注

    简单看了下你的代码,没有使用合理的算法,都是用的穷举,在线程序会用比较复杂的测试用例,算法不合理运行会超过时间和内存限制而不通过。你还需要完善下你的程序。

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!