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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 qq邮箱超大附件过期怎么办 忘记qq邮箱独立密码怎么办 网易邮箱账号忘了怎么办 微信登录密码忘了怎么办 微信太久没登录登录不上怎么办 邮箱独立密码忘记了怎么办 苹果设置id没有邮箱怎么办 苹果手机设置id没有邮箱怎么办 邮箱的附件过期了怎么办 邮箱里的附件过期了怎么办 邮箱中附件过期了怎么办 扣扣邮箱附件过期怎么办 公司网页版邮箱进不去了怎么办 农行客户端密码忘记了怎么办 中国银行客户端密码忘记了怎么办 建行客户端登录密码忘记了怎么办 中国移动客户端密码忘记了怎么办 再歪一点授权码绑定怎么办 网易邮箱号忘了怎么办 忘记支付宝账号和密码怎么办 支付宝账号密码忘了怎么办 发邮箱文件超2g怎么办 报考计算机二级邮箱不存在怎么办 苹果邮箱登录要imap密码怎么办 注销微信支付后怎么办 手机卡注销后支付宝怎么办 12306手机邮箱都换了怎么办 网易手机邮箱手机换了怎么办 崩坏3号被盗了怎么办 qq账号被永久冻结了怎么办 淘宝账号被永久冻结了怎么办 qq账号被永久冻结怎么办 多多理财账号冻结了怎么办 苹果id安全问题忘记了怎么办 网易邮箱帐号忘了怎么办 网易邮箱密码忘记了怎么办 网易邮箱密码忘了怎么办 网易邮箱忘记邮箱账号怎么办 崩坏3三无号被盗怎么办 qq邮箱给58占用怎么办 支付宝邮箱被占用怎么办