zoukankan      html  css  js  c++  java
  • swap的应用两个数的交换

    #include <stdio.h>//这儿表示的函数的输入输出头文件
    void swap(int x,int y);
    void swap_p(int *x,int *y);//表示调用一个方法时必须声明这个方法的原型,否则不能调用(方法二)
    #define swap_m(x,y,t)((t)=(x),(x)=(y),(y)=(t))//这是用红还是声明方法,后面不需要用分号(方法三)
    int main(){//这儿表示的是函数的入口
    int a,b,temp;
    a =10;
    b= 20;
    printf("a=%d,b=%d ",a,b);//输出的格式
    printf("------------- ");
    swap_p(&a,&b);//表示指针形的交换
    /**这儿表示不调用方法的交换(方法一)
    temp = a;
    a = b;
    b = temp;**/
    printf("a=%d,b=%d ",a,b);
    printf("------------- ");
    //之所以值不变,是因为swap的仅为值,当传过来的时候,还是原来的值
    swap(a,b);
    printf("a=%d,b=%d ",a,b);
    //方法三
    printf("------------- ");
    swap_m(a,b,temp);
    printf("a=%d,b=%d ",a,b);
    return 0;

    }
    void swap(int x,int y){
    int temp;
    temp = x;
    x = y;
    y = temp;
    }
    void swap_p(int *x,int *y){
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
    }

  • 相关阅读:
    树剖
    codeforces round 589
    codeforces round 590
    code craft 20
    Ozon Tech Challenge 2020 (Div.1 + Div.2)
    codeforces round 625
    Crime HDU
    codeforces 594
    codeforces 596
    python操作mysql方法和常见问题
  • 原文地址:https://www.cnblogs.com/caocx/p/5795279.html
Copyright © 2011-2022 走看看