现在的位置: 首页 > 14 二级C语言 > 正文

2010年秋 阅读程序 题目解析

2011年09月13日 14 二级C语言 ⁄ 共 159字 ⁄ 字号 暂无评论

第1页是真题,第2页是简要的解答提示,第3页是解题分析和答案。强烈建议先尝试自己完成,或者借助第2页的解答提示来完成,实在不明白再看答案。

第6~13题

6 以下程序运行时输出到屏幕的结果为___(6)___

1
2
3
4
5
6
7
8
9
#include <stdio.h>
 
enum {A,B,C,D} x;
void main()
{
    char s[]="your";
    for(x=B; x<=D; x++)
        putchar(s[x]);
}

7 以下程序运行时输出到屏幕的结果是 (7)

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
 
void main()
{
    int s[12]= {1,2,3,4,4,3,2,1,1,1,2,3},c[5]= {0},i,j;
    for(i=0; i<12; i++) {
        j=s[i];
        c[j]++;
    }
    for(i=1; i<5; i++)
        printf("%d",c[i]);
}

8 以下程序运行时输出到屏幕的结果是___(8)___

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
 
int fun(int x)
{
    static int c,y;
    if(c==0) y=1;
    else y=y+x;
    c++;
    return y;
}
void main()
{
    int i;
    for(i=0; i<2; i++)
        printf("%d",fun(2));
}

9 以下程序运行时输出到屏幕的结果中第一行是___(9)___,第二行是___(10)___

1
2
3
4
5
6
7
8
9
10
11
12
13
void fun(int *a,int b)
{
    while(b>0) {
        *a+=b;
        b--;
    }
}
void main()
{
    int x=0,y=3;
    fun(&x,y);
    printf("%d\n%d\n",x,y);
}

10 以下程序运行时输出到屏幕的结果是___(11)___

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
 
int f(int x)
{
    if(x==0||x==1)return 3;
    return x*x-f(x-2);
}
void main()
{
    printf("%d\n",f(3));
}

11 以下程序运行时输出到屏幕的结果中第一行是___(12)___,第二行是___(13)___

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>
#include <ctype.h>
void compute(char *s)
{
    int t,r;
    char op;
    for(r=0; isdigit(*s); s++)
        r=r*10+*s-'0';
    while(*s) {
        op=*s++;
        /*isdigit(*s)判断s指向的字符是否为数字字符*/       
        for(t=0; isdigit(*s); s++)
            t=t*10+*s-'0';
        switch(op) {
        case '+':
            r=r+t;
            break;
        case '-':
            r=r-t;
            break;
        case '*':
            r=r*t;
            break;
        case '/':
            if(t) r=r/t;
            else {
                puts("devide error");
                return;
            }
        }
    }
    printf("%d\n",r);
}
 
void main()
{
    compute("12+6-19+2");
    compute("12/6*19/2");
}

12 以下程序运行时输出到屏幕的结果中第一行是(14),第二行是 (15),第三行是 (16)
算法提示:将Fibonacci数列(1,1,2,3,5,8,13,...) 前6个数填入a数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
#define N 3
void main()
{
    int i,j,a[N][N]= {1},f1=0,f2=1,temp;
    for(i=1; i<N; i++)
        for(j=0; j<=i; j++) {
            if(j<=i) {
                a[i][j]=f1+f2;
                temp=f1;
                f1=f2;
                f2=f1+temp;
            }
        }
    for(i=0; i<N; i++) {
        for(j=0; j<N; j++)
            printf("%5d",a[i][j]);
        printf("\n");
    }
}

13 以下程序运行时输出到屏幕的结果中第一行是___(17)___,第二行是___(18)___

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
typedef struct {
    int dm; /*产品代码*/
    char *mc;   /*产品名称*/
    long je;   /*金额*/
} PRO;
 
void main()
{
    int i,j,k,n=3;
    PRO sell[10]= {{101,"apple",100},{301,"orange",100},{101,"apple",200}},xy;
    for(i=0; i<n-1; i++) {
        k=i;
        for(j=i+1; j<n; j++)
            if(sell[k].dm<sell[j].dm||sell[k].dm==sell[j].dm&&sell[k].je<sell[j].je)
                k=j;
        if(k!=i) {
            xy=sell[i]; sell[i]=sell[k]; sell[k]=xy;
        }
    }
    for(i=0; i<n; i++)
        printf("%15d%10s%5d\n",sell[i].dm,sell[i].mc,sell[i].je);
}

抱歉!评论已关闭.