买菜-2(100分)


```cpp
#include<iostream>
#include<vector>
#include<utility>
using namespace std;
vector<pair<int, int>>x;
vector<pair<int, int>>y;
int main() {
	long long n,begin,end,sum=0,min,max; cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> begin >> end;
		x.push_back(make_pair(begin, end));
	}
	for (int i = 0; i < n; i++) {
		cin >> begin >> end;
		y.push_back(make_pair(begin, end));
	}
	
	for (int i = 0; i < n; i++) {
		for ( int j=0; j < n; j++) {			
			if (y[j].first > x[i].second)
				break;
			if (x[i].first > y[j].second)
				continue;
			else {
				min = x[i].first > y[j].first ? x[i].first : y[j].first;
				max = x[i].second < y[j].second ? x[i].second : y[j].second;
				sum = sum + max - min;
			}
		}
	}
	cout << sum << endl;
}


1.买菜计价程序: # 定义单字典 menu = { '1': {'name': '青', 'price': 2}, '2': {'name': '土豆', 'price': 3}, '3': {'name': '西红柿', 'price': 4}, '4': {'name': '黄瓜', 'price': 2.5}, '5': {'name': '茄子', 'price': 3.5} } # 显示单 print('单如下:') for key, value in menu.items(): print(key, value['name'], value['price']) # 购买菜品 total_num = 0 # 总购数量 total_price = 0 # 总花费金额 while True: choice = input('请输入要购品编号(按q退出):') if choice == 'q': break if choice not in menu: print('输入有误,请重新输入!') continue num = input('请输入购数量:') try: num = int(num) if num <= 0: print('输入有误,请重新输入!') continue except: print('输入有误,请重新输入!') continue total_num += num total_price += num * menu[choice]['price'] print('已购{}个{},花费{}元。'.format(num, menu[choice]['name'], num * menu[choice]['price'])) # 显示购结果 print('您购了{}个品,总共花费了{}元。'.format(total_num, total_price)) 2.成绩统计程序: import random # 随机生成10名同学的成绩 scores = [] for i in range(10): scores.append(random.randint(0, 100)) print('10名同学的成绩为:', scores) # 统计优秀率和及格率 excellent_count = 0 # 优秀人数 pass_count = 0 # 及格人数 for score in scores: if score >= 90: excellent_count += 1 if score >= 60: pass_count += 1 excellent_rate = excellent_count / len(scores) pass_rate = pass_count / len(scores) print('优秀率为:{:.1%},及格率为:{:.1%}'.format(excellent_rate, pass_rate))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值