当前位置: 首页 > 工具软件 > ROMA > 使用案例 >

Henu ACM Round#21 A Roma and Lucky Numbers

秦跃
2023-12-01
A. Roma and Lucky Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Roma (a popular Russian name that means 'Roman') loves the Little Lvov Elephant's lucky numbers.

Let us remind you that lucky numbers are positive integers whose decimal representation only contains lucky digits 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

Roma's got n positive integers. He wonders, how many of those integers have not more than k lucky digits? Help him, write the program that solves the problem.

Input

The first line contains two integers nk (1 ≤ n, k ≤ 100). The second line contains n integers ai (1 ≤ ai ≤ 109) — the numbers that Roma has.

The numbers in the lines are separated by single spaces.

Output

In a single line print a single integer — the answer to the problem.

Examples
input
3 4
1 2 4
output
3
input
3 2
447 44 77
output
2
Note

In the first sample all numbers contain at most four lucky digits, so the answer is 3.

In the second sample number 447 doesn't fit in, as it contains more than two lucky digits. All other numbers are fine, so the answer is 2.


感受:做cf的题一定要看懂题意,这虽说是个水题,看懂题意都花了我不少时间,不然搜个题解,我都看不懂啥意思

题意:给你n个数,和数字k,让你判断n个数中有几个数包含的4和7的总个数(也就是出现的总次数)不超过k的

(样例一我看了很久才明白为什么输出是3,卡在那因为,规定每个数中包含4和7的总数不能超过4,我感觉1 2 4 中含有4或7的只有4一个,那输出应该是1才是

后来突然顿悟,只要一个数中包含4和7的总个数不超过4都是对的,自然也包括0,4和7出现的总次数 0,1,2,3,4都可以)

解法:统计这n个数中每个数中,4和7出现的总次数ans即可,再与k比较,如果小于等于k,个数num+1

不废话了,上代码。


方法一:用整数存储,循环%10,取出每一位数判断是不是4或7,统计次数

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n,k,num=0;
		n=scanner.nextInt();
		k=scanner.nextInt();
		while((n--) >0){
			int ans=0;
			int x=scanner.nextInt();
			while(x>0){    //统计4和7的个数
				int c=x%10;
				if(c==4||c==7){ 
					ans++; 
				}
				x/=10;
			}
			if(ans<=k)
				num++;
		}
		scanner.close();
		System.out.println(num);
	}
}


方法二:存储成字符数组,直接统计,判断每个数中每一位是不是4或7,统计次数

(因为java从键盘接收的只能是字符串,不能直接接收字符串,所以把每个数先用字符串保存起来,再转换成字符;如果是c或c++,直接就可以存在字符数组中)

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n, k, cnt = 0,len=0;
		n = scanner.nextInt();
		k = scanner.nextInt();
		char[][] num = new char[100][12];
		for (int i = 0; i < n; i++) {
			String str = scanner.next();
			len = str.length();
			for(int j=0;j<len;j++){
				num[i][j] = str.charAt(j);
			}
		}
		for(int i=0;i<n;i++){
			len = num[i].length;
			int ans = 0;
			for(int j=0;j<len;j++){
				if(num[i][j]=='4' ||num[i][j]=='7'){
					ans++;
				}
			}
			if(ans<=k)
				cnt++;
		}	
		System.out.println(cnt);
	}
}



 类似资料:

相关阅读

相关文章

相关问答