zoukankan      html  css  js  c++  java
  • c语言 10

    1、三个值升序

    #include <stdio.h>
    
    void swap(int *x, int *y)
    {
        int tmp = *x;
        *x = *y;
        *y = tmp;
    }
    
    void sort(int *i, int *j, int *k)
    {
        if(*i > *j)
            swap(i, j);
        if(*i > *k)
            swap(i, k);
        if(*j > *k)
            swap(j, k);
    }
    
    int main(void)
    {
        int a, b, c;
        puts("please input three integers.");
        printf("a = "); scanf("%d", &a);
        printf("b = "); scanf("%d", &b);
        printf("c = "); scanf("%d", &c);
        
        sort(&a,&b,&c);
        puts("
    =========================");
        
        printf("a = %d | b = %d | c = %d
    ", a, b, c);
        
        return 0;
    }

    2、三个值降序

    #include <stdio.h>
    
    void swap2(int *x, int *y)
    {
        int tmp = *x;
        *x = *y;
        *y = tmp;
    }
    
    int sort2(int *n1, int *n2, int *n3)
    {
        if(*n1 < *n2)
            swap2(n1, n2);
        if(*n1 < *n3)
            swap2(n1, n3);
        if(*n2 < *n3)
            swap2(n2, n3);
    }
    
    int main(void)
    {
        int a, b, c;
        puts("please input three integers.");
        printf("a = "); scanf("%d", &a);
        printf("b = "); scanf("%d", &b);
        printf("c = "); scanf("%d", &c);
        
        sort2(&a, &b, &c);
        
        puts("
    ==========================");
        
        printf("a = %d | b = %d | c = %d
    ", a, b, c);
        
        return 0;
    }

    3、对数组的元素进行升序排列

    #include <stdio.h>
    
    #define NUMBER 6
    
    void swap(int *x, int *y)
    {
        int tmp = *x;
        *x = *y;
        *y = tmp;
    }
    
    void sort(int x[], int n)
    {
        int i, j;
        for(i = 0; i < n - 1; i++)
        {
            for(j = n - 1; j > i; j--)
            {
                if(x[j - 1] > x[j])
                {
                    swap(&x[j - 1], &x[j]);  // 此处的函数调用给与的实参需要使用取址运算符&,因为x[j-i]和x[j]为数组元素,需要获得指向元素的指针,使用取址运算符&+对象,获得地址,
                    //生成指针。 
                }
            }
        }
    }
    
    int main(void)
    {
        int i, a[NUMBER];
        puts("please input the element of the array.");
        for(i = 0; i < NUMBER; i++)
        {
            printf("NO.%d = ", i + 1); scanf("%d", &a[i]);
        }
        
        sort(a, NUMBER);
        
        puts("
    ======================");
        
        for(i = 0; i < NUMBER; i++)
        {
            printf("a[%d] = %d
    ", i, a[i]);
        }
        
        return 0;
    }

  • 相关阅读:
    resin实现热部署配置
    tomcat实现域名访问步骤
    springboot学习笔记2---配置拦截器:
    springboot学习笔记2:搭建web项目
    springboot学习笔记1:springboot入门
    重识maven
    shiro学习笔记:remeberMe,多次登录锁死账号
    shiro学习笔记:授权管理
    springmvc定时任务及RequestBody注解
    springmvc处理异常
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14828207.html
Copyright © 2011-2022 走看看