码迷,mamicode.com
首页 > 其他好文 > 详细

两个函数

时间:2015-05-04 11:28:07      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

编写两个函数,其功能分别是:

① 求N个整数的次大值和次小值。

② 求两个整数的最大公约数和最小公倍数。

 

#include<iostream>

using namespace std;

void main()

{

         int secondlarge, secondsmall;

         int *p1;

         int *p2;

         int n;

         cout<<"输入数目的个数"<<endl;

         cin>>n;

         p1=new int [n];

         p2=new int[n];

         cout<<"输入"<<n<<"个数"<<endl;

 

 

         for(int i=0;i<n;i++)

         {

                   cin>>p1[i];

                   p2[i]=p1[i];

         }

 

         for(int j=0;j<n;j++)

         {

                   int counter1=0;

                   int counter2=0;

 

 

                   for(int k=0;k<n;k++)

                   {

                            if(p1[j]>=p2[k])

                                     counter1++;

                            if(p1[j]<=p2[k])

                                     counter2++;

                   }

  

                   if(counter1==n-1)

                            secondlarge=p1[j];

                   if(counter2==n-1)

                            secondsmall=p1[j];

         }

        

         cout<<"次大数为 "<<secondlarge<<endl;

         cout<<"次小数为 "<<secondsmall<<endl;

         cin.get();

         cin.get();

}

 

#include <iostream>

using namespace std;

 

int main()

{

         int i,j,k,r,m;

         cout<<"请输入两个数:"<<endl;

         cin>>i>>j;

         m=i*j;

        

         if(i<j)

         {

                   k=i;

                   i=j;

                   j=k;

         }

        

         if(i%j!=0)

         {

                   r=i%j;

                   i=j;

                   j=r;

         }

        

         m=m/j;

         cout<<i<<"和"<<j<<"的最大公因数为:"<<j<<endl;

         cout<<i<<"和"<<j<<"的最小公倍数为:"<<m<<endl;

 

         return 0;

}

两个函数

标签:

原文地址:http://www.cnblogs.com/cuiyuan1996/p/4475430.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!