zoukankan      html  css  js  c++  java
  • SWUST OJ Gold Nuggets Distribution(0490)

    Gold Nuggets Distribution(0490)

    Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 421 Accepted: 234
     
    Description
    Bosses have a bag of gold. Each month there will be two employees to their outstanding performance were a gold award. In line with the rituals, ranking first employees will receive the heaviest nugget, ranked second employee will be the lightest gold. Under this method, unless the Nuggets to a new bag, otherwise employees received the first gold always be employees than the second-Nuggets. If there is a new cyclical Nuggets joined bag, every month to find the lightest and most of the Nuggets. Assuming that there is a comparison of weight machines, we hope that at least by comparison to identify the number and the lightest weight of gold.
    (题目描述就不翻译了,简而言之的意思就是,找出数组中的最大元素和最小元素,有个要求就是,比较的次数最少)
     
    Input
    two lines 
    the first line is a number of gold nuggets and less than 50000 
    the second line are weight of every gold nugget.
     
    Output
    two numbers 
    the first is weight of the heaviest nugget 
    the second is weight of the lightest gold
     
    Sample Input
    6
    5 2 7 6 3 4
     
    Sample Output
    7 2
     
     Hint
    Source
    MrYang
     
     1 #include<iostream>
     2 using namespace std;
     3 
     4 int a[2000000];
     5 
     6 int main()
     7 {
     8     int n,i,j,max=0,min=99999;
     9     cin>>n;
    10     for(i=0;i<n;i++)
    11         cin>>a[i];
    12     for(i=0,j=n-1;i<=j;i++,j--)
    13     {
    14         if(a[i]<a[j])
    15         {
    16             if(a[i]<min)
    17                 min=a[i];
    18             if(a[j]>max)
    19                 max=a[j];
    20         }
    21         else
    22         {
    23             if(a[j]<min)
    24                 min=a[j];
    25             if(a[i]>max)
    26                 max=a[i];
    27         }
    28     }
    29     cout<<max<<' '<<min<<endl;
    30     return 0;
    31 }

    注:两个数比较,则那个大的数,不可能成为这组数中最小的数,同理,那个小的数,不可能成为这组数中最大的数。从常用的 2*n 次比较,变成了(n/2)*3=1.5*n 次比较。

  • 相关阅读:
    嵌入式Linux学习笔记 NAND Flash控制器
    (嵌入式开发)自己写bootloader之编写第一阶段
    C_C++指针指针应用详解
    数据结构笔记-----二叉排序树和哈希表
    map方法和filter方法
    nginx服务器卡住了 解决办法
    vue-devtoools 调试工具安装
    web 本地存储(localStorage、sessionStorage)
    vux使用教程
    一个页面从输入 URL 到页面加载显示完成,这个过程中都发生了什么?
  • 原文地址:https://www.cnblogs.com/haveyoueverbeen/p/4488793.html
Copyright © 2011-2022 走看看