并非标答,自己随便写写的。
第一题:
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
double getAva(int x, int y) {
return 2.0 * x * y / (x + y);
}
int main()
{
using namespace std;
int a, b;
a = b = 1;
while (1) {
cout << "请输入:";
cin >> a >> b;
if (a == 0 || b == 0) {
break;
}
cout << a << " " << b << endl;
cout << getAva(a, b) << endl;
}
}
第二题:
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
using namespace std;
void newScores(int* score, int &cnt) {
while (cin >> score[cnt]) {
cnt++;
if (cnt >= 10) {
cnt--;
return;
}
}
}
double getAvaScores(int* score, int cnt) {
int summ = 0;
for (int i = 0; i < cnt; i++) {
summ += score[i];
}
return (double)summ / cnt;
}
void showScores(int* score, int cnt) {
for (int i = 0; i < cnt; i++) {
cout << score[i] << " ";
}
cout << endl;
cout << getAvaScores(score, cnt) << endl;
}
int main()
{
int score[10];
int cnt = 0;
newScores(score, cnt);
showScores(score, cnt);
}
第三题:
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
using namespace std;
struct box {
char maker[40];
float height;
float width;
float length;
float volumn;
};
void a(box x) {
cout << x.maker << endl;
cout << x.height << endl;
cout << x.width << endl;
cout << x.length << endl;
cout << x.volumn << endl;
cout << "函数a end" << endl << endl;
}
void b(box* x) {
x->volumn = x->height * x->width * x->length;
}
int main()
{
box x;
char t[] = "goodStudy";
strcpy_s(x.maker, t);
x.height = 1;
x.width = 2;
x.length = 3;
x.volumn = 4;
a(x);
cout << "修改前volumn:" << x.volumn << endl;
b(&x);
cout << "修改后volumn:" << x.volumn << endl;
}
第四题:
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
using namespace std;
double getChance(int first, int sec) {
long long allNum = 1;
allNum = (first - 0) * (first - 1) * (first - 2) * (first - 3) * (first - 4) * sec;
long long rightNum = 5 * 4 * 3 * 2;
return (double)rightNum / allNum;
}
int main()
{
cout << getChance(47, 27);
}
第五题
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
using namespace std;
int getJieChen(int x) {
if (x == 1) {
return 1;
}else{
return x * getJieChen(x - 1);
}
}
int main()
{
int x;
while (cin >> x) {
cout << getJieChen(x) << endl;
}
}
第六题:
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
#include <string.h>
using namespace std;
/*bool checkDouble(string num) {
int l = num.size();
bool onePoint = true;
for (int i = 0; i < l; i++) {
if (i == '.' && i != 0 && i != l - 1) {
if (onePoint) {
onePoint = false;
}else {
return false;
}
}
else if (num[i] > '9' || num[i] < '0') {
return false;
}
}
return true;
}*/
int Fill_array(double nums[], int length) {
string temp;
double t;
int cnt = 0;
while (cin >> t) {
nums[cnt] = t;
cnt++;
if (cnt == length) {
return cnt;
}
}
}
void Show_array(double nums[], int length) {
for (int i = 0; i < length; i++) {
cout << nums[i] << " ";
}
cout << endl;
}
void Reverse_array(double nums[], int s, int e) {
while (s < e) {
int t = nums[s];
nums[s] = nums[e];
nums[e] = t;
s++;
e--;
}
}
int main()
{
double x[5];
int num = Fill_array(x, 5);
Show_array(x, num);
Reverse_array(x, 1, num-2);
Show_array(x, num);
}
7.8.9题,略
第十题:
#include <iostream>
#include <fstream>
#include <float.h>
#include <array>
#include <string.h>
using namespace std;
double calculate(double a, double b, double (*cal)(double, double)){
return cal(a, b);
}
double add(double a, double b) {
return a + b;
}
double minuss(double a, double b) {
return a - b;
}
double chen(double a, double b) {
return a * b;
}
double chu(double a, double b) {
return a / b;
}
int main()
{
double(*nums[4])(double, double);
nums[0] = &add;
nums[1] = &minuss;
nums[2] = &chen;
nums[3] = &chu;
for (int i = 0; i < 4; i++) {
int a;
int b;
cin >> a >> b;
cout << calculate(a, b, nums[i]) << endl;
}
}