输入三个数,分别放入变量x,y,z中
打印输入数据中最小的那一个数
解决方案1
定义中间变量 t
1.比较x和y的大小关系,将较小的值赋值给t
2.比较t和z的大小关系,将较小的值赋值给t
3.t 中保存的就是3个数中的较小值
(直接t=z)
代码部分:
#include<stdio.h>
int main( )
{
int x = 0;
int y = 0;
int z = 0;
int t = 0;
printf("Input 3 integer: ");
scanf("%d%d%d", &x, &y, &z);
getchar();
if(x<y)
{
t=x;
}
else
{
t=y;
}
if(t>z)
{
t=z;
}
printf("The smallest is:%d\n",t);
getchar();
return 0;
}
运行结果:
解决方案2
代码:
#include<stdio.h>
int main( )
{
int x = 0;
int y = 0;
int z = 0;
printf("Input 3 integer: ");
scanf("%d%d%d",&x,&y,&z);
getchar();
if(x < y)
{
if(x < z)
{
printf("The smallest is:%d\n",x);
getchar();
}
else
{
printf("The smallest is:%d\n",z);
getchar();
}
}
else
{
if(y < z)
{
printf("The smallest is:%d\n",y);
getchar();
}
else
{
printf("The smallest is:%d\n",z);
getchar();
}
}
getchar();
return 0;
}
运行结果: