Accelerated C 習題解答 第3章

2021-06-20 13:49:18 字數 2456 閱讀 5987

部分習題參考了網上已有的解答

ex.3-0

#include#include#include#include#include#include#includeusing std::cin;

using std::cout;

using std::endl;

using std::string;

using std::vector;

using std::sort;

using std::streamsize;

using std::setprecision;

int main()

sort(homework.begin(),homework.end());

vec_sz mid = size/2;

double median;

median = size % 2 ==0 ? (homework[mid] + homework[mid-1]/2) : homework[mid];

streamsize prec =cout.precision();

cout <<"your final grade is " <

ex.3-1

中值是乙個可將數值集合劃分為相等的上下兩部分的數值。對於有限的數集,通過把所有的值高低排序後找出正中間的乙個作為中位數。中間點取決於所有的數值。

由此可知,如果任何乙個我們已經讀到的值被丟棄,則會改變原先中間點的位置,這樣就得不出準確的中值。

,ex.3-2

#include#include#include#includeusing std::cin;

using std::cout;

using std::endl;

using std::vector;

using std::sort;

int main()

}if (!found)

}for (vec_sz i = 0; i < words.size(); ++i)

getch();

return 0;

}

ex.3-4

#include#include#includeusing std::cin;

using std::cout;

using std::endl;

using std::string;

int main()

if (shortest_size == 0 || s.size() < shortest_size)

}cout <<"longest word is " << longest<<" (" <

ex.3-5

#include#include#include#include#include#includeusing std::cin;

using std::cout;

using std::endl;

using std::setprecision;

using std::string;

using std::streamsize;

using std::vector;

#define num_homework 2

int main()

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::size_type i = 0; i

ex.3-6

#include#include#include#include#includeusing std::cin;

using std::cout;

using std::endl;

using std::setprecision;

using std::string;

using std::streamsize;

int main()

double homework_grade = (count > 0) ? sum / count: 0.0;

streamsize prec =cout.precision();

cout <<"your final grade is " <

Accelerated C 習題解答 第1章

ex.1 01 include include includeint main std cout std string name std cin name std cout include include includeint main std cout std string name std ci...

Accelerated C 習題解答 第6章

部分解答參考了網上已有解答 ex.6 1 include include include include include using namespace std string size type width constvector v vectorframe constvector v ret.pu...

Accelerated C 習題 第0章

include using namespace std intmain this is a quote,and this is a backlash.include using namespace std intmain include using namespace std intmain inc...