2016年9月计算机二级C语言操作题109套(校订版)
更新时间:2017/2/2 17:35:00 浏览量:602 手机版
1
#include
unsigned long fun(unsigned long n)
{ unsigned long x=0; int t;
while(n)
{ t=n%10;
/**********found**********/
if(t%2==____1____)
/**********found**********/
x=____2____+t;
/**********found**********/
n=____3____;
}
return x;
}
main()
{ unsigned long n=-1;
while(n>99999999||n<0)
{ printf("Please input(0
printf("\nThe result is: %ld\n",fun(n));
}
【参考答案】
(1)0 (2) 10*x (3)n/10
【考点分析】
本题考查:已知某数,如何求该数各个位数值,已知各个位数值,如何表示该数;除法运算。
【解题思路】
填空1:定义变量t用来存放某数的各个位数值,此处判断t是否为偶数,即对2求余结果是否为0。
填空2:将t作为x的个位数,原来x的各个位上升1位,即x=10*x+1。
填空3:每循环一次,通过除法运算,去掉数值最后一位。
【解题宝典】
如果知道某数n的各个位的数值,可以得到该数值n,如n的个位为a、十位为b、百位为c,那么n=c*100+b*10+a。如果知道数值n,可以采用求余和除法操作来表示其各个位,如n%10(取个位)、n/10(取十位),读者可以自己考虑一下,如果是三位数,如何提取各个位?四位数呢?
#include
void fun (long s, long *t)
{ int d;
long sl=1;
/************found************/
t = 0;
while ( s > 0)
{ d = s%10;
/************found************/
if (d%2 == 0)
{ *t = d * sl + *t;
sl *= 10;
}
s /= 10;
}
}
main()
{ long s, t;
printf("\nPlease enter s:"); scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ld\n", t);
}
【参考答案】
(1)*t=0;
(2)if(d%2!=0)或if(d%2==1)
【考点分析】
本题考查:指针型变量作为函数参数;if语句条件表达式,结合奇偶数的表示方法来确定该表达式内容。
【解题思路】
(1) 由函数定义可知,变量t是指针变量,所以对t进行赋初值0是不对的。因为t指向的是存放新数的变量,所以此处应给新数赋初值0,即*t=0。
(2) 变量d表示数s各个位上的数,此处的if条件应为判断d是否为奇数。
#include
void fun(char p1[], char p2[])
{
}
main()
{ char s1[80], s2[40] ;void NONO ();
printf("Enter s1 and s2:\n") ;
scanf("%s%s", s1, s2) ;
printf("s1=%s\n", s1) ;
printf("s2=%s\n", s2) ;
printf("Invoke fun(s1,s2):\n") ;
fun(s1, s2) ;
printf("After invoking:\n") ;
printf("%s\n", s1) ;
NONO() ;
}
void NONO ()
{/*
±?oˉêyó?óú′ò?a???t£?ê?è?2aê?êy?Y£?μ÷ó?funoˉêy£?ê?3?êy?Y£?1?±????t?£*/
int i ;
FILE *rf, *wf ;
char s1[80], s2[40] ;
rf = fopen("in.dat","r") ;
wf = fopen("out.dat","w") ;
for(i = 0 ; i < 10 ; i++)
{ fscanf(rf, "%s", s1) ;
fscanf(rf, "%s", s2) ;
fun(s1, s2) ;
fprintf(wf, "%s\n", s1) ;
}
fclose(rf) ;
fclose(wf) ;
}
【参考答案】
void fun(char p1[], char p2[])
{ int i,j;
for(i=0;p1[i]!='\0';i++) ;
for(j=0;p2[j]!='\0';j++)
p1[i++]=p2[j];
p1[i]='\0';
}
【考点分析】
本题考查:不使用字符串函数实现字符串连接操作。通过for循环语句来完成,不要忘了最后需要加上字符串结束标识'\0'。
【解题思路】
本题用两个循环完成操作,第1个循环的作用是求出第1个字符串的长度,即将i指到第1个字符串的末尾。第2个循环的作用是将第2个字符串的字符连到第1个字符串的末尾。最后在第1个字符串的结尾加上字符串结束标识′\0′。
2
# include
#define N 9
void fun(int a[], int n)
{ int i,j, max, min, px, pn, t;
for (i=0; i
{ /**********found**********/
max = min = ___1___;
px = pn = i;
for (j=i+1; j
{ /**********found**********/
if (max<___2___>
{ max = a[j]; px = j; }
/**********found**********/
if (min>___3___)
{ min = a[j]; pn = j; }
}
if (pn != i)