当前位置:网站首页>color socks problem
color socks problem
2022-08-10 02:12:00 【swofford】
题目:
思路:数学问题
Find at least how many socks to make a pair,That is, one sock of each color,再加1只 Can make up a pair!
如:2 2 1 ,则只需要3+1=4Can only make up a pair;
But if there is only one sock of each color or0只,No amount of color socks can make up a pair!
如:1 1 1 0 1 ,Can't get it together,返回-1;
所以,At least one sock color is larger than1只 !
使用singleThe variable record is greater than1the number of socks,如果single=0,ie all color socks are≤ 1,Can't make a pair,返回-1;
注意: 输入数据有多组,使用外层for循环,遍历输入;
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++){
// Iterate over different test data
// 数组长度
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;
// Take one for each colornum
// Singular numbers are not includednum
if(color[i]>1){
single++;
num++;
}else if(color[i]==1){
//
num++;
}
}
if(single==0){
// That is, each color is an OR0 只,You can't get a pair
System.out.println(-1);
}else{
System.out.println(num+1);
}
}
}
}
边栏推荐
猜你喜欢
Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
03|Process Control
What should I do if there is no sound after reinstalling the system in win10?
XSS详解及复现gallerycms字符长度限制短域名绕过
PEG derivative Biotin-PEG1-OH (cas: 95611-10-2, 2-biotinaminoethanol) advantage description
Fedora 36 dnf 安装ModSecurity和 OWASP 核心规则集
Biotin-Cy2 Conjugate, Biotin-Cy2 Conjugate_Cy2 Biotin Conjugate
GBJ1510-ASEMI机器人电源整流桥GBJ1510
unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla
MySQL最大连接数限制如何修改
随机推荐
Fedora 36 dnf 安装ModSecurity和 OWASP 核心规则集
什么是持续测试?
Win11怎么关闭系统保护功能?系统保护还原功能怎么关闭?
什么是一网统管?终于有人讲明白了
CAS:851113-28-5 (生物素-ahx-ahx-酪胺)
跨部门沟通的技巧
y92.第六章 微服务、服务网格及Envoy实战 -- Envoy基础(三)
02| operator
芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长
win10重装系统后没声音怎么办?
365天挑战LeetCode1000题——Day 052 逐步求和得到正数的最小值 贪心
03|Process Control
What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?
Solidity最强对手:MOVE语言及新公链崛起
破产企业的职工退休怎么办?
使用 apxs 构建和安装 Apache 扩展共享对象模块
阿里云混合云管理平台多Region架构
Penetration Testing and Offensive and Defense Confrontation - Vulnerability Scanning & Logic Vulnerability (Part1)
Mysql数据库 ALTER 基本操作
Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection