题目描述
定义一个方法static int ndigit(int n,int k),用来判断某个整数n中数字k(0~9)的个数。
编写主方法,其中输入一个整数m,后面跟m个整数,然后调用方法ndigit()方法来找出这m个整数的幸运数(所谓幸运数是指其中数字8最多的数),将找到的幸运数在独立的行中输出出来。如果有多个幸运数,则输出其中的第一个幸运数,如果所有的数中都没有数字8,则输出NO并换行。
输入样例
5
128 326 78898 5888 971
输出样例
78898
程序代码
import java.util.Scanner;
public class Main {
static int ndigit(int n, int k){
int count = 0;
while(n > 0){
int one = n % 10;
if (one == k){
count++;
}
n /= 10;
}
return count;
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int t = kb.nextInt();
int max = 0;
int show = 0;
while(t > 0){
int num = kb.nextInt();
if(ndigit(num, 8) > max){
max = ndigit(num, 8);
show = num;
}
t--;
}
if(max == 0){
System.out.println("NO");
}else{
System.out.println(show);
}
}
}