C语言程序设计自测练习及参考答案 联系客服

发布时间 : 星期一 文章C语言程序设计自测练习及参考答案更新完毕开始阅读

6. 下面函数是求两个整型参数a和b的最小公倍数。 int f2(int a, int b) {

int i=2, p=1; do {

while(a%i==0 && ___(1)___) { p*=i; a/=i; b/=i; }

___(2)___;

}while(a>=i && ___(3)___); return p*a*b; }

(1) (2) (3)

7. 在输出屏幕上打印出一个由字符’*’组成的等腰三角形,该三角形的高为5行,从上到下每行的字符数依次为1,3,5,7,9。 #include void main() {

int i,j;

for(i=1;___(1)___;i++) { for(j=1;j<=9;j++)

if(j<=5-i || ___(2)___) cout<<’ ’; else ___(3)___; cout<

(1) (2) (3)

8. 统计字符串中英文字母个数的程序。 #include int count (char str[]); void main(){ char s1[80];

cout <<”Enter a line:”; cin >>s1;

cout <<”count=”<

int count(char str[]){

9

int num=0; //给统计变量赋初值 for(int i=0;str[i];i++)

if (str[i]>=’a’ && str[i]<=’z’ ||___(1)___ ) ___(2)___; ___(3)___; }

(1) (2) (3)

9. 主函数调用一个fun函数将字符串逆序。

#include #include

___(1)___; void main( ) { char s[80]; cin>>s; ___(2)___;

cout<<”逆序后的字符串:”<

void fun(char ss[]) { int n=strlen(ss);

for(int i=0; ___(3)____; i++) { char c=ss[i]; ss[i]=ss[n–1–i]; ss[n–1–i]=c; } }

(1) (2) (3)

10. 从一个字符串中删除所有同一个给定字符后得到一个新字符串并输出。 #include const int len=20;

void delstr(char a[],char b[],char c); void main() {

char str1[len],str2[len]; char ch;

cout<<\输入一个字符串:\ cin>>str1;

cout<<\输入一个待删除的字符:\ cin>>ch;

delstr(str1,str2,ch);

10

cout<

void delstr(char a[],char b[],char c) {

int j=0;

for(int i=0; ___(1)___; i++) if(___(2)___) b[j++]=a[i]; b[j]=___(2)___; }

(1) (2) (3)

四、写出程序运行结果

1. #include #include void main() {

int a[8]={25,48,32,85,64,18,48,29}; int max,min; max=min=a[0];

for(int i=0; i<8; i++) { if(max>a[i]) max=a[i]; if(min

cout<<\ cout<<\ }

2. #include void main() {

int a,b;

for(a=1,b=2; b<50;) { cout<

cout<

cout<

11

3. #include const int M=3, N=4; void main() {

int i,j,s=0;

for(i=1;i<=M;i++) for(j=1;j<=N;j++) s+=i*j;

cout<<”s=”<

4. #include void main() {

int a=2,b=5,c=0;

if(a+b>10) c=a*b; else c=3*a+b;

if(c<=20) cout<

a=a+b; b=a+b;c+=a+b;

cout<<\ }

5. #include void main() {

int x=5;

switch(2*x-3) {

case 4: cout<

case 10: cout<<3*x-1<<’ ’; break; default: cout<<\ }

cout<<\ }

6. #include #include

int a[4]={36,-5,73,8}; void main()

12