首页 > 试题广场 >

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

[编程题]编写函数实现两数交换(指针方式)
  • 热度指数:14800 时间限制: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)
值传递,子函数里修改的值不能传递回主函数,但是修改的地址是可以传回去的。指针就是地址

#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)
#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)
 cout << n << " " << m << endl;
编辑于 2024-03-01 15:15:07 回复(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)
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......


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)
#include<stdio.h>
int main()
{
    int m, n, x;
    scanf("%d %d",&m,&n);//键盘输入两个整数m,n。

    //定义指针变量将m,n的值存入指针
    int* xx = &x;
    int* mm = &m;
    int* nn = &n;

    //交换m,n的值
    *xx = *mm;
    *mm = *nn;
    *nn = *xx;

    //打印交换了值的m,n
    printf("%d %d",m,n);
    return 0;
}
发表于 2022-12-30 22:39:21 回复(0)
#include <iostream>
using namespace std;

void Swaps(int*x,int*y)
{
    int temp = *x;
    *x=*y;
    *y=temp;
}
int main() 
{
    int m,n;
    cin>>m>>n;
    Swaps(&m, &n);
    cout<<m<<" "<<n;
    return 0;
}

发表于 2022-12-13 10:08:33 回复(0)
#include<stdio.h>
void exchange(int *a,int *b);
int main()
{
	int c,d;
	scanf("%d %d",&c,&d);
	exchange(&c,&d);
	printf("%d %d",c,d);
	return 0;
}
void exchange(int *a,int *b)
{
	int c;
	c=*a;
	*a=*b;
	*b=c;
}

发表于 2022-11-19 10:45:34 回复(0)
#include <iostream>
using namespace std;
void fun(int *a,int *b){
    int t;
    t=*a;
    *a=*b;
    *b=t;
    cout<<*a<<" "<<*b;
}

int main() {
    int a, b;
    cin>>a>>b;
    fun(&a,&b);
}
// 64 位输出请用 printf("%lld")
发表于 2022-10-30 14:33:12 回复(0)
#include <iostream>
using namespace std;

void swap1(int* p,int* q);
void swap2(int &a,int &b);

void swap1(int* p,int* q){
    int tmp = *p;
    *p = *q;
    *q = tmp;
}
void swap2(int &a,int &b){
    int tmp = a;
    a = b;
    b = tmp;
}
int main() {
    int m,n;
    cin >> m;
    cin >> n;

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

    return 0;
}
// 64 位输出请用 printf("%lld")
发表于 2022-10-24 11:17:33 回复(0)
#include <iostream>

using namespace std;

void swap(int* m,int* n){
  int temp=*m;
  *m=*n;
  *n=temp;
  cout <<*m <<' ' <<*n;
}

int main(){
  int m,n;
  cin >>m >>n;
  swap(&m,&n);

  return 0;
}

发表于 2022-10-06 08:03:46 回复(0)
//纯纯的C
#include<stdio.h>

int main(){

    int m , n;
    int* p = &m;
    int* q = &n;
    scanf("%d\n%d", p, q);

    printf("%d %d", *q, *p);

    return 0;
}

发表于 2022-09-28 22:31:57 回复(0)