直接上代码,出现次数最多的数,提交显示 运行错误
字串统计,提交直接显示错误。
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分,不知道哪错