zoukankan      html  css  js  c++  java
  • [编程题-蘑菇街] 投篮游戏

    [编程题-蘑菇街] 投篮游戏

    有一个投篮游戏。球场有p个篮筐,编号为0,1...,p-1。每个篮筐下有个袋子,每个袋子最多装一个篮球。有n个篮球,每个球编号xi 。规则是将数字为xi 的篮球投到xi 除p的余数为编号的袋里。若袋里已有篮球则球弹出游戏结束输出i,否则重复至所有球都投完。输出-1。问游戏最终的输出是什么?

    输入描述:
    第一行两个整数p,n(2≤p,n≤300)。p为篮筐数,n为篮球数。接着n行为篮球上的数字xi(0≤xi≤1e9)



    输出描述:
    输出游戏的结果
    输入例子:
    10 5
    0
    21
    53
    41
    53
    输出例子:
    4
    #include<iostream>
    #include<set>
    using namespace std;
    int main()
    {
        long p,n;
        while(cin>>p>>n)
        {
            set<long> s;
            int flag=false;
            for(int i=0;i<n;i++)
            {
                int x;
                cin>>x;
                if(flag) continue;
                if(s.find(x%p)==s.end())
                    s.insert(x%p);
                else
                {
                    cout<<i+1<<endl;
                    flag=true;
                }
                    
            }
            if(!flag) cout<<"-1"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    176. Second Highest Salary
    175. Combine Two Tables
    172. Factorial Trailing Zeroes
    171. Excel Sheet Column Number
    169. Majority Element
    168. Excel Sheet Column Title
    167. Two Sum II
    160. Intersection of Two Linked Lists
    个人博客记录
    <meta>标签
  • 原文地址:https://www.cnblogs.com/learning-c/p/5744307.html
Copyright © 2011-2022 走看看