1.16 试写一算法,自大到小依次输出顺序读入的三个整数X,Y和Z的值。
// 文件名:1_16.c #include
void order(int a, int b, int c){ printf("In descending order:\r\n"); printf("orgin: a=%d, b=%d, c=%d\r\n", a, b, c); if(a < b){ a += b; b = a - b; a = a - b; } if(b < c){ b += c; c = b - c; b = b - c; } if(a < b){ a += b; b = a - b; a = a - b; } printf("sorted: a=%d, b=%d, c=%d\r\n", a, b, c); } int main(){ int a = 11; int b = 99; int c = 66; order(a, b, c); return 0; } # 运行结果 ubuntu@ubuntu:~/work/c/algo$ vim 1_16.c ubuntu@ubuntu:~/work/c/algo$ gcc 1_16.c ubuntu@ubuntu:~/work/c/algo$ ./a.out In descending order: orgin: a=11, b=99, c=66 sorted: a=99, b=66, c=11 ubuntu@ubuntu:~/work/c/algo$
1.17 已知k阶斐波那契序列的定义为
f0=0, f1=0, …, fk-2=0, fk-1=1;
fn=fn-1+fn-2+…+fn-k, n=k,k+1,…
试编写求k阶斐波那契序列的第m项值的函数算法,k和m均以值调用的形式在函数参数表中出现。
// 注:K阶斐波那契数列的前K-1项均为0,第k项为1,以后的每一项都是前K项的和 // 文件名:1_17.c #include
// 求值(递归) int k_fib(int k, int m){ // 因为第 k-1 项为0,所以此处 k 最小取2 if((m<0) || (k<2)){ return -1; } int i, v; if((k-1) > m){ return 0; }else if((k-1) == m){ return 1; }else{ for(i=1, v=0; i<=k; i++){ v += k_fib(k, m-i); } return v; } } int main(){ int k, m, v; // 1. k < m k = 4; m = 7; v = k_fib(k, m); if(v < 0){ printf("error, please check the value of k and m."); }else{ printf("if k=%d, m=%d; fib(m) = %d\r\n", k, m, v); } // 2. k == m k = 11; m = 11; v = k_fib(k, m); if(v < 0){ printf("error, please check the value of k and m."); }else{ printf("if k=%d, m=%d; fib(m) = %d\r\n", k, m, v); } // 3. k > m k = 11; m = 5; v = k_fib(k, m); if(v < 0){ printf("error, please check the value of k and m."); }else{ printf("if k=%d, m=%d; fib(m) = %d\r\n", k, m, v); } return 0; } # 运行结果 ubuntu@ubuntu:~/work/c/algo$ vim 1_17.c ubuntu@ubuntu:~/work/c/algo$ gcc 1_17.c ubuntu@ubuntu:~/work/c/algo$ ./a.out if k=4, m=7; fib(m) = 8 if k=11, m=11; fib(m) = 1 if k=11, m=5; fib(m) = 0 ubuntu@ubuntu:~/work/c/algo$
拓展:斐波那契数列
1.18 假设有A、B、C、D、E五个高等院校进行田径对抗赛,各院校的单项成绩均已存入计算机,并构成一张表,表中每一行的形式为
项目名称 性别 校名 成绩 得分 编写算法,处理上述表格,以统计各院校的男、女总分和团体总分,并输出。
// 注:首先结构化表格,采集数据,根据定义的数据结构计算目标值 // 文件名:1_18.c #include
#include #include #include typedef enum{ FEMALE, MALE }SexType; typedef enum{ A, B, C, D, E }SchoolName; typedef struct{ char sport; // 项目名称 int gender; // 性别(女:0;男:1) SchoolName school_name; // 校名为'A','B','C','D'或'E' char result; // 成绩 int score; // 得分 }ResultType; typedef struct{ int male_score; // 男子总分 int female_score; // 女子总分 int total_score; // 男女团体总分 }ScoreType; void Scores(ResultType *result, ScoreType *score, int num){ int i = 0; for(i=0; i<num; i++){ int n = result[i].school_name + 1; score[n].total_score += result[i].score; if(result[i].gender == 1){ score[n].male_score += result[i].score; }else{ score[n].female_score += result[i].score; } } } int main(){ int i, n; ResultType *result; ScoreType score[5] = {0}; printf("请输入各院校参赛的总人数:"); scanf("%d", &n); result = malloc(n * sizeof(ResultType)); memset(result, 0, n*sizeof(ResultType)); printf("请输入各院校运动员的项目名称、性别、校名、成绩、得分:\n"); int school_type; for(i=0; i<n; i++){ setbuf(stdin, NULL); printf("< 运动员%d >\n", i+1); setbuf(stdin, NULL); printf("输入项目:\n"); scanf("%c", &result[i].sport); setbuf(stdin, NULL); printf("输入性别:\n"); scanf("%d", &result[i].gender); setbuf(stdin, NULL); printf("输入校名:\n"); scanf("%d", &school_type); result[i].school_name = school_type; setbuf(stdin, NULL); printf("输入结果:\n"); scanf("%c", &result[i].result); setbuf(stdin, NULL); printf("输入分数:\n"); scanf("%d", &result[i].score); printf("GET: %c %d %d %c %d\r\n", result[i].sport, result[i].gender, result[i].school_name, result[i].result, result[i].score); } Scores(result, score, n); for(i=0; i<5; i++){ printf("the school %d: \r\n", result[i].school_name) ; printf("Total score of male: %d\r\n", score[i].male_score); printf("Total score of female: %d\r\n", score[i].female_score); printf("Total score of all: %d\r\n\n", score[i].total_score); } return 0; } # 运行结果 ubuntu@ubuntu:~/work/c/algo$ gcc 1_18.c ubuntu@ubuntu:~/work/c/algo$ ./a.out 请输入各院校参赛的总人数:2 请输入各院校运动员的项目名称、性别、校名、成绩、得分: < 运动员1 > 输入项目: A 输入性别: 0 输入校名: 1 输入结果: S 输入分数: 88 GET: A 0 1 S 88 < 运动员2 > 输入项目: B 输入性别: 1 输入校名: 1 输入结果: S 输入分数: 99 GET: B 1 1 S 99 the school 1: Total score of male: 0 Total score of female: 0 Total score of all: 0 the school 1: Total score of male: 0 Total score of female: 0 Total score of all: 0 the school 0: Total score of male: 99 Total score of female: 88 Total score of all: 187 the school 0: Total score of male: 0 Total score of female: 0 Total score of all: 0 the school 0: Total score of male: 0 Total score of female: 0 Total score of all: 0 ubuntu@ubuntu:~/work/c/algo$
*1.19 试编写算法,计算i!*2i的值并存入数组a[0…arrsize-1]的第i-1个分量中(i=1,2,…,n)。假设计算机中允许的整数最大值为maxint,则当n>arrsize或对某个k(1≤k≤n)使k!2k>maxint时,应按出错处理。注意选择你认为较好的出错处理方法。
// 注:重点是错误返回和处理 // 文件名:1_19.c #include
#include #define OVERFLOW (-2) #define ERROR (-1) #define OK 0 #define arrsize 100 #define max_int INT_MAX int calc(int i, int *arr){ if(i<1 || i>arrsize){ return ERROR; } printf("calc %d!*2^%d :\r\n", i, i); int j; for(j=1; j<i; j++){ if(j == 1){ arr[j-1] = 2; }else{ if(max_int/(1*j) < arr[j-2]){ return OVERFLOW; } arr[j-1] = 2 * j * arr[j-2]; } } printf("< i = %d, i!*2^i > arr[i-1] = %d\r\n", i, arr[j-1]); return OK; } int main(){ int i, res[arrsize]; i = 6; int ret = calc(i, res); if(ret == OK){ printf("OK!\r\n"); }else if(ret == OVERFLOW){ printf("overflow.\r\n"); }else if(ret == ERROR){ printf("error.\r\n"); }else{ printf("unknown error.\r\n"); } return 0; } # 运行结果 ubuntu@ubuntu:~/work/c/algo$ vim 1_19.c +27 ubuntu@ubuntu:~/work/c/algo$ gcc 1_19.c ubuntu@ubuntu:~/work/c/algo$ ./a.out calc 6!*2^6 : < i = 6, i!*2^i > arr[i-1] = 32767 OK! ubuntu@ubuntu:~/work/c/algo$
1.20 试编写算法求一元多项式的值Pn(x),并确定算法中每一语句的执行次数和整个算法的时间复杂度。注意选择你认为较好的输入和输出方法。本题的输入为ai(i=0,1,…,n),x0和n,输出为Pn(x0)。
// 注:多项式求和,重点是计算次数和时间复杂度 // 文件名:1_20.c #include
#include int calc(int *a, int x, int n){ int i, res; for(i=1, res=0; i<=n; i++){ res += a[i-1]*pow(x, i-1); // 执行 n 次,时间复杂度O(n) } return res; } int main(){ int a[8] = {3, 4, 8, -2, 1, 5, 7, 6}; int n = sizeof(a)/sizeof(a[0]); int x = 2; printf("P%d(%d) = %d\r\n", n, x, calc(a, x, n)); return 0; } # 运行结果 ubuntu@ubuntu:~/work/c/algo$ vim 1_20.c ubuntu@ubuntu:~/work/c/algo$ gcc 1_20.c -lm ubuntu@ubuntu:~/work/c/algo$ ./a.out P8(2) = 1419 ubuntu@ubuntu:~/work/c/algo$
—— 2018-11-27 ——