有N个数,例如3351353558,求出最多的那个数?
解决方案 »
- 怎么显示英文格式的上下午
- 初学java;请问:我用eclipse,已经安装了java3d的包,但引用java3d的程序却是叉,不知是什么问题,请高手帮忙,谢谢!!
- 一个初级问题
- 都不好意思,求一道作业的代码
- 10-30的数字,怎么用正则表达式匹配
- sun one studio 是什么东东?在JAVA。SUN。COM上找不到下载。是不是就是NetBeans IDE ?
- 一个简单的聊天程序,请指点错在哪里了???
- 这个application又错哪了???
- 为什么我的浏览器里看不到选择论坛的列表,也不能在浏览帖子的时候双击自动下移了?
- s木板拼出的最大矩形的面积
- 教授布置的一个JAVA小项目.请老鸟给点建议.
- 初学java,不知道怎么引用?
public static void main(String args[]){
String s = "3351353558";
findMost(s);
}
public static void findMost(String s){
int count = 0;
int most = count;
char num = '0';
for(int i = 0; i < s.length(); i++){
for(int j = i; j < s.length(); j++){
if(s.charAt(i) == s.charAt(j))
count++;
}
if(count > most){
most = count;
num = s.charAt(i);
}
count = 0;
}
System.out.println(num);
}
}
{ /**
* @param args
*/
public static void main(String[] args)
{
String str = "12455547464515475354635"; int[] x = new int[10];
char[] ca = str.toCharArray();
for (int i = 0; i < ca.length; i++)
{
x[ca[i] - '0']++;
} for (int i = 0; i < x.length; i++)
{
System.out.println("字符" + (char) ('0' + i) + "出现了" + x[i] + "次");
} }}
{ /**
* @param args
*/
public static void main(String[] args)
{
String str = "12455547464515475354635"; int[] x = new int[10];
char[] ca = str.toCharArray();
for (int i = 0; i < ca.length; i++)
{
x[ca[i] - '0']++;
} for (int i = 0; i < 10; i++)
{
System.out.println("字符" + (char) ('0' + i) + "出现了" + x[i] + "次");
} }}
public static void main(String args[]) throws Exception{
String str="3351353558";
System.out.println("出现次数最的数字是:"+maxTimesNum(str)); }
public static int maxTimesNum(String str){
if(!str.matches("\\d+")){
new RuntimeException("格式不对");
}
char maxChar=0;
int[] times=new int[10];
int maxIndex=0;
for(char c: str.toCharArray()){
times[c-'0']++;
}
for(int i=0;i<times.length;i++){
if(times[i]>times[maxIndex]) maxIndex=i;
}
return maxIndex;
}
}
public class T {
public static void main(String[] args) {
int[] data = { 3, 3, 5, 20, 3, 5, 3, 5, 5, 8 };
int size = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] > size) {
size = data[i];
}
} int[] counts = new int[size + 1];
for (int i = 0; i < data.length; i++) {
counts[data[i]] += 1;
} int max = 0;
for (int i = 0; i < counts.length; i++) {
if (counts[i] > max) {
max = counts[i];
}
} System.out.println("出现次数最多的有:");
for (int i = 0; i < counts.length; i++) {
if (counts[i] == max) {
System.out.println(i);
}
}
}
}
public static void main(String[] args) {
int[] data = { 3, 3, 5, 1, 3, 5, 3, 5, 5, 8 };
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for (int i = 0; i < data.length; i++) {
if (m.get(data[i]) == null) {
m.put(data[i], 1);
} else {
m.put(data[i], m.get(data[i]) + 1);
}
}
System.out.println("Map里的元素: " + m); int t = 0;
Set<Map.Entry<Integer, Integer>> set = m.entrySet();
for (Entry<Integer, Integer> entry : set) {
if (entry.getValue() > t) {
t = entry.getValue();
}
} System.out.println("次数最多的有:");
for (Entry<Integer, Integer> entry : set) {
if (entry.getValue() == t) {
System.out.println(entry.getKey());
}
}
}
}
String str = "12455547464515475354635";
String out = ""; char[] ca = str.toCharArray(); Arrays.sort(ca); for (char c : ca) {
out += c;
} Matcher m = Pattern.compile("(\\d)\\1*").matcher(out); while (m.find())
System.out.println(m.group().charAt(0) + "的个数:"
+ m.group().length());
}
public class To {
private static String s=JOptionPane.showInputDialog("Enter a number : ");
private static char []a=new char[10];
private static int []b=new int[a.length];
public static void main(String[] args){
for(int i=0;i<10;i++)
a[i]=(char)(i+'0');
for(int i=0;i<s.length();i++)
for(int j=0;j<10;j++){
if(s.charAt(i)==a[j]){
b[j]++;
break;
}
}
int max=b[0],k=0;
for(int i=1;i<b.length;i++)
if(b[i]>max){
max=b[i];
k=i;
}
System.out.println("The number "+k+" occurs "+max+" times ,is the most.");
}
}此代码只可以解决N位数中没有出现次数相同的数字;如果存在,以最先出现的为最多。
如:
输入 112233
出现最多的数字为:1 而 2 和 3 不是。
二楼的方法真的很简单。String s = 3351353558;char getCh() {
int max = 0;
char ch;for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int ntimes = s.length() - s.replaceAll(Character.toString(c), "").length();
if (ntimes > max) {
max = ntimes;
ch = c;
}
}
return ch;
}
private static String s = JOptionPane.showInputDialog("Enter a Integer number: "); public static char getCh(String s) {
int max = 0;
char ch=' '; for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int ntimes = s.length() - s.replaceAll(Character.toString(c), "").length();
if (ntimes > max) {
max = ntimes;
ch = c;
}
}
return ch;
}
public static void main(String[] args){
System.out.println("The number is : "+getCh(s));
}
}楼上的方法确实不错! 不过有个问题没解决: 输入 112233 出现最多的数字为:1 ,而 2 和 3 不是。能不能 再考虑一下?
max = ntimes;
list.clear();
list.add(c);
} else if (ntimes == max) {
list.add(c);
}
public class Test
{
public static void main(String[] args){
String s = "ddfdshdddddddnfsbsasxcdhssssdndsfddsjkd";
char[] c = s.toCharArray();
int count = 0;char ch = c[0];
for(int i = 0;i<c.length-1;i++){
int n=0;
if(c[i]=='\u0000') continue;
for(int j=i+1;j<c.length;j++){
if(c[j]=='\u0000') continue;
if((c[j]^c[i])==0){
n++;
c[j]='\u0000';
}
}
if(n>count){
count =n;
ch=c[i];
}
}
System.out.println(count+"\t"+ch);
}
}