}
while((ch = fgetc(fp)) != EOF) { if( ___(7)___ ) ch++; else if ( ch == 'z' ) ch = a; printf(”%c”, ch); }
___(8)___; printf(”\\n”); }
4.20本程序从键盘输入n(0 < n < 100)个整数,计算并输出其中出现次数最多的元素。当
有多个不同元素有相同的最多出现次数时,选择值更大的元素。 [程序4.2]
#include
{ int a[100], n, i, j, ind, c1, c2;
printf(〞输入n!\\n〞); scanf(〞%d〞, &n); for(i = 0; i < n; i++) scanf(〞%d〞, &a[i]); for(c2 = i = 0; i < n; i++)
{ for(c1 = 1, j = i+1; j < n; j++) if(a[j] == a[i]) ___(3)___ ;
if(c1 > c2 || c1 == c2 && ___(4)___ ) { ___(5)___ ; ind = i; } }
printf(〞其中 %d 出现 %d 次。\\n〞, a[ind], c2); }
4.21本程序是按以下格式输出杨辉三角形的前n(<20)行。
1 1
1
1 2 1
1 3 3 1
1 4 6 4 1 杨辉三角形有以下性质:
① 第一行只有一个元素1。
② 第i(≧2)行有i个元素,它的最左、最右元素为1,中间元素是它上一行(i-1)对应位
置元素与对应位置前一个元素之和。 #include
printf(〞输入n!\\n〞); scanf(〞%d〞, &n); pas[0] = ___(6)___ ;
printf(〞M\\n〞, pas[0]);
21--34 注:解答写在答卷纸上,试卷中的解答不评分
for(i = 2; i <= n; i++)
{ /* 由存储在pas中的第i-1行内容生成第i行内容,并重新存于pas */ pas[i-1] = 1;
for(j = ___(7)___ ; j > 0; j--) pas[j] = ___(8)___; for(j = 0; j < i; j++)
printf(〞M〞, pas[j]); printf(〞\\n〞); } }
4.22本程序求正文文件 st.dat 中不同的整数。程序中用数组b[]存储不同的整数,变量
k 为已存入b[]中的不同整数的个数,设不同整数个数小于1000。 #include
{ FILE *fp; b[N], d, i, k;
if((fp = fopen(___(6)___) == NULL) {
printf(”Can not open file\\n”); return; }
k = 0;
while( fscanf(fp, ”%d”, &d) == 1) { for(b[k] = d,i = 0; b[i] != d; i++); if(___(7)___) k++; }
___(8)___;
for(i = 0; i < k; i++) printf(”m”, b[i]); printf(”\\n”); }
4.23以下程序实现两个多项式相乘。多项式用链表表示,链表上的各表元按多项式的幂指
数降序链接。例如:
f(x) = 5.7x15 + 4.8x6 + 9.65 15 5.7 6 4.8 0 9.65 ∧
设两个多项式f(x)和g(x)分别为
f(x) = fnx + ?? + f1x + f0 g(x) = gmxm + ?? + g1x + g0 其积多项式为
s(x) = f(x)g(x) = skxk + ?? + s1x + s0 其中k = n+m,si = ∑fs*gt (0<= i <= k)
22--34 注:解答写在答卷纸上,试卷中的解答不评分
n
s+t = i
#include
typedef struct elem { int index; double coef; struct elem *next; }POLYNODE;
write(POLYNODE *g) { POLYNODE *p = g;
while (p) { printf(\” , p->coef);
if (p->index) printf(”*x^%d\
if (p->next && p->next->coef > 0) printf(”+”); p = p->next; }
printf(\ }
main()
{ POLYNODE *f, *g, *s, *inpoly(), *polymul();
f = inpoly(); g = inpoly(); s = polymul(f, g); write(s); }
POLYNODE *reverse(POLYNODE *g)
{ POLYNODE *u = NULL, *v = g, *w;
while(v) { w = v->next; v->next = u; u = v; v = w; } return u; }
POLYNODE *polymul(POLYNODE *f, POLYNODE *g)
{ POLYNODE *fp, *gp, *tail, *p = NULL, *q; int i, maxindex; double temp;
maxindex = f->index + g->index; g = reverse(g); for(i = maxindex; i >= 0; i--) { fp = f; gp = g;
while (fp != NULL && fp->index > i) fp = fp->next;
while (gp != NULL && gp->index < i - fp->index) gp = gp->next; temp = 0.0;
while(fp && gp)
if (fp->index + gp->index == i) {
temp += fp->coef * gp->coef; fp = fp->next; gp = gp->next; }
else if (fp->index + gp->index > i) fp = fp->next; else gp = gp->next; if (temp != 0.0) {
q = (POLYNODE *)malloc(sizeof(POLYNODE)); q->index = i; q->coef = temp; q->next = NULL;
23--34 注:解答写在答卷纸上,试卷中的解答不评分
if (p == NULL) p = q; else tail->next = q; tail = q; } }
g = reverse(g); return p; }
POLYNODE *inpoly()
{ POLYNODE *u, *v, *h = NULL, *p; int index; double coef; printf(\ while (index >= 0) {
printf(\ p = (POLYNODE *)malloc(sizeof(POLYNODE)); p->index = index; p->coef = coef; v = h;
while(v != NULL && index < v->index) { u = v; v = v->next; }
if (v == NULL || index > v->index) { p->next = v;
if (v == h) h = p; else u->next = p; } else v->coef += coef;
printf(\ }
return h; }
4.24函数merge(int a[], int n, int b[], int m, int *c)是将两个从小到大有序数组
a和b复制合并出一个有序整数序列c,其中形参n和m分别是数组a和b的元素个数。
void merge(int a[], int n, int b[], int m, int *c) { int i, j;
for(i = j = 0; i < n && j < m;)
*c++ = a[i] < b[j] ? a[i++] : b[j++]; while (___(1)___) *c++ = a[i++]; while (___(2)___) *c++ = b[j++]; }
4.25以下函数encode()和decode()分别实现对字符串的变换和复原。变换函数encode()
顺序考察已知字符串的字符,按以下规则生成新字符串:
(1) 若已知字符串的当前字符不是数字字符,则复制该字符于新字符串中。
(2) 若已知字符串的当前字符是一个数字字符,且它之后没有后继字符,则简单地将它自已复制到新字符串。
(3) 若已知字符串的当前字符是一个数字符,并且还有后继字符,假设这个数字符的面值为n,则将它的后继字符(包括后继字符是一个数字符)重复复制n+1次到新字符串。 (4) 以上述一次变换为一组,在不同组之间另插入一个下划线字符‘_‘用于分隔。
24--34 注:解答写在答卷纸上,试卷中的解答不评分
例如:encode()函数对字符串 26a3t2 的变换结果为 666_a_tttt_2
复原函数decode()做变换函数encode()相反的工作。即复制不连续相同的单个字符,而将一组连续相同的字符(不超过10个)变换成一个用于表示重复次数的数字字符和一个重复出现的字符,并在复原过程中掠过变换函数为不同组之间添加的一个下划线字符。
假定调用变换函数encode()时的已知字符串中不包含下划线字符。 int encode(char *instr, char *outstr) {
char *ip, *op, c; int k, n; ip = instr; op = outstr; while (*ip) {
if (*ip >= ?0? && *ip <= ?9? && *(ip+1) != ?\\0?) { n = ___(1)___ ; c = ___(2)___ ;
for(k = 0; k < n; k++) *op++ = c; } else ___(3)___ ; *op++ = ?_?; ip++; }
if (op > outstr) op--; ___(4)___ ;
return op - outstr; }
int decode(char *instr, char *outstr) { char *ip, *op, c; int n; ip = instr; op = outstr; while (*ip) {
c = *ip; n = 0;
while (*ip == c && n < 10) { ip++; n++; }
if(___(5)___)
*op++ = ?0? + n - 1; *op++ = c;
if(___(6)___) ip++; }
*op = ?\\0?; return op - outstr; }
4.26以下程序用古典的Eratosthenes的筛法求从2起到指定范围内的素数。如果要找出
2至10中的素数,开始时筛中有2到10的数,然后取走筛中的最小的数2,宣布它是素数,并把该素数的倍数都取走。这样,第一步以后,筛子中还留下奇数3、5、7、9;重复上述步骤,再取走最小数3,宣布它为素数,并取走3的倍数,于是留下5、7。反复重复上述步
25--34 注:解答写在答卷纸上,试卷中的解答不评分