对于输入的a、b、c三个整数求最小值。
以下程序有错,如何改呢?
int main()
{
int a,b,c;
scanf(”%d%d%d”,a,b,c);
if((a>b)&&(a>c))
if(bprintf(”min=%d\n”,b);
else
printf(”min=%d\n”,c);
if((aprintf(”min=%d\n”,a);
}
下列程序执行后t 的值为4,则执行时输入a,b的值范围。
#include
int main()
{
int a, b, s = 1,t = 1;
scanf("%d ,%d",&a,&b);
if (a > 0)
s += 1;
if (a > b)
t += s;
else if (a == b)
t = 5;
else
t = 2 * s;
printf("s=%d,t=%d\n",s,t);
}
下列程序当a的值为014和ox14时执行结果分别是:
#include
int main()
{
int a;
scanf("%o", a);
if (a = 0xA | a > 12)
if (011 & 10 == a)
printf("%d!\n", a);
else
printf("Right!%d\n", a);
else
printf("Wrong!%d\n",a);
return 0;
}
执行下列程序段后,x、y和z的值分别是 20 30 30
int x=10,y=20,z=30;
if(x>y) z=x;x=y;y=z;
************************
int x = 10, y = 20, z = 30;
if (x > y)
z = x;
x = y;
y = z;
不执行第一个语句,继续执行之后的语句
求解一元二次方程
#include
#include
int main()
{
float a, b, c, d, x1, x2, q, m, n;
printf("Please input a,b,c\n");
scanf("%f,%f,%f",&a,&b,&c);
d = b*b - 4*a*c;
if (d >= 0)
{
q = sqrt(d);
x1 = (-b + q) / (2 * a);
x2 = (-b - q) / (2 * a);
printf("x1 =%.2f,x2=%.2f\n",x1,x2);
}
else
{
m = b / (2 * a);
n = sqrt(-d) / (2 * a);
printf("x1 = %.2f + %.2fi",m,n);
printf("x1 = %.2f + %.2fi",m,n);
}
}
任意的三个数从大到小的排序
#include
int main()
{
int a, b, c;
printf("请输入三个整数 ");
scanf("%d %d %d",&a,&b,&c);
if (a > b)
{
if (c > a)
printf("%d %d %d\n",c,a,b);
else //c和b
{
if (b > c)
printf("%d %d %d\n", a, b, c);
else
printf("%d %d %d\n", a, c, b);
}
}
else//b>a
{
if (c > b)
printf("%d %d %d\n",c,b,a);
else
{
if (a > c)
printf("%d %d %d\n", b, a, c);
else
printf("%d %d %d\n",b,c,a);
}
}
}
三个数排序之后的优化程序
#include
int main()
{
int a, b, c,t;
printf("请输入三个数 :");
scanf("%d %d %d %d",&a,&b,&c);
if (a < b)
{
t = a;
a = b;
b = t;
}
if (b < c)
{
t = b;
b = c;
c = t;
if (a > b)
{
t = a;
a = b;
b = t;
}
}
printf("从大到小:%d,%d,%d",a,b,c);
return 0;
}
自己编的错的程序,如何更改呢?
#include
int main()
{
int a, b, c,t;
printf("请输入三个整数:");
scanf("%d %d %d",&a,&b,&c);
if (a > b)//交换a,b
{
t = a;
b = a;
a = t;
}
else
{
if (b > c)
{
t = b;
b = a;
a = t;
}
else
{
if (a > b)
{
t = a;
a = b;
b = t;
}
}
}
}
多级选择
多级选择和嵌套选择有什么区别?
来自课件“讨论题2”
关于switch
switch可以嵌套使用吗?
来自课件“讨论题3”
#include
int main()
{
int year;
float money, rate, total;
printf("Input money and year: ");
scanf("%f %d",&money,&year);
if (year == 1)
rate = 0.0032;
else if (year == 2)
rate = 0.0041;
else if (year == 3)
rate = 0.005;
else if (year == 5)
rate = 0.0055;
else if (year == 6)
rate = 0.0065;
else
rate = 0.0;
total = money + money*rate * 12 * year;
printf("Total = %.2f\n",total);
return 0;
}
(一)用switch解决多分支问题
(二)实现加减练习题
编写一个程序。实现简单的加减乘除
#include
int main()
{
float d1, d2;//定义操作数
char op; //操作符变量
scanf("%f %c %f",&d1,&op,&d2);
switch (op)//根据操作符分别进行处理
{
case '+':
printf("%.2f +%.2f = %.2f\n",d1,d2,d1+d2);
break;
case '-':
printf("%.2f - %.2f =%.2f\n",d1,d2,d1-d2);
break;
case '*':
printf("%.2f - %.2f =%.2f\n",d1,d2,d1*d2);
break;
case '/':
if (d2 == 0)//判断除数是否为0
printf("Division by zero.\n");
else
printf("%.2f/%.2f=%.2f\n",d1,d2,d1/d2);
break;
}
}
(三)switch的结构
型变量,以下正确的switch语句是
A. switch (a/b)
{ case 1: case 3.2: y=a+b; break ;
case 0: case 5: y=a-b;
}
B. switch (a*a+b*b);
{case 3:
case 1: y=a+b; break ;
case 0: y=b-a; break;
}
C. switch a
{ default : x=a+b;
case 10 : y=a-b;break;
case 11 : y=a*d; break;
}
D. switch(a+b)
{case 10: x=a+b; break;
case 11: y=a-b; break;
}
不提倡使用goto语句
goto 语句的简单实例
简单实例
#include
int main()
{
int n = 0;
printf("input a string: \n");
loop:if (getchar() != '\n')
{
n++;
goto loop;
}
printf("%d",n);
}
#include
int main()
{
int n = 0;
printf("input a string: \n");
while (getchar() != '\n')
{
n++;
}
printf("%d\n",n);
}
计算税金
按目前国家税收标准,如果月收入1.58万元,应交国家的税金是多少?
来自课件“讨论题4”
输出奇数
能正确表达“当x的值是[-1,100]或[200,210]范围内的奇数时,输出x”的if语句应该怎样写?
来自课件“讨论题5”输入某年某月某日,判断并输出这一天是这一年的第几天
问题分析:
输入年月日,计算当前月份之前的天数 + 当月的天数
注意:如何判断闰年
是闰年而且月份超过2月则给总天数再加1.
#include
int main()
{
int day, month, year,sum;
printf("请输入某年某月某日:");
scanf("%d %d %d",&year,&month,&day);
switch (month)//计算当前月之前的月份的天数
{
case 1:
sum = 0;
break;
case 2:
sum = 31;
break;
case 3:
sum = 59;
break;
case 4 :
sum = 90;
break;
case 5:
sum = 120;
break;
case 6:
sum = 151;
break;
case 7:
sum = 181;
break;
case 8:
sum = 212;
break;
case 9:
sum = 243;
break;
case 10:
sum = 273;
break;
case 11:
sum = 304;
break;
case 12:
sum = 334;
break;
}
//判断是否是闰年
if (((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) && month >2)
sum = sum + 1;如果是闰年的话,多加一天
sum = sum + day;加上当前月的天数
printf("总共%d day",sum);
}
#include
int main()
{
int year;
float money, rate, total;
printf("Input money and year:");
scanf("%f %d",&money,&year);
switch (year)
{
case 1:
rate = 0.0032;
case 2:
rate = 0.0041;
break;
case 3:
rate = 0.005;
break;
case 5:
rate = 0.0055;
break;
case 8:
rate = 0.0065;
break;
default:
rate = 0.0;
}
total = money + money*rate * 12 * year;
printf("Total = %.2f\n",total);
}