首页 > 试题广场 >

编写函数实现两数交换(指针方式)

[编程题]编写函数实现两数交换(指针方式)
  • 热度指数:20309 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
编写一个函数,实现两个整数的交换,要求采用指针的方式实现。

输入描述:
键盘输入2个整数 m 和 n


输出描述:
输出交换后m 和 n 的值,中间使用空格隔开
示例1

输入

2
3

输出

3 2
#include<stdio.h>
void swap(int *p1,int *p2)
{
    int temp;
    temp=*p1;
    *p1=*p2;
    *p2=temp;
}
int main()
{
    int m,n;
    scanf("%d %d",&m,&n);
    swap(&m,&n);
    printf("%d %d",m,n);
    return 0;
}
发表于 2022-03-04 12:23:34 回复(1)
//emmm....突发奇想就写了这个,编译跑过了,但是不符合题意... anyway,既然想到了,那就发出来吧..
int main() {

    int m, n;
    cin >> m;
    cin >> n;

    // write your code here......
    int temp = *(&m);
    *(&m) = *(&n);
    *(&n) = temp;

    cout << m << " " << n << endl;

    return 0;
}
发表于 2022-05-19 21:02:41 回复(2)
 cout << n << " " << m << endl;
编辑于 2024-03-01 15:15:07 回复(0)
值传递,子函数里修改的值不能传递回主函数,但是修改的地址是可以传回去的。指针就是地址

#include <iostream>
using namespace std;

// write your code here......
void myswap(int *a,int *b){
    int temp = *a;
    *a = *b;
    *b =temp;   
}//相当于交换了m和n的地址,自然修改了其中代表的值
//end

int main() {

    int m, n;
    cin >> m;
    cin >> n;

    // write your code here......
    myswap(&m,&n);  
    //end

    cout << m << " " << n << endl;

    return 0;
}


