一、將三個整數按升序重新排列
【題目】試寫一算法,如果三個整數a,b和c的值不是依次非遞增的,則通過交換,令其為非遞增。
要求實現下列函數:
void Descend(int &a, int &b, int &c);
/* 通過交換,令 a >= b >= c */
#include "allinclude.h" //DO NOT edit this line
void Descend(int &a, int &b, int &c) { // 通過交換,令 a >= b >= c
// Add your code here
int t,max;
//先找出abc中的最大值
max = (((a>=b)?a:b)>=c)?((a>=b)?a:b):c;
if(max==a){
if(b<c){
t = b;
b = c;
c = t;
}
}
if(max==b){
if(a>=c){
t = a;
a = b;
b = t;
}else{
t = b;
b = c;
c = a;
a = t;
}
}
if(max==c){
if(b>=a){
t = a;
a = c;
c = t;
}else{
t = c;
c = b;
b = a;
a = t;
}
}
}
二、求一元多項式的值
【題目】試編寫算法求一元多項式 P(x) = a0 + a1x + a2x^2 + ... + anx^n的值P(x0),並確定算法中每一語句的執行次數和整個算法的時間復雜度。
要求實現下列函數:
float Polynomial(int n, int a[], float x0);
/* 求一元多項式的值P(x0)。 /
/ 數組a的元素a[i]為i次項的系數,i=0,1,...,n */
#include "allinclude.h" //DO NOT edit this line
float Polynomial(int n, int a[], float x0){
// Add your code here
float p = 0;
float t = x0;
if(n)
for(int i = 1;i<=n;i++){
p += a[i]*t;
t = t*x0;
}
return a[0]+p;
}
三、求k階裴波那契序列的第m項的值
【題目】已知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,則返回OK;/
/ 否則(比如,參數k和m不合理)返回ERROR */
#include "allinclude.h" //DO NOT edit this line
Status Fibonacci(int k, int m, int &f) {
// Add your code here
int a[100],sum;
//異常處理
if(k < 2||m < 0) return ERROR;
if(m < k-1) f = 0;
else{
if(m == k-1) f = 1;
else{
//前k-1項都為0
for(int i = 0;i < k-1;i++) a[i] = 0;
//第k-1項為1
a[k-1] = 1;
//前k項和為下一項
for(int j = k;j <= m;j++){
sum = 0;
for(int i = j-k;i < j;i++) sum += a[i];
a[j] = sum;
}
f = a[m];
}
}
return OK;
}
四、計算i!×2^i的值
【題目】試編寫算法,計算i!×2^i的值並存入數組a[0..n-1]的第i-1個分量中 (i=1,2,…,n)。假設計算機中允許的整數最大值為MAXINT,則當對某個k(1≤k≤n)使k!×2^k>MAXINT時,應按出錯處理。注意選擇你認為較好的出錯處理方法。
要求實現下列函數:
Status Series(int a[], int n);
/* 求i!*2^i序列的值並依次存入長度為n的數組a;若所有 /
/ 值均不超過MAXINT,則返回OK,否則返回EOVERFLOW */
#include "allinclude.h" //DO NOT edit this line
Status Series(int a[], int n) {
// Add your code here
if(n <= 0) return ERROR;
int l,m;
l = 1;
m = 2;
for(int i = 1;i <= n;i++){
l *= i;
a[i-1] = l*m;
m *= 2;
if(l > MAXINT||m > MAXINT||l*m > MAXINT) return EOVERFLOW;
}
return OK;
}
五、由一維數組構建一個序列
【題目】試寫一算法,由長度為n的一維數組a構建一個序列S。
要求實現下列函數:
Status CreateSequence(Sequence &S, int n, ElemType a);
/ 由長度為n的一維數組a構建一個序列S,並返回OK。 /
/ 若構建失敗,則返回ERROR */
序列的定義為:
typedef struct {
ElemType *elem;
int length;
} Sequence;
#include "allinclude.h" //DO NOT edit this line
Status CreateSequence(Sequence &S, int n, ElemType *a) {
S.elem = a;
if(NULL == S.elem||n <= 0) return ERROR;
S.length = n;
return OK;
}
六、構建一個值為x的結點
【題目】鏈表的結點和指針類型定義如下
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
試寫一函數,構建一個值為x的結點。
要求實現下列函數:
LinkList MakeNode(ElemType x);
/* 構建一個值為x的結點,並返回其指針。 /
/ 若構建失敗,則返回NULL。 */
#include "allinclude.h" //DO NOT edit this line
LinkList MakeNode(ElemType x) {
// Add your code here
LinkList p;
p = (LinkList)malloc(sizeof(LNode));//sizeof里面最好不要寫LinkList
If(! p) return NULL;
p->data = x;
return p;
}
七、構建長度為2且兩個結點的值依次為x和y的鏈表
【題目】鏈表的結點和指針類型定義如下
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
試寫一函數,構建長度為2且兩個結點的值依次為x和y的鏈表。
要求實現下列函數:
LinkList CreateLinkList(ElemType x, ElemType y);
/* 構建其兩個結點的值依次為x和y的鏈表。/
/ 若構建失敗,則返回NULL。 */
#include "allinclude.h" //DO NOT edit this line
LinkList CreateLinkList(ElemType x, ElemType y) {
// Add your code here
LNode *p1;
LNode *p2;
p1 = (LNode *)malloc(sizeof(LNode));
p2 = (LNode *)malloc(sizeof(LNode));
if(!p1||!p2) return NULL;
p1->data = x;
p1->next = p2;
p2->data = y;
p2->next = NULL;
return p1;
}
八、構建長度為2的升序鏈表
【題目】鏈表的結點和指針類型定義如下
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
試寫一函數,構建長度為2的升序鏈表,兩個結點的值
分別為x和y,但應小的在前,大的在后。
要求實現下列函數:
LinkList CreateOrdLList(ElemType x, ElemType y);
/* 構建長度為2的升序鏈表。 /
/ 若構建失敗,則返回NULL。 */
#include "allinclude.h" //DO NOT edit this line
LinkList CreateOrdLList(ElemType x, ElemType y) {
// Add your code here
LNode *p1,*p2;
p1 = (LNode *)malloc(sizeof(LNode));
p2 = (LNode *)malloc(sizeof(LNode));
If(!p1||!p2) return NULL;
ElemType t;
if(x > y){
t = x;
x = y;
y = t;
}
p1->data = x;
p1->next = p2;
p2->data = y;
p2->next = NULL;
return p1;
}
