diff --git a/1 绪论.md b/1 绪论.md index 96b9a8e..402898a 100644 --- a/1 绪论.md +++ b/1 绪论.md @@ -123,5 +123,89 @@ int main() { ### 编写算法,处理上述表格,以统计各院校的男女总分和团体总分,并输出。 ```cpp +#include +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 +#include +#include +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 +#include +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; +} +``` \ No newline at end of file diff --git a/1.18 b/1.18 new file mode 100755 index 0000000..f8f6e97 Binary files /dev/null and b/1.18 differ diff --git a/1.18.cpp b/1.18.cpp index d9c2f74..44e1f9d 100644 --- a/1.18.cpp +++ b/1.18.cpp @@ -1,8 +1,9 @@ #include -#include 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 << ... } diff --git a/1.19 b/1.19 new file mode 100755 index 0000000..21cc27e Binary files /dev/null and b/1.19 differ diff --git a/1.19.cpp b/1.19.cpp new file mode 100644 index 0000000..71e51f1 --- /dev/null +++ b/1.19.cpp @@ -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 +#include +#include +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; +} \ No newline at end of file diff --git a/1.20 b/1.20 new file mode 100755 index 0000000..50f86b2 Binary files /dev/null and b/1.20 differ diff --git a/1.20.cpp b/1.20.cpp new file mode 100644 index 0000000..ff7f213 --- /dev/null +++ b/1.20.cpp @@ -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 +#include +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; +} \ No newline at end of file