编辑于 2024-02-04 10:47:36 回复(0)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
void function(int* p, int* q)
{
    int tmp = *p;
    *p = *q;
    *q = tmp;
}
int main()
{
    int m = 0;
    int n = 0;
    scanf("%d%d", &m, &n);
    int* p = &m;
    int* q = &n;
    function(p, q);
    printf("%d %d", m, n);
    return 0;
}
编辑于 2024-01-27 19:12:56 回复(1)
#include<stdio.h>
void swap(int *a,int *b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
int main ()
{
    int m,n=0;
    scanf("%d %d",&m,&n);
    swap(&m,&n);
    printf("%d %d",m,n);
    return 0;
}

编辑于 2024-01-14 17:21:15 回复(0)
int main()
{
    int m = 1;
    int n = 1;
    scanf("%d %d", &m, &n);

    int* p = &m;
    int* q = &n;
    int c = 1;
    c = *p;
    *p = *q;
    *q = c;
    printf("m=%d,n=%d", m, n);
    return 0;
}
发表于 2023-10-14 09:43:10 回复(0)
#include <iostream>
using namespace std;

// write your code here......

void num(int *a,int *b){
    int temp=*a;
    *a=*b;
    *b=temp;
}
void num1(int &a,int &b){
    int temp = a;
    a = b;
    b = temp;
}

int main() {

    int m, n;
    cin >> m;
    cin >> n;

    // write your code here......
    //引用
    // num1(m,n);
    // cout << m << " " << n << endl;

    //指针
    num(&m,&n);
    cout << m << " " << n << endl;
    return 0;
}
发表于 2025-06-13 10:50:37 回复(0)
#include<stdio.h>
void swap(int * a,int *b);
int main(){
   int a;
   scanf("%d",&a);
   int b;
   scanf("%d",&b);
   int* p;
   int* z;
    p=&a;
    z=&b;
    swap(p,z);
    printf("%d %d",a,b);
}

 void swap(int * a,int *b){//´«ÈëÖ¸ÕëÀàÐÍ
    int t;
    t=*a;//½â´ðÖ¸Õ룬»ñÈ¡ÕæÊµÖµ
    *a=*b;
    *b=t;
 }
发表于 2025-03-09 15:14:18 回复(0)
#include <iostream>
using namespace std;

// write your code here......
void swap(int* a, int* b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}


int main() {

    int m, n;
    cin >> m;
    cin >> n;

    // write your code here......
    swap(&m, &n);
    
    cout << m << " " << n << endl;

    return 0;
}

发表于 2025-01-17 16:03:27 回复(0)
#include<stdio.h>
void Exchange(int* m,int* n)
{
    int temp = *m;
    *m = *n;
    *n = temp; 
}
int main()
{
    int m = 0;
    int n = 0;
    scanf("%d\n%d",&m,&n);
    Exchange(&m,&n);
    printf("%d %d",m,n);
    return 0;
}

发表于 2025-01-10 10:01:19 回复(0)
#include<stdio.h>

int main()
{
    int m,n;
    scanf("%d %d",&m,&n);
    int*p=&m;
    int*q=&n;
    int temp=0;
    temp=*p;
    *p=*q;
    *q=temp;
    printf("%d %d",m,n);
    return 0;
}

发表于 2024-10-05 10:33:01 回复(0)
#include <iostream>
using namespace std;
//交换都给玩烂掉了
// write your code here......

template <typename T>
void myswap(T& a,T &b)
{
    a ^= b;
    b ^= a;
    a ^= b;
}

// void myswap(int& a,int &b)
// {
//     a += b;
//     b = a-b;
//     a = a-b;
// }

int main() {

    int m, n;
    cin >> m;
    cin >> n;

    // write your code here......
    myswap<int>(m,n);

    cout << m << " " << n << endl;

    return 0;
}

发表于 2024-08-20 14:50:14 回复(0)
#include <stdio.h>

void my_change(int* a, int* b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}
int main()
{
    int a=0;
    int b=0;
    int* p=&a;
    int* pc=&b;
    scanf("%d %d", &a, &b);
    my_change(p, pc);
    printf("%d %d", a, b);
}

编辑于 2024-04-20 15:28:45 回复(0)
#include <iostream>
using namespace std;


void Swap(int* x, int* y)
{
    *x ^= *y;
    *y ^= *x;
    *x ^= *y;
}

int main() 
{
    int m = 0;
    int n = 0;

    scanf("%d%d", &m, &n);

    Swap(&m, &n);

    printf("%d %d", m, n);

    return 0;
}

发表于 2024-03-27 11:55:34 回复(0)
#include<stdio.h>
void swap(int* a, int* b) {
    int temp = 0;
    temp = *a;
    *a = *b;
    *b = temp;
}
int main() {
    int n = 0, m = 0;
    scanf("%d%d", &n, &m);
    swap(&n, &m);
    printf("%d %d", n, m);
    return 0;
}

发表于 2023-11-18 21:23:38 回复(0)
#include <iostream>
using namespace std;

// write your code here......


int main() {

    int m, n;
    cin >> m;
    cin >> n;

    // write your code here......
    int *p1 = &m;
    int *p2 = &n;
    *p1 = *p1 - *p2;
    *p2 = *p1 + *p2;
    *p1 = *p2 - *p1;

    cout << m << " " << n << endl;

    return 0;
}

发表于 2023-07-30 12:39:28 回复(0)
#include <iostream>
using namespace std;

template <typename T>
void Swap(T& a, T& b);

template <typename T>
void Swapp(T* a, T* b);

int main() {

    int m, n;
    while(cin >> m >> n) {
        Swapp(&m, &n);
        //Swap(m, n);
        cout << m << " " << n << endl;
    }

    return 0;
}

template <typename T>
void Swap(T& a, T& b) {
    T temp;
    temp = a;
    a = b;
    b = temp;
}
template <typename T>
void Swapp(T* a, T* b) {
    T temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

发表于 2023-05-01 21:00:30 回复(0)
//异或求解
#include <iostream>
using namespace std;

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

int main()
{

    int m, n;
    cin >> m;
    cin >> n;    

    swap(m,n);
   
    cout << m << " " << n << endl;

    return 0;
}
发表于 2023-02-03 19:32:26 回复(0)
//初学编程 加油~~
#include <stdio.h>

void to_exchange(int *&p) {
    int tmp;
    tmp = p[0];
    p[0] = p[1];
    p[1] = tmp;
}
int main() {
    int a[2];
    int *p=a;
    int i;
    for ( i=0; i <2; ++i) {
        scanf("%d",p+i);
    }
    to_exchange(p);
    for (int j = 0; j < 2; ++j) {
        printf("%d ",*(p+j));
    }
    return 0;
}
发表于 2023-01-29 19:36:03 回复(0)