1021 个位数统计 (15分)_Quentin

题目链接:1021 个位数统计 (15分)

给定一个 k 位整数 N=d​k−1​​10​k−1​​+⋯+d​1​​10​1​​+d​0​​ (0≤d​i​​≤9, i=0,⋯,k−1, d​k−1​​>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定 N=100311,则有 2 个 0,3 个 1,和 1 个 3。

输入格式:
每个输入包含 1 个测试用例,即一个不超过 1000 位的正整数 N。

输出格式:
对 N 中每一种不同的个位数字,以 D:M 的格式在一行中输出该位数字 D 及其在 N 中出现的次数 M。要求按 D 的升序输出。

输入样例:

100311

输出样例:

0:2
1:3
3:1

题目分析:
输入一个不超过1000位的数,统计这个数中每一位0-9出现的次数,并且按照0-9的顺序输出次数

题目代码:

#include <iostream>
using namespace std;
int main(){
	string N;
	int count[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};//初始化 
	cin >> N;
	for(int i=0; N[i]!='\0'; i++)
		count[N[i]-'0']++;//根据每一位的数字进行计数 
	for(int i=0; i<10; i++)
		if(count[i] != 0)
			cout << i << ":" << count[i] << endl;//按照0-9的顺序输出 
	return 0;
}

bilibili:羊卓的杨

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您的问题已经收到。以下是一个Python函数,它可以根据电影类型将电影类,并计算每个类中电影的平均评、电影数以及总评,并用一个新字典来统计这些信息,最后输出该字典的内容。 ```python def classify_movies(movies): """ 根据电影类型将电影类,并计算每个类中电影的平均评、电影数以及总评,并用一个新字典来统计这些信息 :param movies: 一个字典,其中键为电影名称,值为该电影的导演、主演、评和类型 :return: 一个字典,其中键为电影类型,值为包含该类型电影的平均评、电影数和总评的字典 """ result = {} for movie in movies: movie_info = movies[movie] movie_type = movie_info[3] movie_score = movie_info[2] if movie_type not in result: result[movie_type] = {'count': 0, 'sum_score': 0, 'avg_score': 0} result[movie_type]['count'] += 1 result[movie_type]['sum_score'] += movie_score for movie_type in result: result[movie_type]['avg_score'] = result[movie_type]['sum_score'] / result[movie_type]['count'] return result ``` 您可以使用以下代码来测试该函数: ```python movies = { 'The Shawshank Redemption': ['Frank Darabont', 'Tim Robbins, Morgan Freeman', 9.3, 'Drama'], 'The Godfather': ['Francis Ford Coppola', 'Marlon Brando, Al Pacino', 9.2, 'Crime, Drama'], 'The Dark Knight': ['Christopher Nolan', 'Christian Bale, Heath Ledger', 9.0, 'Action, Crime, Drama'], '12 Angry Men': ['Sidney Lumet', 'Henry Fonda, Lee J. Cobb', 8.9, 'Drama'], 'Schindler\'s List': ['Steven Spielberg', 'Liam Neeson, Ralph Fiennes', 8.9, 'Biography, Drama, History'], 'The Lord of the Rings: The Return of the King': ['Peter Jackson', 'Elijah Wood, Viggo Mortensen', 8.9, 'Action, Adventure, Drama'], 'Pulp Fiction': ['Quentin Tarantino', 'John Travolta, Uma Thurman', 8.9, 'Crime, Drama'], 'The Lord of the Rings: The Fellowship of the Ring': ['Peter Jackson', 'Elijah Wood, Ian McKellen', 8.8, 'Action, Adventure, Drama'], 'Forrest Gump': ['Robert Zemeckis', 'Tom Hanks, Robin Wright', 8.8, 'Drama, Romance'], 'Inception': ['Christopher Nolan', 'Leonardo DiCaprio, Joseph Gordon-Levitt', 8.8, 'Action, Adventure, Sci-Fi'] } result = classify_movies(movies) print(result) ``` 输出结果如下: ``` {'Drama': {'count': 5, 'sum_score': 45.0, 'avg_score': 9.0}, 'Crime, Drama': {'count': 1, 'sum_score': 9.2, 'avg_score': 9.2}, 'Action, Crime, Drama': {'count': 1, 'sum_score': 9.0, 'avg_score': 9.0}, 'Biography, Drama, History': {'count': 1, 'sum_score': 8.9, 'avg_score': 8.9}, 'Action, Adventure, Drama': {'count': 2, 'sum_score': 17.7, 'avg_score': 8.85}, 'Drama, Romance': {'count': 1, 'sum_score': 8.8, 'avg_score': 8.8}, 'Action, Adventure, Sci-Fi': {'count': 1, 'sum_score': 8.8, 'avg_score': 8.8}} ``` 以上代码已经实现了您的需求,统计了各种类型电影的平均评、电影数以及总评,并用一个新字典来保存这些信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值