指针练习——变量交换
时间: 1ms 内存:128M
描述:
指针的功能多种多样,指针是c语言的灵魂,所以说掌握指针是很重要的。
下面要求你用指针实现两个数字的交换
输入:
两个int型的变量
输出:
交换后的两个变量
示例输入:
1 2
示例输出:
2 1
提示:
参考答案(内存最优[0]):
#include<stdio.h>
int main()
{
int a,b;
int *c=&a,*d=&b;
void exc(int*,int*);
scanf("%d%d",&a,&b);
exc(c,d);
printf("%d ",a);
printf("%d",b);
return 0;
}
void exc(int*p,int*q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}
参考答案(时间最优[0]):
#include<iostream>
using namespace std;
int main()
{
int a,b;
int *c=&a,*d=&b;
void exc(int*,int*);
cin>>a>>b;
exc(c,d);
cout<<a<<" "<<b<<endl;
return 0;
}
void exc(int *a,int *b)
{
int c;
c=*b;
*b=*a;
*a=c;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。
