zoukankan      html  css  js  c++  java
  • ZOJ Problem Set–1101 Gamblers

    Time Limit: 2 Seconds      Memory Limit: 65536 KB


    A group of n gamblers decide to play a game:

    At the beginning of the game each of them will cover up his wager on the table and the assitant must make sure that there are no two gamblers have put the same amount. If one has no money left, one may borrow some chips and his wager amount is considered to be negative. Assume that they all bet integer amount of money.

    Then when they unveil their wagers, the winner is the one who's bet is exactly the same as the sum of that of 3 other gamblers. If there are more than one winners, the one with the largest bet wins.

    For example, suppose Tom, Bill, John, Roger and Bush bet $2, $3, $5, $7 and $12, respectively. Then the winner is Bush with $12 since $2 + $3 + $7 = $12 and it's the largest bet
    Input

    Wagers of several groups of gamblers, each consisting of a line containing an integer 1 <= n <= 1000 indicating the number of gamblers in a group, followed by their amount of wagers, one per line. Each wager is a distinct integer between -536870912 and +536870911 inclusive. The last line of input contains 0.
    Output
    For each group, a single line containing the wager amount of the winner, or a single line containing "no solution".
    Sample Input
    5
    2
    3
    5
    7
    12
    5
    2
    16
    64
    256
    1024
    0
    Output for Sample Input
    12
    no solution
     
    千万不要自作聪明!一个数不是一定是比它小的几个数的和的,也可能是比它大的数和负数的和。代码如下:
    #include<iostream>
    
    #include<set>
    
    using namespace std;
    
    int main()
    
    {
    
      int gamblers;
    
      while(cin>>gamblers && gamblers)
    
      {
    
        if(gamblers < 4)
    
        {
    
          cout<<"no solution"<<endl;
    
          continue;
    
        }
    
        int *wagers = new int[gamblers];
    
        set<int> se;
    
        int wager;
    
        while(gamblers-- && cin>>wager)
    
        {
    
          se.insert(wager);
    
        }
    
        int arrIndex = 0;
    
        for(set<int>::reverse_iterator it = se.rbegin(); it != se.rend(); it++)
    
        {
    
          *(wagers + arrIndex) = *it;
    
          arrIndex++;
    
        }
    
        int winWager;
    
        for(int winner = 0; winner < arrIndex; winner++)
    
        {
    
          winWager = *(wagers + winner);
    
          for(int i = 0; i < arrIndex; i++)
    
          {
    
            for(int j = 0;j < arrIndex; j++)
    
            {
    
              if( winner != i && winner != j && i != j)
    
              {
    
                int last = winWager - (*(wagers + i) + *(wagers + j));
    
                if(last != winWager 
    
                  && last != *(wagers+i) 
    
                  && last != *(wagers+j) 
    
                  && se.find(last) != se.end())
    
                {
    
                  cout<<winWager<<endl;
    
                  goto breakAll;
    
                }
    
              }
    
            }
    
          }
    
        }
    
        delete []wagers;
    
        cout<<"no solution"<<endl;
    
        breakAll:;
    
      }
    
      return 0;
    
    }
  • 相关阅读:
    游标本次循环,跳过某些语句
    SQL 把结果集的某列连加成一行
    DotNetty 跨平台的网络通信库(转)
    Slickflow.NET 开源工作流引擎基础介绍-.NET Core2.0 版本实现介绍 (转)
    C# Winform应用程序占用内存较大解决方法整理(转)
    (转)vs2010 vs2013等vs中如何统计整个项目的代码行数
    如何将运维的报警做成运营的报警--Java后端架构
    阿里员工内部常用免费工具包 (转)
    ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl (转)
    C#与MATLAB混合编程
  • 原文地址:https://www.cnblogs.com/malloc/p/2407736.html
Copyright © 2011-2022 走看看