有一个投篮游戏。球场有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
import java.util.*;public class Main{public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { boolean success = true; int ind = -1; int p = sc.nextInt();//篮筐数 int n = sc.nextInt();//篮球数 //记录篮袋里是否已有篮球 int[]book = new int[p]; //记录篮球上的数字 int[]x = new int[n]; for(int i=0;i<n;i++){ x[i] = sc.nextInt(); } for(int i=0;i<n;i++){ if(book[x[i]%p]==1){ success = false; ind = i+1; break; } else book[x[i]%p] = 1; } System.out.println(ind); } }}
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in=new Scanner(System.in); while(in.hasNext()){ int p = in.nextInt();//篮筐数 int n = in.nextInt();//篮球数 int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=in.nextInt(); } int[] flag=new int[10];//作为哈希表的数组 int end=-1; for(int i=0;i<n;i++){ int index=arr[i]%p; int row=index/32; int column=index%32; if((flag[row]&(1<<column))!=0){ end=i+1; break; }else{ flag[row]=flag[row]|(1<<column); } } System.out.println(end); } } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while((line = br.readLine()) != null){ String[] params = line.split(" "); int p = Integer.parseInt(params[0]); int n = Integer.parseInt(params[1]); boolean[] bag = new boolean[p]; // 除以p的余数只有可能是0~p-1 int[] x = new int[n + 1]; for(int i = 1; i <= n; i++) x[i] = Integer.parseInt(br.readLine()); int i = 1; while(i <= n){ if(bag[x[i] % p]) break; else bag[x[i] % p] = true; i++; } System.out.println(i > n? -1: i); } } }
#include <bits/stdc++.h> using namespace std; int main() { int p,n,x[310],c[310]; while(cin>>p>>n) { int result=-1; memset(c,0,sizeof(c)); for(int i=0;i<n;i++) cin>>x[i]; for(int i=0;i<n;i++) { if(c[x[i]%p]==1) { result = i+1; break; }else c[x[i]%p]++; } cout<<result<<endl; } return 0; }
#include<iostream> #include<vector> using namespace std; int main() { int p,n; while(cin>>p>>n) { vector<bool> basket(p); vector<int> ball(n); for(int i=0;i<n;i++) { cin>>ball[i]; } int num=-1; for(int i=0;i<n;i++) { if(!basket[ball[i]%p]) basket[ball[i]%p]=1; else { num=i; break; } } if(num!=-1) cout<<num+1<<endl; else cout<<-1<<endl; } return 0; }
#include<iostream> using namespace std; int main(){ int p,n; while( cin >> p >> n){ int a[301],b[301]={0},i,x; for(i=0;i<n;i++){ cin >> a[i]; } for(i=0;i<n;i++){ x = a[i] % p; if(b[x]!= 1){ b[x] = 1; } else{ cout << i+1<<endl; break; } } if(i == n) cout << -1 <<endl; } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int end = -1; int p = in.nextInt(); int n = in.nextInt(); int[] x = new int[n]; for(int i = 0;i < n;i++){ x[i] = in.nextInt(); } int[] judge = new int[p]; for(int i = 0;i < n;i++){ if(judge[x[i] % p] == 1){ end = i + 1; break; } else judge[x[i] % p] = 1; } System.out.println(end); } } }
import java.util.Scanner; public class Mugu4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int p = scanner.nextInt(); int n = scanner.nextInt(); int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = scanner.nextInt(); } System.out.println(getIndexOfBas(p, n, x)); } } public static int getIndexOfBas(int p, int n, int[] x) { boolean[] hasBas = new boolean[p]; for(int i=0;i<n;i++){ int index = x[i] % p; if(!hasBas[index]){ hasBas[index] = true; } else{ return i+1; } } return -1; } }
#include <stdio.h> int main() { int i; int j; int temp; int num[10]; int basket; int flag = 0; int blankNum; int basketNum; int record = -1; while((scanf("%d%d",&blankNum,&basketNum) == 2)) { int num[blankNum]; for(i = 0; i < blankNum; i++) { num[i] = 0; } for(i = 0; i < basketNum; i++) { scanf("%d",&basket); temp = basket%blankNum; if(num[temp]) { flag++; if(1 == flag) { record = i+1; } } else { num[temp] = 1; } } if(flag != 0) { printf("%d\n",record); flag = 0; } else { printf("-1\n"); } } return 0; }
#include <iostream> #include <string> #include <iomanip> #include <vector> #include <algorithm> #include <string.h> #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define MAXN 400 using namespace std; int p,n; int x[MAXN]; bool empty[MAXN]; int main(){ ios::sync_with_stdio(0); while(cin>>p>>n){ REP(i,n) cin>>x[i]; memset(empty,0,sizeof(empty)); int re=-1; REP(i,n) if(empty[x[i]%p]){re=i+1; break;}else empty[x[i]%p]=1; cout<<re<<endl; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) {//注意while处理多个case int p = in.nextInt(); int n = in.nextInt(); int[] x = new int[n]; int[] a = new int[p]; boolean flag = true; for(int i = 0;i<n;i++){ x[i] = in.nextInt(); } for(int i = 0;i<n&&flag;i++){ int mod = x[i]%p; if(a[mod]==0){ a[mod] = 1; }else{ System.out.println(i+1); flag = false; } } if(flag) System.out.println(-1); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNextInt()){ int p=sc.nextInt(); int n=sc.nextInt(); if(n==0){//测试用例含有 127 0 输出-1 System.out.println(-1); continue; } int[] basketball=new int[n]; boolean[] bl=new boolean[p]; for(int i=0;i<n;i++){//必须先输入完再判断 否则超时 basketball[i]=sc.nextInt(); } for(int i=0;i<n;i++){ int x=basketball[i]%p; if(bl[x]==true){ System.out.println(i+1); break; } bl[x]=true; if(i==n-1) System.out.println(-1); } } } }
用哈希表实现,感觉还是蛮清晰的 #include <iostream> using namespace std; int main() { int p,n; while(cin>>p>>n){ int *data=new int[n]; int *hash_table=new int[p]; int val=-1; for(size_t i=0;i<n;i++) cin>>data[i]; for(size_t i=0;i<p;i++) hash_table[i]=0; for(size_t i=0;i<n;i++){ hash_table[data[i]%p]++; if(hash_table[data[i]%p]>1){ val=i; break; } } if(val!=-1) cout<<val+1<<endl; else cout<<-1<<endl; delete[] data; delete[] hash_table; } }
为什么都写得这么长,用集合来解感觉很简短。 这一题挺简单的,但是写之前看到了下面的评论给坑了,以为如果出现重复, 要在数据输入完成前就终止此次xi的接收,实际上不是的,必须完整地接收 n 个 x,最后再给出结果。 #include<iostream> #include <set> using namespace std; int main() { int p, n; //篮筐数,篮球数 int x, i; while(cin >> p >> n){ int flag=0; //作用:1.判断是否有冲突 2.如果有冲突则记录第一个冲突的序号 set<int> myset; //每一批输入样例构造一个空集合 for(i=1; i<=n; i++){ //以1开始,便于这个题目处理 cin >> x; if(myset.find(x%p)!=myset.end() && !flag) flag = i; //注意输入示例,只记录第一次冲突的,后面再有冲突的并不记录 else myset.insert(x%p); } if(flag) cout << flag << endl; // 有冲突 else cout << -1 << endl; // 没有冲突 } return 0; }
// 题比较简单,看到有人说没通过测试,是因为原题是在ACM上考的,要使用ACM“风格”--main里的while大循环。 // 本人水手较低,代码写的不好,轻拍! #include <iostream> using namespace std; int main(int argc, const char * argv[]) { // insert code here... int p, n, t, i; long x[300]; while (cin >> p >> n) { if (p < 2 || n < 2 || p > 300 || n > 300) { cout << -1 << endl; continue; } int a[300] = {0}; for (i = 0; i < n; i++) { cin >> x[i]; } for (i = 0; i < n; i++) { t = x[i]%p; if (a[t] == 0) a[t] = 1; else { cout << i+1 << endl; break; } } if (i == n) cout << -1 << endl; } return 0; }