现在的问题是,最少要从抽屉中取出多少只袜子才能保证其中一定有两只可以配成颜色相同的一双?
第一行一个数字表示测试数据的组数。
对于每组数据,第一行数字表示袜子的颜色种数。
第二行有个数字,第
个数字
表示第
种颜色的袜子有
个。
对于每组数据,输出一行一个数字表示答案。若无解输出 -1。
2 2 2 2 3 0 0 0
3 -1
T = int(input()) while T: n = int(input()) arr = list(map(int, input().split())) ma = max(arr) if ma < 2: print(-1) else: cnt = 0 for num in arr: if num != 0: cnt += 1 print(cnt + 1) T -= 1
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in=new Scanner(System.in); int T=in.nextInt(); for(int t=0;t<T;t++){ // 遍历不同测试数据 // 数组长度 int n=in.nextInt(); int[] color=new int[n]; int single=0; int num=0; for(int i=0;i<n;i++){ int temp=in.nextInt(); color[i]=temp; // 每种颜色取一个就是num // 若为单数则不算入num if(color[i]>1){ single++; num++; }else if(color[i]==1){ // num++; } } if(single==0){ // 即每种颜色都是一只或0 只,则无法凑一双 System.out.println(-1); }else{ System.out.println(num+1); } } } }
n = int(input()) list1 = [] list2 = [] for i in range(n): list1.append(list(map(int, input().split()))) list2.append(list(map(int, input().split()))) for i in range(n): num = 0 if max(list2[i]) <= 1: print(-1) elif max(list2[i]) >= 2: for j in list2[i]: if j > 0: num += 1 print(num+1)
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; function findminsocks(socks){ let single = 0 let minnum = 0 for(let i=0;i<socks.length;i++){ if(socks[i]>1){ single++ minnum++ }else if(socks[i]===1){ minnum++ } } if(single===0){ console.log(-1) }else{ console.log(minnum+1) } } void async function () { // Write your code here line = await readline() while(line){ var types = await readline() var sock = await readline() var socks = sock.split(' ').map(Number) findminsocks(socks) line-- } }()JS实现如上
// 优化前的版本 public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt();// T为测试数据组数 for (int i = 0 ; i < T ; i ++){ int n = in.nextInt();// n是袜子颜色的种类数 int[] arr = new int[n]; // 某种颜色的袜子数量 for (int j = 0 ; j < n ; j ++){ arr[j] = in.nextInt(); } // arr从小到大排序,最大值<=1则返回-1。否则返回种类数n中不为零的颜色种类数+1 Arrays.sort(arr); if (arr[n-1] <= 1) System.out.println(-1); else // 这里应该遍历arr,统计元素不为零的个数count,然后输出count+1。但我觉得前面排序就花了一部分时间了,再从头遍历统计的话太耗时了。就开始想怎么优化。 System.out.println(n+1); } } // 最后优化过的版本 public void success() { Scanner in = new Scanner(System.in); int T = in.nextInt();// T为测试数据组数 for (int i = 0; i < T; i++) { int n = in.nextInt();// n是袜子颜色的种类数 int max = 0; // 一组袜子中的最大值。是判断能不能成双的关键!max<=1直接返回-1 int nonZeroCount = 0; // 记录一组袜子中颜色不为零的数量 for (int j = 0; j < n; j++) { int input = in.nextInt(); max = Math.max(max, input); if (input != 0) { nonZeroCount++; } } if (max <= 1) System.out.println(-1); else System.out.println(nonZeroCount + 1); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int i=0;i<T;i++){ int kind = in.nextInt(); int count = 0;//jizhong int flag = 0;//youmeiyou >2 int[] color = new int[kind]; for(int j=0;j<kind;j++){ color[j] = in.nextInt(); if(color[j]!=0){ count++; if(color[j]>=2) flag++; } } if(count==0) System.out.println(-1); else if(count>0&&flag==0) System.out.println(-1); else System.out.println(count+1); } } }
#include <iostream> #include <vector> using namespace std; int main() { int T; cin >> T; while(T--){ int n; cin >> n; int num; int ans = 0; int num_1 = 0; for(int i = 0; i < n; i++){ cin >> num; if(num == 1) num_1++; if(num >= 1) ans++; } if(ans == num_1) cout << -1 << endl; else cout << ans+1 << endl; } }
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const inputArr = [];//存放输入的数据 let flag = 0; rl.on('line', function(line){ //line是输入的每一行,为字符串格式 inputArr.push(line.split(' ').map(item=>parseInt(item)));//将输入流保存到inputArr中(注意为字符串数组) if(flag === inputArr[0][0] * 2){ rl.close() } flag++ }).on('close', function(){ fun(inputArr); }) //解决函数 function fun(arr) { let zushu = arr[0][0] let start = 2 let count = 0 while(zushu--){ let max = Math.max.apply(null,arr[start])&nbs***bsp;let max = Math.max(...arr[start]) if(max>1){ for(let i=0;i<arr[start].length;i++){ if(arr[start][i]!==0){ count++ } } console.log(++count) count = 0 }else{ console.log(-1) } start = start+2 } }
//import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int i = 0; i < T; i++) { int n = sc.nextInt(); int cnt = 0; boolean hasTwo = false; for (int j = 0; j < n; j++) { int cur = sc.nextInt(); if (cur > 0) { cnt++; if (cur > 1) { hasTwo = true; } } } if (cnt == 0 || !hasTwo) { System.out.println(-1); } else { System.out.println(cnt + 1); } } sc.close(); } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int T = in.nextInt(); for(int i = 0; i < T; i++){ int numLength = in.nextInt(); int count = 0; int flag = 0; int tmp = 0; for(int j=0; j < numLength; j++){ int a = in.nextInt(); //至少有一个颜色的数量大于2才有可能得到一双 flag = a>=2?(flag+1):flag; count = a>0?(count+1):count; } tmp = (count>0 && flag > 0 )?(count+1):-1; System.out.println(tmp); } } }