当前位置:网站首页>彩色袜子题
彩色袜子题
2022-08-10 00:41:00 【斯沃福德】
题目:

思路:数学问题
求至少多少袜子能凑出一对,即每种颜色的袜子各一只时,再加1只 就能凑出一双!
如:2 2 1 ,则只需要3+1=4只就能凑出一双;
但如果每种颜色的袜子只有一只或者0只,再多颜色的袜子也凑不出一双!
如:1 1 1 0 1 ,怎么凑都不行,返回-1;
所以,至少要有一种袜子的颜色要大于1只 !
使用single变量记录大于1的袜子的数量,如果single=0,即所有颜色袜子都≤ 1,则无法凑出一双,返回-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++){
// 遍历不同测试数据
// 数组长度
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);
}
}
}
}
边栏推荐
- C language pointer practice questions
- Quick responsiveness intelligent/smart responsiveness of polyethylene glycol type nano/reduction response hydrogels research and preparation
- 输入的这些数是否对称
- 《痞子衡嵌入式半月刊》 第 60 期
- 有PEG-Biotin参与的(CAS:1778736-18-7)Biotin-PEG4-OH广泛用于分子靶点检测
- PEG 衍生物Biotin-PEG1-OH(cas:95611-10-2,2-生物素氨基乙醇)优势说明
- -Pickling peanuts-
- 由生物素参与的D-Biotinol,CAS号:53906-36-8具体特性说明
- ASEMI整流桥GBJ1010参数,GBJ1010规格,GBJ1010封装
- 3511. 倒水问题
猜你喜欢

PEG 衍生物Biotin-PEG1-OH(cas:95611-10-2,2-生物素氨基乙醇)优势说明

为什么字符串一旦创建就不可以改变?

365 days challenge LeetCode1000 questions - Day 052 Step by step summation to get the minimum value of positive numbers Greedy

【kali-密码攻击】(5.2.1)密码分析:Hash Identifier(哈希识别)

ITK编译remote库

西安生物素-四聚乙二醇-酰胺-4苯酚 浅黄色半固态

D-Biotinol Involved by Biotin, CAS No: 53906-36-8 Specific Properties Description

win10重装系统后没声音怎么办?

温度响应性纳米水凝胶光子品体/纤维素修饰荧光水凝胶/载脂质体水凝胶的制备方法

《痞子衡嵌入式半月刊》 第 60 期
随机推荐
el-input保留一位小数点
Win11怎么关闭系统保护功能?系统保护还原功能怎么关闭?
Mysql数据库 ALTER 基本操作
微信公众号如何开通支付功能?
Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
How to activate the payment function on WeChat official account?
assert利用蚁剑登录
Mysql database ALTER basic operations
解决sed替换文本,里面含有“/“、“#”等特殊字符的问题
渗透测试与攻防对抗——漏洞扫描&逻辑漏洞(Part1)
PEG 衍生物Biotin-PEG1-OH(cas:95611-10-2,2-生物素氨基乙醇)优势说明
微信小程序tab切换时保存checkbox状态
How to add control panel to right click menu in win7
Minimum number of steps to get out of the maze 2
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
Solidity最强对手:MOVE语言及新公链崛起
shell指定参数名传参
【kali-密码攻击】(5.1.2)密码在线破解:Medusa
eyb:Redis学习(4)
Solving for the number of mines