sicily--1159. Sum

1.练练重载函数

2.记得之前也有一道类似的题目,同样也是在“最高进位”的问题上忘记考虑了

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

class BigNum
{
public:
	BigNum()
	{
		this->num = "0";
	}
	BigNum(string n)
	{
		this->num = n;
	}
	string getNum()
	{
		return this->num;
	}

	friend BigNum operator +(const BigNum &n1, const BigNum &n2)
	{
		string temp;//和的倒置
		int carry = 0;
		//计算结果
		for(int i = n1.num.length() - 1, j = n2.num.length() - 1; i >= 0 || j >= 0; i--, j--)
		{
			if(i >= 0 && j >= 0)//两者、进位之和
			{
				temp.push_back(((n1.num[i] - 48) + (n2.num[j] - 48) + carry) % 10 + 48);//存储结果
				if((n1.num[i] - 48) + (n2.num[j] - 48) + carry > 9)//有新的进位
				{
					carry = 1;//两个十进制之和的进位不会超过1
				}
				else//没有进位
				{
					carry = 0;
				}
			}
			else//某一个数的位数已经结束
			{
				if(i >= 0)
				{
					temp.push_back(((n1.num[i] - 48) + carry) % 10 + 48);
					if((n1.num[i] - 48) + carry > 9)
					{
						carry = 1;
					}
					else
					{
						carry = 0;
					}
				}
				else//j >= 0
				{
					temp.push_back(((n2.num[j] - 48) + carry) % 10 + 48);
					if((n2.num[j] - 48) + carry > 9)
					{
						carry = 1;
					}
					else
					{
						carry = 0;
					}
				}
			}
		}
		if(carry == 1)//最高进位
			temp.push_back('1');

		//获取正确的结果
		string rev;
		bool flag = true;//去除头部的0 
		for(int i = temp.length() - 1; i >= 0; i--)
		{
			if(temp[i] == '0' && flag)
			{
				continue;
			}
			else
				flag = false;

			rev.push_back(temp[i]);
		}

		//创建一个BigNum对象,并返回
		BigNum sum(rev);
		return sum;
	}

	friend ostream& operator<<(ostream& outs, const BigNum& n)
	{
		outs << n.num;
		
		return outs;
	}
private:
	string num;
	
};

int main()
{
	int N;
	while(cin >> N && N != 0)
	{
		BigNum sum;//初始化为0
		for(int i = 1; i <= N; i++)
		{
			string s;
			cin >> s;
			BigNum temp(s);

			sum = sum + temp;
		}
		cout << sum << endl;
	}
	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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值