当前位置:网站首页>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);
}
}
}
}
边栏推荐
- R语言使用glm函数构建逻辑回归模型(logistic)、使用subgroupAnalysis函数进行亚组分析并可视化森林图
- XSS高级 svg 复现一个循环问题以及两个循环问题
- [转] Typora_Markdown_图片标题(题注)
- 跳房子游戏
- Characteristics of the (CAS:1527486-16-3TAMRA-azide-PEG3-Biotin) reaction in biotin azide!
- 芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长
- DHCP——动态主机配置协议
- -Chess game-
- 对修饰器的实验支持功能在将来的版本中可能更改。在 “tsconfig“ 或 “jsconfig“ 中设置 “experimentalDecorators“ 选项以删除此警告
- CAS:851113-28-5 (Biotin-ahx-ahx-tyramine)
猜你喜欢

Pyscript,创建一个能执行crud操作的网页应用

Docker interview question 2--get the number of database connections and docker-compose

小程序中计算距离信息

鲜花线上销售管理系统的设计与实现

Sikuli 基于图形识别的自动化测试技术

渗透测试与攻防对抗——漏洞扫描&逻辑漏洞(Part1)

惊掉你下巴,程序员编码竟然可以被 996 指数化

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

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

ASEMI整流桥GBJ1010参数,GBJ1010规格,GBJ1010封装
随机推荐
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
【LeetCode】求根节点到叶节点数字之和
DHCP——动态主机配置协议
unity编辑器扩展界面使用 List
为什么字符串一旦创建就不可以改变?
Solving for the number of mines
assert利用蚁剑登录
CAS:183896-00-6 (Biotin-PEG3-C3-NH2) PEG derivative
手把手教你编写性能测试用例
芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长
365 days challenge LeetCode1000 questions - Day 052 Step by step summation to get the minimum value of positive numbers Greedy
2022金九银十工作潮,怎么样才能成功跳槽面试拿到高薪呢?
开发IM即时通讯容易吗?需要什么技术
【Grpc】报错:status = StatusCode.UNIMPLEMENTED details = ““
FITC标记生物素(FITC-生物素|CAS:134759-22-1)有哪些知识了?
eyb:Redis学习(4)
跳房子游戏
字符统计柱状图
鲜花线上销售管理系统的设计与实现
Stanford CS143 速通PA1教程