C语言实验——三个数排序

来源:互联网 发布:淘宝网首页登录 编辑:程序博客网 时间:2024/06/05 23:56

C语言实验——三个数排序

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

输入三个整数x,y,z,请把这三个数由小到大输出。

Input

输入数据包含3个整数x,y,z,分别用逗号隔开。

Output

输出由小到大排序后的结果,用空格隔开。

Example Input

2,1,3

Example Output

1 2 3
思路:始终让第一个数最小,最后一个最大,如果第一个大于其他数就交换。
#include<stdio.h>int main(){    int a, b, c, d;    scanf("%d,%d,%d",&a, &b, &c);    if(a > b)    {        d = a;        a = b;        b = d;    }    if(a > c)    {        d = c;        c = a;        a = d;    }    if(c < b)    {        d = b;        b = c;        c = d;    }    printf("%d %d %d\n",a, b, c);    return 0;}