Henu ACM Round#21 A Roma and Lucky Numbers

解决一个编程问题,通过统计一组正整数中每个数包含的幸运数字4和7的数量是否不超过给定阈值k,来找出符合条件的数的个数。
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);
	}
}



【源码免费下载链接】:https://renmaiwang.cn/s/qqeui ### Python通过Matplotlib生成复合饼图在数据分析与可视化领域,图表是一种强大的工具,能够帮助我们更好地理解数据、发现模式并做出决策。其中,饼图因其直观性而在展示部分与整体的关系时尤为常见。然而,在某些情况下,单一的饼图可能无法完全展现复杂的数据结构或层级关系。这时,复合饼图(也称为嵌套饼图或多层饼图)就派上了用场。#### 复合饼图的概念复合饼图是一种特殊的饼图形式,它将一个或多个较小的饼图嵌入到一个较大的饼图中,以此来表示数据的不同层次或类别。这种图表非常适合用来展示多层次的数据结构,比如市场份额中的细分市场占比等。#### Matplotlib简介Matplotlib是一个用于Python的2D绘图库,它可以生成各种类型的图表,包括线图、柱状图、散点图、饼图等。由于其高度的自定义能力和灵活性,Matplotlib成为了数据科学家和工程师们进行数据可视化的主要工具之一。#### 使用Matplotlib绘制复合饼图下面我们将详细介绍如何使用Matplotlib库在Python中生成复合饼图。1. **导入必要的库** ```python import matplotlib.pyplot as plt from matplotlib.patches import ConnectionPatch ```2. **创建画布和子图** 在复合饼图中,通常会有一个主饼图和一个或多个子饼图。因此,我们需要创建一个包含这些子图的画布。 ```python fig = plt.figure(figsize=(9, 5.0625)) ax1 = fig.add_subplot(121) # 主饼图所在的子图 ax2 = fig.add_subplot(122) # 子饼图所在的子图 fig.
【源码免费下载链接】:https://renmaiwang.cn/s/u0npk 在Java程序设计语言中,将字符串内的字符按字母顺序重新排列是一种常见的操作,在处理文本数据或需要对字母顺序进行排序的场景中尤为常见。本节内容将详细讲解如何实现这一功能。由于Java字符串结构固定,无法对其进行后续更改,因此在对字符串中的字符进行排序时必须采取特殊方式。为达到排序目的,我们需要理解以下关键点:首先,Java字符串是不可变对象,默认由`String`类创建;其次,在这种类型下无法直接修改原有内容,因此实现字符排序需要通过构造新的字符串对象来完成。 具体步骤如下: 1. **转换字符数组**:利用`toCharArray()`方法将原始字符串转换为可操作的字符数组。 2. **排序字符数组**:调用`Arrays.sort()`方法对上述生成的字符数组进行排序,默认按Unicode值排列,对于a-z范围内的字母顺序与Unicode排序一致。 3. **构造新字符串**:通过`new String(charArray)`或`String.valueOf(charArray)`将已排序的字符数组转换为新的字符串对象。 以下示例代码展示了这一操作的具体实现: ```javaimport java.util.Arrays;public class Zifuchuan { public static void main(String[] args) { String originalStr = "java 字符串a-z排序"; // 步骤1:将字符串转换为字符数组 char[] chars = originalStr.toCharArray(); // 步骤2:对字符数组进行排序 Arrays.sort(chars); // 步骤3:构建新的排序后的字符串 String sortedStr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值