JAVA小问题

2024-12-04 20:12:39
推荐回答(2个)
回答(1):

import java.util.*;

class Test
{
public static void main(String[] args)
{
System.out.println(test("345666533"));
}

public static int test(String str) {
int[] nums = new int[str.length()];
for(int i=0; i for(int j=0; j if(str.charAt(i) == str.charAt(j))
nums[i] ++;
}
}

int max = nums[0];
List list = new ArrayList();
for(int i=1; i if(max < nums[i])
max = nums[i];
else if(max == nums[i])
list.add(Integer.parseInt(str.substring(i, i+1)));
}

int val = list.get(0);
for(int i=1; i if(val < list.get(i))
val = list.get(i);
}

return val;
}
}

回答(2):

import java.util.*;
import java.io.*;

public class qcishu
{
private int max=0,mnum;
int cishu(int num)
{
String s=String.valueOf(num);
char c,k;
c=s.charAt(0);
int count=1,i=1;
while(i {if(s.charAt(i)==c)count++;
i++;
}
String str=s.replace(String.valueOf(c),"");
if(count>max){max=count;mnum=Integer.parseInt(String.valueOf(c));}
else if(count==max)
{if(Integer.parseInt(String.valueOf(c))>mnum)mnum=Integer.parseInt(String.valueOf(c));}
if(str.length()>=count)return cishu(Integer.parseInt(str));
else return mnum;
}

public static void main(String[]args)
{
System.out.print("请输入数字(不超过10位数):");
Scanner input = new Scanner(System.in);
int num=input.nextInt();
qcishu my =new qcishu();
System.out.println("返回数为:"+my.cishu(num));}
}