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

2011年9月全国二级C上机真题第1~2套

2013年02月25日 14 二级C语言 ⁄ 共 1657字 ⁄ 字号 暂无评论

第1~2套

【程序填空题_1】给定程序中,函数fun的功能是:把形参s所指字符串中下标为奇数右移到下一个奇数位置,最右边被移出字符串的字符绕回到第一个奇数位置,下标为偶数的字符不动(注:字符串长度大于等于2)。例如,形参s所指的字符串为abcdefgh,执行结果为:ahcbedgf。

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>
void fun(char  *s)
{
    int  i, n, k;
    char c;
    n=0;
    for(i=0; s[i]!='\0'; i++)  n++;
    /**********found**********/
    if(n%2==0) k=n-___1___ ;
    else       k=n-2;
    /**********found**********/
    c=___2___ ;
    for(i=k-2; i>=1; i=i-2)  s[i+2]=s[i];
    /**********found**********/
    s[1]=___3___ ;
}
main()
{
    char  s[80]="abcdefgh";
    printf("\nThe original string is :  %s\n",s);
    fun(s);
    printf("\nThe result is :  %s\n",s);
}

【程序修改题_1】给定程序MODI1.C中,fun函数的功能是:求
s=aa…aa-…-aaa-aa-a
(此处aa…aa表示n个a,a和n的值在1至9之间)
例如 a=3, n=6,则以一表达式为:
s=333333-33333-3333-333-33-3
其值是:2962968
a和n是fun函数的形参,表达式的值作为函数值传回main函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
long fun (int a, int n)
{
    int  j ;
    /**************found**************/
    long  s = 0,  t = 1 ;
    for ( j = 0 ; j < n ; j++)
        t = t * 10 + a ;
    s = t ;
    for ( j = 1 ; j < n ; j++) {
        /**************found**************/
        t = t % 10 ;
        s = s - t ;
    }
    return(s) ;
}
 
main( )
{
    int  a, n ;
    printf( "\nPlease enter a and n:") ;
    scanf(  "%d%d", &a, &n ) ;
    printf( "The value of  function is: %ld\n", fun ( a, n ) );
}

【程序设计题_1】请编写一个函数void fun(char *tt, int pp[]),统计在tt所指字符串中 ’a’ 到 ’z’ 26个小写字母各自出现的次数,并依次放在pp所指数组中。
例如,当输入字符串:abcdefgabcdeabc后,程序的输出结果应该是:
3 3 3 2 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <string.h>
void fun(char *tt, int pp[])
{
 
}
main( )
{
    char aa[1000] ;
    int  bb[26], k ;
    printf( "\nPlease enter  a char string:" ) ;
    scanf("%s", aa) ;
    fun(aa, bb ) ;
    for ( k = 0 ; k < 26 ; k++ ) printf ("%d ", bb[k]) ;
    printf( "\n" ) ;
}

抱歉!评论已关闭.