第4章 选择结构 练习题
{ case 0: b++; case 1: a++; case 2: a++;b++; }
printf(\
}
运行结果为:a=2,b=2 18.
#include
char ch;
ch=getchar( ); switch(ch)
{ case ?A? : printf(“%c”,?A?);
case ?B? : printf(“%c”,?B?); break; default: printf(“%s\\n”,”other”); } }
当从键盘输入字母A时,运行结果为:AB 当从键盘输入字母a时,运行结果为:other
19.
#include
case 2: b=2;break; default : b=10; }
printf(\}
若键盘输入1,运行结果为:2 若键盘输入5,运行结果为:10 20.
#include
int a=10,b=11,c=0; switch(a%3)
{ case 0: c++;break; case 1: c++;
9
第4章 选择结构 练习题
case 2: switch(b%2)
{case 0: c++;; default : c++; } }
printf(\}
运行结果为:2
三、编程题
1、输入两个数,输出较大的数。 #include
printf(“Please input two numbers :”) ;
scanf(“%d%d”,&a,&b); if(a>b) max=a; else
max=b;
printf(“max=%d\\n”,max); return 0 ; } 2、输入一个整数,判断其奇偶性。 #include
int m; scanf(\ if (m%2 == 0) printf(\ else printf(\
}
3、输入一个正整数,判断其是否为3和7的公倍数,若是输出“Yes”,否则输出“No”。#include
int m; scanf(\ if (m%3 == 0&& m%7 == 0) printf(\ else printf(\ }
10
第4章 选择结构 练习题
4、输入一个字符,如果是大写英文字母,将其转换为小写字母并输出,如果不是,则原样输出。 #include
5、输入一个字符,如果是英文字母,输出“abcd”,如果是数字字符,输出“####”,如果都不是,输出“other”。 #include
else if(c>=?0?&&c<=?9?)
printf(“####”); else
printf(“other”); return 0; } 6、输入三个数,按照从小到大的顺序输出。 #include
scanf(“%f%f%f”,&a,&b,&c); if(a>b)
{t=a;a=b;b=t;} if(a>c)
{t=a;a=c;c=t;} if(b>c)
{t=b;b=c;c=t;}
printf(\}
7、函数y=f(x)表示如下,编程实现输入一个x值,输出y值。 2x+1 (x<0) y= 0 (x=0) 2x-1 (x>0)
#include
11
第4章 选择结构 练习题
int x,y;
scanf(“%d”,&x); if(x<0)
y=2*x+1; else if(x>0) y=2*x-1; else y=0;
printf(“%d”,y); }
8、编程,输入每个月的上网时间,计算上网费用,资费标准如下:
30元??费用??每小时3元?每小时2.5元? #include
printf(“please input hour:\\n”); scanf(“%d”,&hour); if(hour<=10) fee=30;
else if(hour<=50) fee=3*hour; else
fee=hour*2.5;
printf(“The total fee is %f”,fee); }
?10小时10?50小时?50小时9、神州行用户无月租费,话费每分钟0.6元,全球通用户月租费50元,话费每分钟0.4元。输入一个月的通话时间,分别计算出两种方式的费用,判断哪一种合适。 #include
void main() { float t, szx, qqt ;
printf(“请输入您的通话时间:”); scanf(“%f,”,&t); szx= 0.6*t;
qqt=50+0.4*t; if (szx>qqt)
printf(“建议使用全球通”); else
printf(“建议使用神州行); }
12
第4章 选择结构 练习题
10、要求按照考试成绩的等级输出百分制分数段,A等为85分以上,B等为70~84分,C等为60~69分 ,D等为 60分以下 。成绩的等级由键盘输入。 #include
13
相关推荐: