/**********
【题目】试写一算法,如果三个整数a,b和c的值
不是依次非递增的,则通过交换,令其为非递增。
***********/
void Descend(int &a, int &b, int &c)
/* 通过交换,令 a >= b >= c */
{
if (a < b) swap(a, b);
if (a < c) swap(a, c);
if (b < c) swap(b, c);
}
/**********
【题目】试编写算法求一元多项式
P(x) = a0 + a1x + a2x^2 + ... + anx^n
的值P(x0),并确定算法中每一语句的执行次数和整个算法
的时间复杂度。
**********/
float pow(float base, int exp); // 指数函数
float Polynomial(int n, int a[], float x)
/* 求一元多项式的值P(x)。 */
/* 数组a的元素a[i]为i次项的系数,i=0,...,n */
{
float result = 0;
while (n != 0)
{
result += a[n] * pow(x, n);
n--;
}
result += a[0];
return result;
}
float pow(float base, int exp)
{
float result = 1;
while (exp != 0)
{
result *= base;
exp--;
}
return result;
}
/**********
【题目】已知k阶裴波那契序列的定义为
f(0)=0, f(1)=0, ..., f(k-2)=0, f(k-1)=1;
f(n)=f(n-1)+f(n-2)+...+f(n-k), n=k,k+1,...
试编写求k阶裴波那契序列的第m项值的函数算法,
k和m均以值调用的形式在函数参数表中出现。
**********/
Status Fibonacci(int k, int m, int &f)
/* 求k阶斐波那契序列的第m项的值f */
{
if (k < 2 || m < 0) {
return ERROR;
}
if (m < k-1) {
f = 0;
return OK;
}
if (m == k-1) {
f = 1;
return OK;
}
int Fib[100], i, j, temp=0;
for (i=0; i<k-1; i++) {
Fib[i] = 0;
}
Fib[k-1] = 1;
for (i=k; i<=m; i++) {
temp = 0;
for (j=i-k; j<i; j++) {
temp += Fib[j];
Fib[i] = temp;
}
}
f = Fib[m];
return OK;
}
/**********
【题目】试编写算法,计算i!×2^i的值并存入数组
a[0..n-1]的第i-1个分量中 (i=1,2,…,n)。假设计
算机中允许的整数最大值为MAXINT,则当对某个k
(1≤k≤n)使k!×2^k>MAXINT时,应按出错处理。注意
选择你认为较好的出错处理方法。
**********/
float pow(float base, int exp); // 指数函数
int factorial(int integer); // 阶乘函数
Status Series(int a[], int n)
/* 求i!*2^i序列的值并依次存入长度为n的数组a; */
/* 若所有值均不超过MAXINT,则返回OK,否则OVERFLOW */
{
int left = 0, right = 0, sum = 0;
for (int i=1; i<=n; i++) {
sum = 0;
left = factorial(i);
right = pow(2, i);
sum = left * right;
if (left>MAXINT || right>MAXINT || sum>MAXINT) {
return OVERFLOW;
}
a[i-1] = left * right;
}
return OK;
}
float pow(float base, int exp)
{
float result = 1;
while (exp != 0)
{
result *= base;
exp--;
}
return result;
}
int factorial(int integer)
{
int result = 1;
while (integer != 0) {
result *= integer;
integer--;
}
return result;
}
/**********
【题目】假设有A、B、C、D、E五个高等院校进行田径对抗赛,
各院校的单项成绩均以存入计算机并构成一张表,表中每一行
的形式为:
项目名称 性别 校名 成绩 得分
编写算法,处理上述表格,以统计各院校的男、女总分和团体
总分,并输出。
**********/
void Scores(ResultType *result, ScoreType *score)
/* 求各校的男、女总分和团体总分, 并依次存入数组score */
/* 假设比赛结果已经储存在result[ ]数组中, */
/* 并以特殊记录 {"", male, ' ', "", 0 }(域scorce=0)*/
/* 表示结束 */
{
while (result->score != 0) {
if (result->gender == female) {
score[result->schoolname - 'A'].femalescore += result->score;
}
else if (result -> gender == male) {
score [result->schoolname - 'A'].malescore += result->score;
}
score[result->schoolname - 'A'].totalscore += result->score;
++result;
}
}
/**********
【题目】试写一算法,对序列S的第i个元素赋以值e。
序列的类型定义为:
typedef struct {
ElemType *elem;
int length;
} Sequence;
***********/
Status Assign(Sequence &S, int i, ElemType e)
/* 对序列S的第i个元素赋以值e,并返回OK。 */
/* 若S或i不合法,则赋值失败,返回ERROR */
{
if (i>=S.length || S.elem==NULL) return ERROR;
S.elem[i] = e;
return OK;
}
/**********
【题目】试写一算法,由长度为n的一维数组a构建一个序列S。
序列的类型定义为:
typedef struct {
ElemType *elem;
int length;
} Sequence;
***********/
Status CreateSequence(Sequence &S, int n, ElemType *a)
/* 由长度为n的一维数组a构建一个序列S,并返回OK。 */
/* 若构建失败,则返回ERROR */
{
if (n < 1) return ERROR;
S.elem = (ElemType*)malloc(n * sizeof(ElemType));
S.length = n;
S.elem = a;
return OK;
}
/**********
【题目】链表的结点和指针类型定义如下
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
试写一函数,构建长度为2且两个结点的值依次为x和y的链表。
**********/
LinkList CreateLinkList(ElemType x, ElemType y)
/* 构建其两个结点的值依次为x和y的链表。*/
/* 若构建失败,则返回NULL。 */
{
LinkList node;
node = (LinkList)malloc(sizeof(LNode));
if (node==NULL) return NULL;
else {
node->data = x;
node->next = (LinkList)malloc(sizeof(LNode));
if (node->next==NULL) {
free(node);
return NULL;
}
node->next->data = y;
}
return node;
}
/**********
【题目】链表的结点和指针类型定义如下
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
试写一函数,构建长度为2的升序链表,两个结点的值
分别为x和y,但应小的在前,大的在后。
**********/
LinkList CreateOrdLList(ElemType x, ElemType y)
/* 构建长度为2的升序链表。 */
/* 若构建失败,则返回NULL。 */
{
LinkList node;
node = (LinkList)malloc(sizeof(LNode));
if (node==NULL) return NULL;
else {
node->data = x < y ? x : y;
node->next = (LinkList)malloc(sizeof(LNode));
if (node->next==NULL) {
free(node);
return NULL;
}
else node->next->data = x > y ? x : y;
}
return node;
}