【2分】C. 三数论大小(指针)

题目描述

输入三个整数,然后按照从大到小的顺序输出数值。

要求:用三个指针分别指向这三个整数,排序过程必须通过这三个指针来操作,不能直接访问这三个整数

输出时,必须使用这三个指针,不能使用存储三个整数的变量

输入

第一行输入t表示有t个测试实例

第二行起,每行输入三个整数

输入t行

输入

每行按照从大到小的顺序输出每个实例

在每行中,每个数据输出后都带

第一种:使用纯指针
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{   //freopen("d:\\a.txt","r",stdin); //提交到后台时,要注释掉这行
    int t;
    cin>>t;

     while(t--){
         int *p,*q,*r;
         p=new int;
         q=new int;
         r=new int;
         cin>>*p>>*q>>*r;

         if(*p>*q){
            if(*r>*p)
                cout<<*r<<" "<<*p<<" "<<*q<<" "<<endl;
            else if(*r>*q)
                 cout<<*p<<" "<<*r<<" "<<*q<<" "<<endl;
            else
                cout<<*p<<" "<<*q<<" "<<*r<<" "<<endl;
          }
         else{
            if(*r>*q)
                cout<<*r<<" "<<*q<<" "<<*p<<" "<<endl;
            else if(*r>*p)
                cout<<*q<<" "<<*r<<" "<<*p<<" "<<endl;
            else
                cout<<*q<<" "<<*p<<" "<<*r<<" "<<endl;
          }
     }
     return 0;
}

---------------------------------------------
二、利用排序算法,对三个数进行排序
#include<iostream>
#include<stdio.h>
using namespace std;
void sort1(int* a,int* b,int* c);
main()
{   //freopen("d:\\a.txt",  "r", stdin);  //提交到后台时,要注释掉这行
    int t,*a,*b,*c;
    cin>>t;
    while(t--){
        a=new int;
        b=new int;
        c=new int;
        cin>>*a>>*b>>*c;
        sort1(a,b,c);
        cout<<*a<<" "<<*b<<" "<<*c<<" "<<endl;
     }
}

void sort1(int* a,int* b,int* c){   //最简单的冒泡排序
     int s[3]={*a,*b,*c};
     int temp;
     for(int i=0;i<2;i++)
        for(int j=0;j<2-i;j++){
            if(s[j]<s[j+1]){
               temp=s[j];
               s[j]=s[j+1];
               s[j+1]=temp;
         }
     }
    *a=s[0];*b=s[1];*c=s[2];
}

-------------------------------------------------
三、利用sort函数
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

void compare(int* a,int* b,int* c);

main()
{   //freopen("d:\\a.txt",  "r", stdin);  //提交到后台时,要注释掉这行
    int t,*a,*b,*c;
    cin>>t;
    while(t--){
        a=new int;
        b=new int;
        c=new int;
        cin>>*a>>*b>>*c;
        compare(a,b,c);
        cout<<*a<<" "<<*b<<" "<<*c<<" "<<endl;
    }
}

void compare(int* a,int* b,int* c){
     int s[3]={*a,*b,*c};
     sort(s,s+3);
     *a=s[2];*b=s[1];*c=s[0];
}

有一个空格,即使该行最后一个数据输出后也要再输出一个空格

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值