站点图标 陌路寒暄

指针练习--变量交换

指针练习--变量交换

时间: 1ms        内存:128M

描述:

注:本题只需要提交填写部分的代码

用指针变量对两个整数按从小到大排序。

/*C++*/

#include <iostream>
using namespace std;
int main()
{
    int *p1,*p2,*p;
    int a,b;
    cin>>a>>b;
    p1=&a;
    p2=&b;
    if(a>b)
    {
    /*******************************
     请在该部分补充缺少的代码
    ********************************/

    }
    cout<<"min:"<<*p1<<endl;
    cout<<"max:"<<*p2<<endl;
    return 0;
}

/*C*/

#include<stdio.h>
int main()
{
    int *p1,*p2,*p;
    int a,b;
    scanf("%d%d",&a,&b);
    p1=&a;
    p2=&b;
    if(a>b)
    {
    /*******************************
     请在该部分补充缺少的代码
    ********************************/

    }
    printf("min:%d\n",*p1);
    printf("max:%d\n",*p2);
    return 0;
}

输入:

两个整数

输出:

按从小到大输出两个整数

示例输入:

2 1

示例输出:

min:1
max:2

提示:

参考答案(内存最优[1092]):


#include<stdio.h>
int main()
{
    int *p1,*p2,*p;
    int a,b;
    scanf("%d%d",&a,&b);
    p1=&a;
    p2=&b;
    if(a>b)
    {
  {
    p=p1; 
    p1=p2; 
    p2=p; 
    } 

}
    printf("min:%d\n",*p1);
    printf("max:%d\n",*p2);
    return 0;
}

参考答案(时间最优[0]):


#include <iostream>
using namespace std;
int main()
{
    int *p1,*p2,*p;
    int a,b;
    cin>>a>>b;
    p1=&a;
    p2=&b;
    if(a>b)
    {
        p=p1;
        p1=p2;
        p2=p;
    }
    cout<<"min:"<<*p1<<endl;
    cout<<"max:"<<*p2<<endl;
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版