现在的位置: 首页 > 综合 > 正文

第7周任务2-扩展练习1、2

2014年01月06日 ⁄ 综合 ⁄ 共 916字 ⁄ 字号 评论关闭
/* Copyright (c) 2012, 烟台大学计算机学院 
* All rights reserved. 
* 作    者: 徐凯旋 
* 完成日期:2012 年 10 月 13 日 
* 版 本 号:v1.0 
* 输入描述:请输入四个整数  
* 问题描述:求出给定的四个整数的最大值和最小值并输出  
* 程序输出:则此四个整数的最大值为 最小值为   
* 问题分析:略 
* 算法设计:略 
*/   

扩展一:输入四个整数,输出其中的最大值和最小值

#include <iostream>
using namespace std;
int main()
{
  int a,b,c,d,m,n,p,q,x,y;
  cout<<"请输入四个整数:";
  cin>>a>>b>>c>>d;
     if(a>b)
	    m=a,n=b;
	 else 
		m=b,n=a;
	 if(c>d)
		p=c,p=d;
	 else 
		p=d,q=c;
	 if(m>p)
		x=m;
	 else
		x=p;
	 if(n>q)
		y=q;
     else
		y=n;
  cout<<"则此四个整数中的最大值为:"<<x;
  cout<<" ";
  cout<<"最小值为:"<<y<<endl;
  return 0;
}      

运行结果:

扩展二: 

#include <iostream>
using namespace std;
int main()
{
  int a,b,c,d,e,f,m,n,p,q,t,w,x,y;
  cout<<"请输入四个整数:";
  cin>>a>>b>>c>>d;
     if(a>b)
	    e=a,f=b;
	 else 
		e=b,f=a;//"e"为前两数中较大的值;"f"为前两数中较小的值。
	 if(c>d)
		m=c,n=d;
	 else 
		m=d,n=c;//"m"为后两数中较大的值;"n"为后两数中较小的值。
	 if(e>m)
		x=e,p=m;
	 else
		x=m,p=e;//"x"为四数中的最大值。
	 if(f>n)
		q=f,y=n;
     else
		q=n,y=f;//"y"为四数中的最小值。
     if(p>q)
		t=p,w=q;
	 else
        t=q,w=p;//“t”为四数中的第二大值;“w”为四数中的第三大值。
  cout<<"则此四个整数按从大到小的顺序排列为:"<<x<<">"<<t<<">"<<w<<">"<<y<<endl;
  return 0;
}    

运行结果:

心得体会:

扩展二让人很无语啊,编着编着就晕了。还是加上些标注比较清楚。

抱歉!评论已关闭.