sicily--1324. Score

本文介绍了一个简单的程序,用于计算特定字符串序列中字符的累积得分。当遇到字符'X'或'x'时,权重会重置为1,之后每个字符都会使得分增加,权重也随之递增。

水题;记得在重新出现“X”后, 权值变为1即可

#include<iostream>
#include<string>
using namespace std;

int main()
{
    int testNum;
    cin >> testNum;
    while(testNum != 0)
    {
        string s;
        int score = 0;
        int weight = 1;
        cin >> s;
        int len = s.length();
        for(int i = 0; i < len; i++)
        {
            if(s[i] == 'x' || s[i] == 'X')
            {
                weight = 1;
            }
            else
            {
                score = score + weight;
                weight++;
            }
        }
    
        cout << score << endl;
        testNum--;
        
    }
    return 0;
    
}                                 


任务描述 本关任务:编写程序,多维度分析葡萄酒数据。 相关知识 葡萄酒评论分析报告描述 winemag-data.csv 文件 winemag-data.csv 包含 编号、国家、描述、评分、价格、省份 等 6列 和12974行葡萄酒评论的数据。数据格式如下所示:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬ number,country,description,points,price,province 30,France,"Red cherry fruit comes laced with....",86,15,Beaujolais 50,Italy,"This blend of Nero Avola and Syrah....",86,15,Sicily 100,US,"Fresh apple, lemon and pear flavors....",88,18,New York‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬ 通过分析这些数据,用户可以根据产地、评份、价格等挑选适合自己的葡萄酒,商家可以分析消费者的购买行为习惯,可以更加准确地提供适合市场的产品,精准定位客户。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬ 请读取文件中的数据,完成以下任务:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬ 输入 国家名列表 ,统计文件中出现的葡萄酒生产国家,输出不重复的国家名列表,按字母表升序排序, 若国家名数据缺失,略过该条数据,返回值中不包含空字符串元素 输入 平均分 ,计算每个国家的葡萄酒的平均得分( 保留最多 2 位小数),返回值为国家名和得分的列表 输入其他时,输出 输入错误 编程要求 根据提示,在右侧编辑器补充代码,完成葡萄酒评论分析报告代码编写。 测试说明 平台会对你编写的代码进行测试: 示例仅为格式展示,与测试用例无关 测试输入1: 国家名列表 预期输出1: ['Argentina', 'Armenia', ...... 'US', 'Ukraine', 'Uruguay'] 测试输入2: 平均分 预期输出2: [['Argentina', 86.72], ['Armenia', 87.0],...... ['Ukraine', 83.0], ['Uruguay', 88.0]] 开始你的任务吧,祝你成功! import csv def read_wine_data(): """读取葡萄酒数据文件,返回数据行列表(跳过表头)""" data = [] with open('winemag-data.csv', 'r', encoding='utf-8') as f: reader = csv.reader(f) next(reader) # 跳过表头 for row in reader: data.append(row) return data def get_unique_countries(data): """从数据中提取不重复的国家名,按字母升序排序,排除空值""" countries = set() for row in data: country = row[1].strip() # 去除可能的空格 if country: # 排除空字符串 countries.add(country) return sorted(countries) # 按字母升序排序 def get_country_average_scores(data): """计算每个国家的葡萄酒平均得分,保留两位小数,按国家升序排序""" country_scores = {} for row in data: country = row[1].strip() if not country: continue # 跳过空国家 points = float(row[3].strip()) # 转换评分为浮点数 # 累计每个国家的评分 if country in country_scores: country_scores[country].append(points) else: country_scores[country] = [points] # 计算平均分并整理结果 average_scores = [] for country in sorted(country_scores.keys()): # 按国家升序 scores = country_scores[country] avg = sum(scores) / len(scores) average_scores.append((country, round(avg, 2))) return average_scores # 主程序执行 if __name__ == "__main__": data = read_wine_data() user_input = input().strip() if user_input == "国家名列表": countries = get_unique_countries(data) for country in countries: print(country) elif user_input == "平均分": avg_scores = get_country_average_scores(data) for country, score in avg_scores: print(f"{country}: {score:.2f}") else: print("输入错误")
最新发布
12-03
文件“winemag-data.csv”包含编号、国家、描述、评分、价格、省份等6列和12974行葡萄酒评论的数据。数据格式如下所示: number,country,description,points,price,province 30,France,"Red cherry fruit comes lacedwith....",86,15,Beaujolais 50,Italy,"This blend of Nero Avola and Syrah....",86,15,Sicily 100,US,"Fresh apple, lemon and pearflavors....",88,18,New York 通过分析这些数据,用户可以根据产地、评份、价格等挑选适合自己的葡萄酒,商家可以分析消费者的购买行为习惯,可以更加准确地提供适合市场的产品,精准定位客户。 请读取文件中的数据,完成以下任务: (a) 统计文件中出现的葡萄酒生产国家,输出不重复的国家名列表,按字母表升序排序,若国家名数据缺失,略过该条数据,返回值中不包含空字符串元素 (b) 计算每个国家的葡萄酒的平均得分,返回值为国家名和得分的列表 (c) 计算每个国家的葡萄酒的平均得分,返回值为国家名和得分的列表, 按评分由高到低降序排列 (d) 评分最高的十款葡萄酒的编号、出产国、评分和价格,按评分降序输出 (e) 价格最高的二十款葡萄酒的编号、出产国、评分和价格,按价格降序输出 (f) 统计各个评分的葡萄酒数量是多少?输出包含评分和数量的列表 (g) 输出拥有葡萄酒数量最多的评分和数量 (h) 输出拥有葡萄酒数量最多的评分的葡萄酒的平均价格
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值