accelerated c++答案(自写)---第三章
来源:互联网 发布:网络直播收入排名 编辑:程序博客网 时间:2024/05/21 09:09
看accelerated c++发现没有课后答案,我自己贴出来吧。随手所写,看看就好。
用中值求成绩
#include<iostream>#include<iomanip>#include<ios>#include<algorithm>#include<vector>#include<string>using namespace std;int main(){//请求输入姓名cout << "please enter your name!:";string name;getline(cin,name);//此方法可以处理有空格的输入//cin >> name;cout << "hello!"<<name<<endl;//请求输入期中和期末成绩cout << "please enter your midterm and final grade:";double midterm_grade, final_grade;cin >> midterm_grade >> final_grade;//请求输入平时作业成绩cout << "please enter your homework ended with a char!";double x;vector<double> homework;while (cin >> x){homework.push_back(x);}auto size = homework.size();//vector<double>::size_type size = cout.precision();//或者这样,不用autoif (size == 0){cout << "please enter your homework grade!" << endl;return 1;}sort(homework.begin(),homework.end());//alrorithm中还有诸如max等算法auto mid = size / 2;double median_grade = size % 2 == 0 ? (homework[mid] + homework[mid - 1]) / 2 : homework[mid];auto prec = cout.precision();//streamsize prec2 = cout.precision();//或者这样cout << "your final grade is:" << setprecision(3)<< 0.2*midterm_grade + 0.4*final_grade + median_grade*0.4<< setprecision(prec) << endl;system("pause");return 0;}3-2、将整数集合分为4部分非递增输出,只有一点当不为4的整数倍数时,最后一个输出和以前的不等大。同时注意总的大小小于4的情况即可。
#include<iostream>#include<iomanip>#include<ios>#include<algorithm>#include<vector>#include<string>using namespace std;int main(){cout << "please enter the num to be sorted,ended with ctr+z:";vector<int> store;int input;while (cin >> input)store.push_back(input);auto size_of_store = store.size();if (size_of_store == 0){cout << "ok i guess you do not want to do this"<<endl;return 1;}sort(store.begin(),store.end());auto increment = size_of_store / 4;//增量vector<int>::iterator ptr = store.end();if (increment == 0)//少于4{ptr--;while (ptr != store.begin()){cout << *ptr<<endl;--ptr;}cout << *ptr << endl;}else{ptr--;int count = 0;while (ptr != store.begin()){cout << *ptr << " ";count++;if (count%increment == 0&&count!=4*increment)cout << endl;--ptr;}cout << *ptr << endl;}system("pause");return 0;}3-3、统计单词出现次数
#include<iostream>#include<iomanip>#include<ios>#include<algorithm>#include<vector>#include<string>using namespace std;struct str_count{string index;int count;};int main(){cout << "enter your word:";string input;str_count input_count;vector<str_count> store;vector<str_count>::iterator ptr;bool found = false;while (cin >> input){for (ptr = store.begin(); ptr != store.end(); ++ptr){if (ptr->index == input){ptr->count++;found = true;break;}}if (!found){input_count.index = input;input_count.count = 1;store.push_back(input_count);}found = false;}ptr = store.begin();while (ptr != store.end()){cout << ptr->index << ": " << ptr->count << endl;++ptr;}system("pause");return 0;}3-4、报告最长最短字符串长度
#include<iostream>#include<iomanip>#include<ios>#include<algorithm>#include<vector>#include<string>using namespace std;int main(){cout << "enter your string:";string max;string min;string input;cin >> input;max = min = input;while (cin >> input){if (input.size() > max.size())max = input;if (input.size() < min.size())min = input;}cout << "the minimum string is:" << min << " the size is:"<<min.size()<<endl;cout << "the maximum string is:" << max <<" the size is:"<<max.size()<< endl;system("pause");return 0;}3-5、计算保存学生成绩
将第一个整合一下过来就行了。
0 0
- accelerated c++答案(自写)---第三章
- Accelerated C++ 第三章 3.3
- 《accelerated c++》---------第六章
- accelerated c++ 1章 练习答案
- c++primer第五版课后练习答案(第三章)
- C Primer Plus(第6版)第三章答案
- C++primer第五版第三章答案(1)
- C++primer第五版第三章答案(2)
- Accelerated C++ 第三章 3.4 &&3.5 习题
- Accelerated C++ 习题答案
- 谭浩强 《C程序设计》 第三版 第三章习题答案 (修正)
- 《accelerated c++》第九章---设计类
- 【Accelerated C++】重点回顾(续)
- 【Accelerated C++】重点回顾(续)
- “Accelerated C++” 阅读笔记(一)
- “Accelerated C++” 阅读笔记(二)
- “Accelerated C++” 阅读笔记(三)
- “Accelerated C++” 阅读笔记(四)
- Replica Island 学习笔记 04 - AndouKun
- Android解耦库EventBus的使用和源码分析
- 字节对齐
- codeforces 474C Captain Marmot dfs
- java的重写、重载、覆盖的区别
- accelerated c++答案(自写)---第三章
- C#-—体验字段
- UBuntu安裝使用PIP
- Android Launcher3壁纸居中问题
- 关于 sprintf,关于类型安全。
- stardict安装
- 【java】java.awt基础(简易记事本代码)
- 【远程Service】AIDL进程间通信实例
- codeforces 474D Flowers dp