C++程序设计教程(钱能)第五章习题
- 1. 递归方法计算poly函数
- 2. 使用函数声明、定义和调用,改写程序
- 3. 求整数对的最大公约数并排序
- 4. 用递归方法求解母牛问题
- 5. 编程实现
- 6. 读入C-串,并排序后输出
1. 递归方法计算poly函数
#include
using namespace std;
double poly(double x, int n) {
if (n == 0)
return 1;
if (n == 1)
return x;
if (n > 1)
return ((2 * n - 1)*x*poly(x, n - 1) - (n - 1)*poly(x, n - 2)) / n;
}
int main(int argc, char** argv) {
cout << "poly(1.2,8) = " << poly(1.2, 8) << endl;
}
2. 使用函数声明、定义和调用,改写程序
#include
using namespace std;
const int rows = 5;
const int columns = 4;
int a[rows][columns];
void input();
void total();
void average();
void input() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cin >> a[i][j];
}
}
}
void total() {
for (int i = 0; i < rows; i++) {
double sum = 0;
for (int j = 0; j < columns; j++) {
sum += a[i][j];
}
cout << (i + 1) << ":" << sum << endl;
}
}
void average() {
for (int i = 0; i < columns; i++) {
double sum = 0;
for (int j = 0; j < rows; j++) {
sum += a[j][i];
}
cout << "No "<<i<<" average is "<< sum/rows << endl;
}
}
int main(int argc, char** argv) {
cout << "-----------【输入5人4科成绩】--------------" << endl;
input();
cout << "-----------【个人总成绩】--------------" << endl;
total();
cout << "-----------【各科平均成绩】----------------" << endl;
average();
}
3. 求整数对的最大公约数并排序
12 35
77 91
123 789
24 28
64 112
1024 888
98 54
#include
#include
#include
#include
#include
using namespace std;
long fun(int a, int b);
void print(const vector<int>& v);
long fun(int a, int b) {
for (int temp; b; ) {
temp = a%b;
a = b;
b = temp;
}
return a;
}
void print(const vector<int>& v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
int main(int argc, char** argv) {
vector<int>result;
ifstream in("123.txt");
for (string s; getline(in, s); ) {
int a, b;
for (istringstream sin(s); sin >> a >> b; ) {
result.push_back(fun(a, b));
}
}
sort(result.begin(),result.end());
print(result);
}
4. 用递归方法求解母牛问题
#include
using namespace std;
int cows(int n) {
if (n < 4)
return 1;
else
return cows(n - 1) + cows(n - 3);
}
int main(int argc, char** argv) {
for (int i = 1; i < 21; i++) {
cout << "第" << i << "年,母牛数为" << cows(i) << endl;
}
}
5. 编程实现
12 567 91 33 657 812 2221 3 77
#include
#include
#include
#include
#include
#include
using namespace std;
int calculate(int number);
bool compare(int a, int b);
void print(const vector<int>&v);
vector<int> input(string filename);
int calculate(int number) {
int sum = 0;
for (int x = number; x; x /= 10) {
sum += pow((x % 10),2);
}
return sum;
}
bool compare(int a, int b) {
return calculate(a) < calculate(b);
}
void print(const vector<int>&v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
vector<int> input(string filename) {
vector<int>numbers;
fstream in(filename);
for (string s; getline(in, s); ) {
int element = 0;
for (istringstream sin(s); sin >> element; ) {
numbers.push_back(element);
}
}
return numbers;
}
int main(int argc, char** argv) {
vector<int>numbers = input("456.txt");
if (numbers.size() == 0)
cout << "No Element!"<<endl;
else {
sort(numbers.begin(), numbers.end(), compare);
print(numbers);
}
}
6. 读入C-串,并排序后输出
asfdf rtr
Asfasdf sdfgfgfdgdg
gfdgdfg
fgf dgf
efdf b34 4345 tr6
efdsf th 776
#include
#include
#include
#include
using namespace std;
int main(int argc, char** argv) {
vector<string>strs;
fstream in("789.txt");
for (string s; getline(in, s); ) {
strs.push_back(s);
}
sort(strs.begin(), strs.end());
for (int i = 0; i < strs.size(); i++) {
cout << strs[i] << endl;
}
}