编辑:
我使用scanf函数从控制台获得3个不同的数字。 (限制:只有整数可能是负数,正数)。我必须按照这样的顺序排列这些数字,以便它显示具有适当运算符关系的下降顺序,如:
Input:
Enter 1st Num: 6
Enter 2nd Num: 3
Enter 3rd Num: 4
Expected Output: 6 > 3 > 2
Input:
Enter 1st Num: 3
Enter 2nd Num: 2
Enter 3rd Num: 6
Expected Output: 6 > 3 > 2
#
Input:
Enter 1st Num: 6
Enter 2nd Num: 6
Enter 3rd Num: 4
Expected Output: 6 = 6 > 4
Input:
Enter 1st Num: 6
Enter 2nd Num: 3
Enter 3rd Num: 3
Expected Output: 6 > 3 = 3
Input:
Enter 1st Num: 6
Enter 2nd Num: 6
Enter 3rd Num: 6
Expected Output: 6 = 6 = 6
注意:我为2个数字做了同样的程序:
#include <stdio.h>
int main(void) {
int a,b;
printf("Enter 1st Num:");
scanf("%d",&a);
printf("Enter 2nd Num:");
scanf("%d",&b);
if (a>b)
{
printf("%d > %d",a,b);
}
else
{
if (b > a){
printf("%d > %d",b,a);
}
else{
printf("%d = %d", a,b);
}
}
return 0;
}
以下代码的输出是:
Input:
Enter 1st Num: 4
Enter 2nd Num: 5
Output: 5 > 4
Input:
Enter 1st Num: 5
Enter 2nd Num: 4
Output: 5 > 4
Input:
Enter 1st Num: 4
Enter 2nd Num: 4
Output: 4 = 4
答案 0 :(得分:1)
打印数字的方法:
void sort(int a, int b, int c)
{
int top, mid, bot;
if (a > b)
{
if (c > a)
{
top = c;
mid = a;
bot = b;
}
else
{
top = a;
if (c > b)
{
mid = c;
bot = b;
}
else
{
mid = b;
bot = c;
}
}
}
else
{
if (c > b)
{
top = c;
mid = b;
bot = a;
}
else
{
top = b;
if (c > a)
{
mid = c;
bot = a;
}
else
{
mid = a;
bot = c;
}
}
}
printf("%i %c %i %c %i \n",
top, (top == mid) ? '=' : '>',
mid, (mid == bot) ? '=' : '>',
bot);
}