1
1
mirror of https://github.com/ParkMoonJ/KaoYan.git synced 2026-06-17 07:36:45 +08:00

Create 1.20.cpp

This commit is contained in:
ParkMoonJ
2021-04-06 14:44:44 +08:00
parent 58d00b64b1
commit c7d16d3348
7 changed files with 143 additions and 6 deletions

View File

@@ -123,5 +123,89 @@ int main() {
### 编写算法,处理上述表格,以统计各院校的男女总分和团体总分,并输出。
```cpp
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef enum {A, B, C, D, E} SchoolName;
typedef enum {FEMALE, MALE} Gender;
typedef struct {
char event[3];
Gender gender;
SchoolName school;
int score;
int point;
} Component;
typedef struct {
int maleSum;
int femaleSum;
int totalSum;
} Sum;
for (int i = 0; i < MAXSIZE; ++i) {
result[report[i].school].totalSum += report[i].point;
switch (report[i].gender) {
case 0:
result[report[i].school].femaleSum += report[i].point;
case 1:
result[report[i].school].maleSum += report[i].point;
}
}
for (int i = A; i < E; ++i) {
cout << ...
}
```
### 1.19 试编写算法,计算 $i! \cdot 2^i (i = 0, 1, \cdots, n - 1)$ 的值并分别存入数组 `a[arrsize]` 的各个分量中。假设计算机允许的整数最大值为 `MAXINT`,则当 $n > arrsize$ 或对某个 $k (0 \le k \le n - 1)$ 使 $k! \cdot 2^k > MAXINT$ 时,应按出错处理。注意选择你认为较好的出错处理方法。
```cpp
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define ARRSIZE 20
#define MAXINT 1000000000
int a[ARRSIZE];
int main() {
a[0] = 1;
cout << a[0];
for (int i = 1; a[i - 1] <= MAXINT / 2 / i; ++i) {
a[i] = a[i - 1] * i * 2;
cout << a[i] << endl;
}
cout << "OVERFLOW";
return 0;
}
```
### 1.20 试编写算法求一元多项式 $P_n(x) = \sum\limits_{i=0}^{n}a_ix^i$ 的值 $P_n(x_0)$,并确定算法中每一语句的执行次数和整个算法的时间复杂度。注意选择你认为较好的输入和输出方法。本题的输入为 $a_i (i = 0, 1, \cdots, n)$ $x_0$ 和 $n$,输出为 $P_n(x_0)$。
```cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n, x, p = 0, tmp = 1;
cin >> n >> x;
int a[n];
if (n < 0) {
cout << "ERROR" << endl;
main();
} // O(1)
for (int i = 0; i <= n; ++i) {
cin >> a[i];
p += a[i] * tmp;
tmp *= x;
} // O(n)
cout << p;
return 0;
}
```

BIN
1.18 Executable file

Binary file not shown.

View File

@@ -1,8 +1,9 @@
#include <iostream>
#include <algorithm>
using namespace std;
typedef enum {A, B, C, D, E} SchoolNmae;
#define MAXSIZE 100
typedef enum {A, B, C, D, E} SchoolName;
typedef enum {FEMALE, MALE} Gender;
typedef struct {
@@ -19,8 +20,16 @@ typedef struct {
int totalSum;
} Sum;
int main() {
while
cin >> item[i].name >> item[i].gender >> item[i].score >> item[i].point;
for (int i = 0; i < MAXSIZE; ++i) {
result[report[i].school].totalSum += report[i].point;
switch (report[i].gender) {
case 0:
result[report[i].school].femaleSum += report[i].point;
case 1:
result[report[i].school].maleSum += report[i].point;
}
}
for (int i = A; i < E; ++i) {
cout << ...
}

BIN
1.19 Executable file

Binary file not shown.

22
1.19.cpp Normal file
View File

@@ -0,0 +1,22 @@
// 试编写算法,计算 $i! \cdot 2^i (i = 0, 1, \cdots, n - 1)$ 的值并分别存入数组 `a[arrsize]` 的各个分量中。假设计算机允许的整数最大值为 `MAXINT`,则当 $n > arrsize$ 或对某个 $k (0 \le k \le n - 1)$ 使 $k! \cdot 2^k > MAXINT$ 时,应按出错处理。注意选择你认为较好的出错处理方法。
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define ARRSIZE 20
#define MAXINT 1000000000
int a[ARRSIZE];
int main() {
a[0] = 1;
cout << a[0];
for (int i = 1; a[i - 1] <= MAXINT / 2 / i; ++i) {
a[i] = a[i - 1] * i * 2;
cout << a[i] << endl;
}
cout << "OVERFLOW";
return 0;
}

BIN
1.20 Executable file

Binary file not shown.

22
1.20.cpp Normal file
View File

@@ -0,0 +1,22 @@
// 试编写算法求一元多项式 $P_n(x) = \sum\limits_{i=0}^{n}a_ix^i$ 的值 $P_n(x_0)$,并确定算法中每一语句的执行次数和整个算法的时间复杂度。注意选择你认为较好的输入和输出方法。本题的输入为 $a_i (i = 0, 1, \cdots, n)$ $x_0$ 和 $n$,输出为 $P_n(x_0)$。
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n, x, p = 0, tmp = 1;
cin >> n >> x;
int a[n];
if (n < 0) {
cout << "ERROR" << endl;
main();
} // O(1)
for (int i = 0; i <= n; ++i) {
cin >> a[i];
p += a[i] * tmp;
tmp *= x;
} // O(n)
cout << p;
return 0;
}