Problem C: 农夫果园
来源:互联网 发布:淘宝客链接怎么生成 编辑:程序博客网 时间:2024/03/29 13:27
Description
一果农,家里有苹果树、梨树,2016年收成不错,现在需要你来编程,求一下去年总共收获了多少斤的水果。
Input
第一行N表示之后有N行输入。
之后的N行输入,每行一个水果。包括一个字符和一个整数。
其中字符是‘a’或‘p’,分别表示是一个苹果或梨。整数是这个水果的重量。
Output
见样例。
Sample Input
5a 10p 20a 30p 40a 50
Sample Output
The 1th fruit is created.The 1th apple is created.The 2th fruit is created.The 1th peach is created.The 3th fruit is created.The 2th apple is created.The 4th fruit is created.The 2th peach is created.The 5th fruit is created.The 3th apple is created.There are 5 fruits, including 3 apples and 2 peaches. Their total weights is 150.
HINT
请使用C++的面向对象来实现,练习下如何编写类、使用类。
Append Code
#include <iostream>using namespace std;class sellFruit{private: static int numOfFruits, numOfApples,numOfPeach,numOfAll;public: sellFruit() { } void setFruit(char f,int h) { if(f == 'a') { numOfApples++; numOfFruits++; cout << "The "<<numOfFruits<<"th fruit is created."<< endl; cout << "The "<<numOfApples<<"th apple is created." << endl; } else { numOfFruits++; numOfPeach++; cout << "The "<<numOfFruits<<"th fruit is created."<< endl; cout << "The "<<numOfPeach<<"th peach is created." << endl; } numOfAll += h; } static void getAll() { cout << "There are "<<numOfFruits<<" fruits, including "<<numOfApples<<" apples and "<<numOfPeach<<" peaches. Their total weights is "<<numOfAll<<"."; }};int sellFruit::numOfAll = 0;int sellFruit::numOfPeach = 0;int sellFruit::numOfFruits = 0;int sellFruit::numOfApples= 0;int main(){ int num,h; char c; sellFruit *s; cin >> num; s = new sellFruit[num]; for(int i = 0;i < num;i++) { cin >> c >> h; s[i].setFruit(c,h); } sellFruit::getAll(); return 0;}
阅读全文
0 0
- Problem C: 农夫果园
- Problem E: 农夫果园
- C语言农夫过河问题
- c实现农夫过河问题
- 农夫过河问题的c语言实现
- 农夫过河(数据结构)之C语言
- 果园监控
- 经典的农夫养牛题目,C语言,递归实现
- 农夫过河问题——C语言程序设计(转)
- 农夫过河问题——c语言代码
- 《算法与数据结构---C语言描述》里的农夫过河
- Problem C
- Problem C
- Problem C
- Problem C
- Problem C
- Problem C
- Problem c
- 学习淘淘商城第八十课(安装Keepalived)
- 文件的类型及权限
- 机器学习VS深度学习的区别
- opendir&readdir
- Ubuntu 14.04中文输入法的安装
- Problem C: 农夫果园
- UVa 1149 Bin Packing——优先队列
- 程序设计师真情忏悔录
- hive数据挖掘
- R语言基础
- Swift静态和动态分发机制
- Problem A: 家教课程
- map,set,multimap,multiset的异同
- 关于C++中的虚拟继承的一些总结