Problem E: 农夫果园
来源:互联网 发布:阿里云 弹性web托管 编辑:程序博客网 时间:2024/04/20 10:11
HomeWeb BoardProblemSetStandingStatusStatistics
Submit: 1338 Solved: 1085
[Submit][Status][Web Board][Submit][Status][Web Board]
Problem E: 农夫果园
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1338 Solved: 1085
[Submit][Status][Web Board]
Description
秋天到了,果园里的水果成熟了,商贩们来收水果了,农夫们都希望自己的水果能卖个好价钱。
现在果园里有三种水果正在销售,苹果(Apple)、香蕉(Banana)、梨(Pear)。每次销售都会记录下水果的种类、单价和总量,input()函数可以读取每条销售记录的单价和总量,total()函数可以计算出这次销售的总价。
但是,销售员在记录时忙中出错,各中水果的单价和总量的单位没有统一。单价是每公斤的价格,而水果是按箱记录的。其中,苹果一箱按30公斤计算,香蕉一箱按25公斤计算,梨一箱按20公斤计算。每种水果每次销售的总价是“单价*总量*每箱公斤数”。
现在,你来设计一个程序帮忙计算果园卖出水果的总价。由于total()函数对每种水果的计算方式都不一样,因此使用多态来实现。
-----------------------------------------------------------------------------
你设计并实现这个水果类的派生体系,使得main()函数能够运行并得到正确的输出。调用格式见append.cc
Input
输入的第一个整数n,表示后面有n条水果收购的记录。每条记录分为3部分,水果种类、单价和总量。
Output
输出为一行,表示整个果园卖出水果的总价。
Sample Input
5Apple 4.2 100Banana 8.8 50Apple 4.5 200Banana 7.8 100Pear 3.7 100
Sample Output
Total Price : 77500
HINT
Append Code
append.c,append.cc,
한국어< 中文 فارسی English ไทย
All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin:admin
#include <iostream>using namespace std;class Fruit{public: virtual void input() = 0; virtual double total() const = 0;public: Fruit( ) { }protected: double uprice; int num;};class Apple : public Fruit{public: void input() { cin >> uprice; cin >> num; } double total() const { return uprice * num * 30; }public: Apple( ) : Fruit() { }}; class Banana : public Fruit{public: void input() { cin >> uprice; cin >> num; } double total() const { return uprice * num * 25; }public: Banana( ) : Fruit() { }}; class Pear : public Fruit{public: void input() { cin >> uprice; cin >> num; } double total() const { return uprice * num * 20; }public: Pear( ) : Fruit() { }}; int main(){ Fruit* fruit; string fruit_name; double sum = 0.0; int cases; cin >> cases; for(int i = 1; i <= cases; i++) { cin >> fruit_name; if(fruit_name == "Apple") fruit = new Apple(); if(fruit_name == "Banana") fruit = new Banana(); if(fruit_name == "Pear") fruit = new Pear(); fruit->input(); sum += fruit->total(); delete fruit; } cout << "Total Price : " << sum << endl; return 0;}
0 0
- Problem E: 农夫果园
- Problem C: 农夫果园
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem-E
- 深入理解Python中的生成器
- iOS小技巧总结,绝对有你想要的
- OpenAI Gym学习
- 使用正则表达式获取指定的字符串的值
- c++实验6—项目六:字符串插入
- Problem E: 农夫果园
- @JsonFormat和@DateTimeFormat 实践测试
- java 查找子串出现的次数
- hdu 6026 用nlogn的dijstra敲一发
- Python 数字
- 一篇文章让你彻底搞清楚Python中self的含义
- golang中对象方法作为函数指针
- 老鸟的几年工作总结
- android 隐藏导航栏+标题栏