AC++第三章答案
来源:互联网 发布:网络投资与理财风险 编辑:程序博客网 时间:2024/04/30 11:21
3-1 如本章内容所述,计算数值集合中值有三点:
1)中值区分数值集合中比其大的一半和比其小的一半;
2)数值集合必须由大到小排序以抉择出哪个数值可以成为中值;
3)中值必须由整个数值集合中所有数决定。
由上述三点决定了整个数值集合一旦有了变化,那么很可能会影响到中值的判断。
比如有一个数值集合已经读入了1,3, 5三个元素,中值为3。下一个读入的为15,则中值为3和5的均值,即4。此时再次读入一个数20。如果舍弃1或3,则中值则为5和15的均值;如果舍弃了5,中值为3和15的均值,如果舍弃15,中值为3和5的均值。并且舍弃两个数,也同样会导致中值结果发生变化,因此不可以随便舍弃。
3-2 源代码:
#include <algorithm>#include <iostream>#include <ios>#include <iomanip>#include <vector>using namespace std;int main(int argc, char *argv[]){cout << "Enter the numbers until the end of the file: ";int num;vector<int> vec_nums;while (cin >> num) {vec_nums.push_back(num);//cout<<num<<endl;}typedef vector<int>::size_type vec_sz;vec_sz size = vec_nums.size();if (size / 4 == 0) {cout << endl << "You must enter at least 4 numbers." << endl;//cout<<size;return 1;}sort(vec_nums.begin(), vec_nums.end());vec_sz mid_1;vec_sz mid_2;vec_sz mid_3;mid_2 = size / 2;mid_1 = size / 4;mid_3 = size * 3 / 4;vec_sz count = 0;cout << "Now print the first part of the numbers: " << endl;for (count = size; count > mid_3; --count) {cout << vec_nums[count - 1] << " ";}cout << endl;cout << "Now print the second part of the numbers: " << endl;for (; count > mid_2; --count) {cout << vec_nums[count - 1] << " ";}cout << endl;cout << "Now print the third part of the numbers: " << endl;for (; count > mid_1; --count) {cout << vec_nums[count - 1] << " ";}cout << endl;cout << "Now print the last part of the numbers: " << endl;for (; count > 0; --count) {cout << vec_nums[count - 1] << " ";}cout << endl; system("PAUSE"); //return EXIT_SUCCESS; return 1;}
我这个写的比较麻烦,下面贴上github上某前辈简洁之作:(但同样在eclipse中无法直接ctrl+Z结束,输入完要先按enter,然后点击红色结束按钮才能看到结果,而直接打开运行的.exe文件可以识别数字之间的空格)
#include <algorithm>#include <iostream>#include <vector>using namespace std;int main() { vector<int> integers; cout << "Integers: "; int x; while (cin >> x) integers.push_back(x); if (integers.size() == 0) { cout << endl << "No integers!" << endl; return 1; } sort(integers.rbegin(), integers.rend()); typedef vector<int>::size_type vec_sz; cout << "1st quartile" << endl; for (vec_sz i = 0; i < integers.size() / 4; ++i) cout << integers[i] << endl; cout << "2nd quartile" << endl; for (vec_sz i = integers.size() / 4; i < integers.size() / 2; ++i) cout << integers[i] << endl; cout << "3rd quartile" << endl; for (vec_sz i = integers.size() / 2; i < integers.size() * 3 / 4; ++i) cout << integers[i] << endl; cout << "4th quartile" << endl; for (vec_sz i = integers.size() * 3 / 4; i < integers.size(); ++i) cout << integers[i] << endl; system("PAUSE"); return 0;}
两程序执行结果相同,说明只要将sort的参数分别定义为*.rbegin() 和 *.rend()则代表将排序变为递减。
3-3 源代码:
#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;int main() { typedef vector<string>::size_type vec_sz; vector<string> words; cout << "Enter the words until the end of the file: "; string word; while (cin >> word) { words.push_back(word); } if(words.size()==0) { cout<<"Please enter at least 1 word."<<endl; return 0; } sort(words.begin(),words.end()); vec_sz size=words.size(); vec_sz index = 0; int count = 0; word=words[0]; while(index<size) { if(words[index]!=word) { cout<<word<<": "<<count; count=0; } count++; index++; } cout<<word<<": "<<count; return 0;}
下面同样附上github上前辈的代码,不过貌似没有我的简洁。。。但是更加规矩:
#include <iostream>#include <string>#include <vector>using namespace std;int main() { typedef vector<string>::size_type vec_sz; vector<string> words; vector<int> counts; cout << "Words: "; string s; while (cin >> s) { bool found = false; for (vec_sz i = 0; i < words.size(); ++i) { if (s == words[i]) {++counts[i];found = true; } } if (!found) { words.push_back(s); counts.push_back(1); } } for (vec_sz i = 0; i < words.size(); ++i) cout << words[i] << " appeared " << counts[ ] << " times" << endl; return 0;}
3-4 源代码:(本题没什么好说的。。。)
//#include <algorithm>#include <iostream>//#include <ios>//#include <iomanip>#include <string>using namespace std;int main(int argc, char *argv[]) {cout << "Enter the words until the end of the file: "<<endl;string word="";string max_word, min_word;cin >> word;max_word = word;min_word = word;while (cin >> word) {if (word.size() > max_word.size()) {max_word = word;}if (word.size() < min_word.size()) {min_word = word;}}cout << max_word.size() << endl;cout << min_word.size() << endl;//system("PAUSE");//return EXIT_SUCCESS;return 1;}
3-5 源代码:(本题题意一开始没有搞清楚,先贴上前辈的代码,自己的明天有空再写。。。)
#include <iomanip>#ifndef __GNUC__#include <ios>#endif#include <iostream>#include <string>#include <vector>using std::cin; using std::setprecision;using std::cout; using std::string;using std::endl; using std::streamsize;#define NUM_HOMEWORK 2using std::vector;int main() { vector<string> names; vector<double> final_grades; bool done = false; while (!done) { // ask for and read the student's name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; names.push_back(name); // ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; // ask for the homework grades cout << "Enter both your homework grades, " "followed by end-of-file: "; // the number and sum of grades read so far int count = 0; double sum = 0; // a variable into which to read double x; // invariant: // we have read `count' grades so far, and // `sum' is the sum of the first `count' grades while (count < NUM_HOMEWORK) { ++count; cin >> x; sum += x; } double final_grade = 0.2 * midterm + 0.4 * final + 0.4 * sum / count; final_grades.push_back(final_grade); cout << "More? (Y/N) "; string s; cin >> s; if (s != "Y") done = true; } for (vector<string>::size_type i = 0; i < names.size(); ++i) { // write the result streamsize prec = cout.precision(); cout << names[i] << "'s final grade is " << setprecision(3) << final_grades[i] << setprecision(prec) << endl; } return 0;}3-6 同上,先贴上前辈的代码,自己的晚上回去写。
#include <iomanip>#ifndef __GNUC__#include <ios>#endif#include <iostream>#include <string>using std::cin; using std::setprecision;using std::cout; using std::string;using std::endl; using std::streamsize;int main() { // ask for and read the student's name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; // ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; // ask for the homework grades cout << "Enter all your homework grades, " "followed by end-of-file: "; // the number and sum of grades read so far int count = 0; double sum = 0; // a variable into which to read double x; // invariant: // we have read `count' grades so far, and // `sum' is the sum of the first `count' grades while (cin >> x) { ++count; sum += x; } double homework_grade = (count > 0) ? sum / count : 0.0; // write the result streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * homework_grade << setprecision(prec) << endl; return 0;}
0 0
- AC++第三章答案
- AC++第二章答案
- 思科CCNA第三学期第三章答案
- Opencv第三章练习题答案
- 编译原理答案--第三章
- 学习opencv第三章答案
- AC++第一章答案
- 计算机网路第三章部分答案
- 编程珠玑第三章习题答案
- 学习opencv 习题答案-第三章
- c++ primer plus第三章习题答案
- 思科CCNA第一学期第三章答案
- 思科CCNA第二学期第三章答案
- 思科CCNA第三学期第二章答案
- 思科CCNA第三学期第四章答案
- 思科CCNA第三学期第五章答案
- 思科CCNA第三学期第六章答案
- 思科CCNA第三学期第七章答案
- servlet概论http://www.iteedu.com/webtech/j2ee/javaservletsbczn/1.php
- Java中数据库连接池原理机制的详细讲解
- xml object 转换
- opencv简单的数据类型
- go 速学 - 12 - 方法
- AC++第三章答案
- 第五周 程序阅读——指针(3)
- 大端和小端(Big endian and Little endian)
- Binary Tree Right Side View
- 5.9.3 静态初始化块
- android常见面试题与我自己的回答
- Binary Tree Inorder Traversal
- java并发编程 之 并发集合对象
- 数据集基本信息查看