登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

三个数大小比较  

2006-02-23 22:00:45|  分类: C/C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
//1_16
/*
  Name: 1_16 
  Copyright: 秒大刀 
  Author: 秒大刀 
  Date: 24-02-05 22:27
  Description: 试写一个算法,自大至小依次输出顺序读入的三个整数X、Y和Z的值。 
  Algorithm description:利用三次比较得出结果
*/
struct num3
{
    int first,second,third;
};
num3 sort3(const num3&  In)
{
    num3 Out;
    if(In.first>In.second)
    {
        Out.first=In.first;
        Out.third=In.second;
    }
    else
    {
        Out.first=In.second;
        Out.third=In.first;
    }
    //Until now,Out.first>Out.third
    if(Out.third<In.third)
    {
        Out.second=In.third;
    }
    else
    {
        Out.second=Out.third;
        Out.third=In.third;
    }
    //Until now,the min is in Out.third
    if(Out.first>Out.second)
    {
        return Out;//We have got the answer.
    }
    else
    {//Swap Out.first and Out.second.
        int temp=Out.first;
        Out.first=Out.second;
        Out.second=temp;
    }
    return Out;//At last we make it.
  
}
//The next is test code:
#include<iostream>
using namespace std;
int main()
{
    while(1)
    {
            cout<<"请输入三个数:"<<std::flush; 
            num3 In;
            cin>>In.first>>In.second>>In.third;
            num3 Out=sort3(In);
            cout<<endl<<In.first<<','<<In.second <<','<<In.third<<"----SORT---->"
              <<Out.first<<','<<Out.second<<','<<Out.third<<endl<<endl;
    }
 return 0;
}


  评论这张
 
阅读(1283)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018