当前位置:网站首页>彩色袜子题
彩色袜子题
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);
}
}
}
}
边栏推荐
猜你喜欢
20220809-PotPlayer如何设置默认字体色-设置默认字体色的方法
Quick responsiveness intelligent/smart responsiveness of polyethylene glycol type nano/reduction response hydrogels research and preparation
快速响应性智能型/智能响应性聚乙二醇纳米/还原响应型水凝胶的研究与制备
ITK编译remote库
【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price
Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
CAS:851113-28-5 (Biotin-ahx-ahx-tyramine)
D-Biotinol Involved by Biotin, CAS No: 53906-36-8 Specific Properties Description
C language structure, function and pointer exercise (simple address book)
Enhanced Deep Residual Networks for Single Image Super-Resolution
随机推荐
温度响应性纳米水凝胶光子品体/纤维素修饰荧光水凝胶/载脂质体水凝胶的制备方法
Stanford CS143 Speed Pass PA1 Tutorial
【Django】缓存
Aptos 深度解读:机遇、挑战与风险
信息化和数字化的核心差异
Pagoda measurement - building LightPicture open source map bed system
c语言文件基本操作总结
改变社交与工作状态的即时通讯是什么呢?
即时通讯开发如何撸一个WebSocket服务器
无js实现弹出层效果
20220809-PotPlayer如何设置默认字体色-设置默认字体色的方法
鲜花线上销售管理系统的设计与实现
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
开发IM即时通讯容易吗?需要什么技术
【软考软件评测师】软件测试基础知识
Data storage - the C language
shell指定参数名传参
使用 GoogleTest 框架对 C 代码进行单元测试
数据建模已死,真的吗?
XSS高级 svg 复现一个循环问题以及两个循环问题