您的位置:首页 > 编程语言 > C语言/C++

输入三个数字比较大小 更多数字比较大小 C++

2009-11-04 16:08 1196 查看
 昨天,我的大学同学(也是我的老师,他编程很厉害,佩服他)叫我用C++编写一个三个数字比较大小的小程序,我写出来了如下:

#include <iostream>
#include "conio.h"

using namespace std;
void compare(int &a, int &b, int &c)
{
 int t
 if(a < b){t = a; a = b; b = t;}
 if(a < c ){  t= a; a = c; c = t;}
 if( b < c){t = b; b = c; c = t;}

 cout << "值最大的整数: " << a << endl;
 cout << "位于中间值的整数: " << b << endl;
 cout << "值最小的整数: " << c << endl;
  
}

int _tmain(int argc, _TCHAR* argv[])
{
 int a, b, c;
 cout << "输入三个数字,中间用空格隔开" << endl;
 cin >> a >> b >> c;

 while (a == b || a == c || b == c)
 {
  cout << "请输入三个不同的数:" << endl;
  cin >> a >> b >> c;
 }

 compare(a, b, c);

 printf("请按任意键退出/r/n");
 getch();

}

 

方法很多种,我就写了这种,比较简单的。

编写完了,他叫我写更多的数字比较大小呢?怎么写,我郁闷了,那不是要写很多?

后来,他不用5分钟编写了个,比较有深度的,用到指针和数组的,在这里给大家分享下:

#include "stdafx.h"
#include <stdio.h>

int getnum(int *lp)
{
 int n=0,i;
 for(i=1; i<6; i++){
  if( lp
<=lp[i] ) n=i;
 }
 i = lp
;
 lp
=-0x7fffffff;

 return i;
}

int main()
{
 int i,a[6];
 for(i=0; i<6; i++){scanf("%d", &a[i]);getchar();}

 for(i=0; i<6; i++) printf("%d ", getnum(a));
 getchar();
}

代码也不多,但是知识点包含几个,程序很不错,希望大家能明白。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c c++ 编程