2010軟件水平考試程序員考試模擬試題及答案(1)

字號:

2010軟件水平考試程序員考試模擬試題及答案(1)

    本試卷共8道題,考生需解答5道題,其中試題1~試題3是必答題,試題4和試題5兩題任選1道題,試題6至試題8三題任選1道解答。對于任選題,如果解答的試題數(shù)超過1道,則題號小的1道題解答有效。每題15分,滿分75分。
    試題一(15分,每空3分)
    閱讀以下說明和流程圖,將應(yīng)填入__(n)__處的字句寫在答題紙的對應(yīng)欄內(nèi)。
    [說明]
    已知頭指針分別為La和Lb的有序單鏈表,其數(shù)據(jù)元素都是按值非遞減排列?,F(xiàn)要歸并La和Lb得到單鏈表Lc,使得Lc中的元素按值非遞減排列。程序流程圖如下所示。
    試題二(15分,每空3分)
    閱讀以下函數(shù)說明和C語言函數(shù),將應(yīng)填入___(n)___處的字句寫在答題紙的對應(yīng)欄內(nèi)。
    [程序2.1說明]
    已知一個排好序的數(shù)組,現(xiàn)輸入一個數(shù),要求按原來的順序規(guī)律,將它插入到數(shù)組中。
    [程序2.1]
    #include
    #define N 100
    void main()
    {
    float a[N=1],x;
    int i,p;
    printf("輸入已經(jīng)排好序的數(shù)列:");
    for( i=0; i
    scanf(%f",&a[i]);
    printf("輸入要插入的數(shù):");
    scanf("%f",&x);
    for( i=0,p=N; i
    if(x
    {
    ___(1)___
    break;
    }
    for( i=N-1; i>=p; i-- )
    ___(2)___
    ___(3)___
    for( i=0; i<=N; i++ )
    printf("%f\t",a[i]);
    }
    [程序2.2說明]
    本程序用變量count統(tǒng)計文件中字符的個數(shù)。
    [程序2.2]
    #include
    #include
    void main()
    {
    FILE *fp;
    long count=0;
    if( (fp=fopen("letter.txt","r") ) == NULL )
    {
    printf("can not open file\n");
    exit(0);
    }
    while( !feof(fp) )
    {
    ___(4)___
    count++;
    }
    printf("count=%d\n",count);
    ___(5)___
    }
    試題三(15分,每空3分)
    閱讀以下說明和C語言程序,將應(yīng)填入___(n)___處的字句寫在答題紙的對應(yīng)欄內(nèi)。
    [說明]
    Fibonacci數(shù)列
    列A={11,2,3,5,8,…}有如下性質(zhì):
    a0=a1=1
    ai=ai-1+ai-2 i>1
    對于給定的n,另外有一個由n個元素組成的數(shù)列xn,該數(shù)列中各元素的值為:
    xi=ai/ai+1 i=0,1,…,n
    現(xiàn)要求對xn中的元素按升序進行排序,然后以分數(shù)形式輸出排序后的xn。例如n=5時,
    排序前的xn={1/1,1/2,2/3,3/5,5/8},排序后的xn={1/2,3/5,5/8,2/3,1/1}。程序中函數(shù)
    make()首先生成排序前的xn,然后調(diào)用函數(shù)sort()進行排序,最后輸出所求結(jié)果。
    [程序]
    #include
    #include
    #include
    struct fact
    {
    long m,n;
    };
    void sort(int n,struct fact *p)
    {
    int a;
    long s,t,u,v;
    struct fact *q,*end;
    for(end=p+(n-1),a=1;a;end--)
    for(a=0,q=p;q
    {
    s=q->m;
    t=q->n;
    u=(q+1)->m;
    v=(q+1)->n;
    if(___(1)___)
    {
    q->m=u;
    ___(2)___
    ___(3)___
    (q+1)->n=t;
    a=1;
    }
    }
    }
    void make(int n)
    {
    int i;
    long a,b,c;
    struct fact *x,*y;
    x=(struct fact *)malloc(sizeof(struct fact)*n);
    x->m=1;
    x->n=1;
    for( a=1,b=1,i=2;i<=n;i++)
    {
    ___(4)___
    a=b;
    b=c;
    (x+(i-1))->m=a;
    (x+(i-1))->n=b;
    }
    ___(5)___
    printf("x%d={/",n,x->m,x->n);
    for(y=x+1;y
    printf(",/",y->m,y->n);
    printf("}\n");
    free(x);
    }
    void main()
    {
    int n;
    printf("input n:");
    scanf("%d",&n);
    make(n);
    }
    試題四(15分,每空3分)
    閱讀以下說明和C語言程序,將應(yīng)填入___(n)___處的字句寫在答題紙的對應(yīng)欄內(nèi)。
    [說明]
    本程序?qū)δ畴姶a文(原文)進行加密形成密碼文,其加密算法如下:
    假定原文為C1,C2,C3,…,Cn加密后形成的密文為S1,S2,S3,…,Sn,首先讀入正整數(shù)
    key(key>1)作為加密鑰匙,并將密文字符位置按順時針方向連成一個環(huán),如下圖所示:
    加密時從 S1 位置起順時針計數(shù),當數(shù)到第 key 個字符位置時,將原文中的字符放入該密文字符位置中,同時從環(huán)中除去該字符位置;接著從環(huán)中下一個字符位置起繼續(xù)計數(shù),當再次數(shù)到第 key 個字符位置時,將原文中字符 C2 放入其中,并從環(huán)中除去該字符位置;依次類推,直至 n 個原文字符全部放入密文環(huán)中。由此產(chǎn)生的 S1S2...Sn 即為原文的密文。
    例如,當 Key=3 時,原文:this is a decoding system 的密文為:
    aotgnhedi ys d imietsnc ss
    當Key=4時,該原文的密文為:
    ssdtyd htegiasiscnm e ion
    #include
    #include
    typedef struct node
    { char ch;
    struct node *forward; /* Link to next node. */
    struct node *backward;/* Link to previous node.*/
    } CODE;
    int strlen(char *s)
    { int len = 0;
    while (*s++ != ’\0’ )
    len++;
    return( len );
    }
    char *decode(char *old,int key)
    { char *New; int length,count,i;
    CODE *loop,*p;
    length=strlen(old);
    loop=(CODE *) malloc( length*sizeof(CODE) );
    for ( i = 1;i
    { loop[i].forward = &loop[i+1];
    ___(1)___
    }
    loop[0].backward = &loop[length-1];
    loop[0].forward = &loop[1];
    loop[length-1].forward = loop;
    ___(2)___
    for ( p = loop,i = 0;i
    { for ( count = 1;count
    p= p->forward ;
    ___(3)___
    p->backward->forward = p->forward ;
    p->forward->backward = p->backward ;
    ___(4)___
    }
    New = ( char *)malloc( ( length+1 ) *sizeof(char) );
    for ( i=0;i
    ___(5)
    ___
    New[length]=’\0’;
    return (New);
    }
    void main()
    { char old[256];
    int key , num=0;
    printf("\nPlease input the telegraph: \n");
    while ( num<255 && ( old[num++] = getchar()) != ’\n’ );
    old [ (num==255)?num:num-1] = ’\0’;
    do
    { printf( "\nPlease input Key ( Key>1 ):" );
    scanf("%d",&key);
    } while ( key<=1 );
    printf( "\nThe decode of telegraph:’%s’ is:\n’%s’\n",old,decode( old,key ) );
    }
    試題五(15分,每空3分)
    閱讀以下說明及Visual Basic 程序代碼,將應(yīng)填入___(n)___處的字句寫在答題紙的對應(yīng)欄內(nèi)。
    [說明]
    本程序?qū)崿F(xiàn)如下功能:首先,單擊“生成”按鈕,生成一個由10個隨機大寫字母組成的字符串,并在文本框中顯示。然后,單擊“排序”按鈕,將此隨機字符串中的各個字母按遞增順序添加到列表框中。
    程序運行界面如下:
    [程序]
    As String
    Private Sub get_Click()
    Dim i As Integer
    Randomize
    For i = 1 To 10
    str1 = Trim(str1) & Chr(Int( ___(1)___ ))
    Next
    Text1.Text = str1
    End Sub
    Private Sub sort_Click()
    Dim i As Integer
    Dim j As Integer
    For i = 1 To 26
    j = ___(2)___
    Do While j > 0
    ___(3)___
    j = j - 1
    Loop
    Next
    End Sub
    Private Function search(str1 As String, str2 As String) As Integer
    Dim int1 As Integer, int2 As Integer
    int1 = 1
    Do
    int1 = ___(4)___
    If int1 = 0 Then Exit Do
    int2 = int2 + 1
    int1 = int1 + 1
    Loop
    search = ___(5)___
    End Function (
    試題六(15分,每空3分)
    閱讀以下說明和C++程序,將應(yīng)填入___(n)___處的字句寫在答題紙的對應(yīng)欄內(nèi)。
    [說明]
    字符串在程序設(shè)計中扮演著重要角色?,F(xiàn)需要設(shè)計字符串基類string,包含設(shè)置字符串、返回字符串長度及內(nèi)容等功能。另有一個具有編輯功能的串類edit_string,派生于string,在其中設(shè)置一個光標,使其能支持在光標處的插入、刪除操作。
    [程序]
    #include
    #include
    #include
    class string
    {
    int length;
    char *data;
    public:
    int get_length() { return length; }
    char *get_data() { return data; }
    ~string() { delete data; }
    int set_data( int in_length, char *in_data);
    int set_data( char *data );
    void print() { cout<
    };
    class edit_string : public string
    {
    int cursor;
    public:
    int get_cursor() { return cursor; }
    void move_cursor( int dis ) { cursor=dis; }
    int add_data(string *new_data);
    void delete_data( int num );
    };
    int string::set_data( int in_length, char *in_data )
    {
    length=in_length;
    if(!data)
    delete data;
    ___(1)___
    strcpy(data,in_data);
    return length;
    }
    int string::set_data( char *in_data )
    {
    ___(2)___
    if(!data)
    delete data;
    ___(1)___
    strcpy(data,in_data);
    return length;
    }
    int edit_string::add_data( string *new_data )
    {
    int n,k,m;
    char *cp,*pt;
    n=new_data->get_length();
    pt=new_data->get_data();
    cp=this->get_data();
    m=this->get_length();
    char *news = new char[n+m+1]
    ;
    for( int i=0; i
    news[i]=cp[i];
    k=i;
    for( int j=0; j
    news[i]=pt[j];
    cursor=i;
    for( j=k; j
    ___(3)___
    news[i]=’\0’;
    ___(4)___
    delete news;
    return cursor;
    }
    void edit_string::delete_data( int num )
    {
    int m;
    char *cp;
    cp=this->get_data();
    m=this->get_length();
    for( int i=cursor; i
    ___(5)___
    cp[i]=’\0’;
    }
    試題七(15分,每空3分)
    閱讀以下說明及Visual Basic 程序代碼,將應(yīng)填入___(n)___處的字句寫在答題紙的對應(yīng)欄內(nèi)。
    [說明]
    設(shè)窗體上有兩個文本框和一個按鈕,在第一個文本框text1中輸入一個全部由“0”和“1” 組成的字符串,單擊按鈕,在第二個文本框text2中顯示出給定字符串中連續(xù)的0和連續(xù)的1中連續(xù)字符數(shù)目的值。如果輸入的字符串中有不是“0”和“1”的字符,使用消息框顯示錯誤信息。
    運行界面如下:
    [程序]
    Private Sub Command1_Click()
    Dim strSource As String
    Dim str1 As String
    Dim result As Integer
    Dim int1 As Integer, num As Integer
    strSource = Text1.Text
    If Len(strSource) = 0 Then
    MsgBox "請在文本框中輸入由0和1組成的字符串"
    Exit Sub
    End If
    For int1 = 1 To Len(strSource)
    ___(1)___
    If str1 <> "1" And str1 <> "0" Then
    MsgBox "不能輸入非0或非1的字符"
    Exit Sub
    End If
    Next
    str1 = ___(2)____
    num = 1
    For int1 = 2 To Len(strSource)
    If str1 = Mid(strSource, int1, 1) Then
    ___(3)___
    Else
    If result < num Then
    result = num
    End If
    str1 = Mid(strSource, int1, 1)
    ___(4)___
    End If
    Next
    If result
    < num Then
    result = num
    End If
    ___(5)___
    End Sub
    試題八(15分,每空3分)
    閱讀以下說明、Java代碼將應(yīng)填入___(n)___處的字句寫在答題紙的對應(yīng)欄內(nèi)。
    [說明]
    IC卡和200卡都是從電話卡派生。下面程序?qū)㈦娫捒ǘx為抽象類。其中balance為雙精度變量,代表電話卡中的余額;cardNumber是長整型,代表電話卡的卡號;password是整型變量,代表電話卡的密碼;connectNumber是字符串變量,代表電話卡的接入號碼;connected是布爾變量,代表電話是否接通。
    performDial()實現(xiàn)各種電話接通后的扣除費用的操作。其中200卡每次通話扣除0.5 元的通話費用和附加費用;IC卡每次通話扣除0.9元的通話費。TimeLeft()方法用于測試電話卡余額還可以撥打電話的次數(shù)。performConnection()用于電話接入操作,如果卡號和密碼正確,則接通,否則,接不通。
    [程序]
    abstract class PhoneCard
    {
    double balace;
    ___(1)___ performDial();
    double getBalance()
    { return balance; }
    double TimeLeft()
    {
    double current=balance;
    int times=0;
    do
    {
    ___(2)___
    times++;
    }while(balance>=0);
    balance=current;
    return times-1;
    }
    }
    abstract class Number_PhoneCard extends PhoneCard
    {
    long cardNumber;
    int password;
    String connectNumber;
    Boolean connected;
    Boolean performConnection( long cn, int pw )
    {
    if( cn==cardNumber && ___(3)___ )
    {
    connected=true;
    return true;
    }
    else return false;
    }
    }
    class IC_Card ___(4)___
    {
    boolean performDial()
    {
    if(balance>0.9)
    {
    balance-=0.9;
    return true;
    }
    else return false;
    }
    }
    class D200_Card ___(4)___
    {
    static double additoryFee;
    static{ additoryFee=0.1; }
    boolean performDial()
    {
    if(balance>(0.5+additoryFee))
    {
    ___(5)___
    return true;
    }
    else return false;
    }
    }