当前位置:网站首页>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);
}
}
}
}
边栏推荐
- 20220809-PotPlayer如何设置默认字体色-设置默认字体色的方法
- pyhton之问~~~~~if __name__ == ‘__main__‘:是什么?
- -Chess game-
- 分析 20 个 veToken 生态系统协议 这种代币模型为何受欢迎?
- Docker 面试题2则--取数据库连接数和docker-compose
- 走出迷宫的最少步数2
- 【Swoole系列3.5】进程池与进程管理器
- R语言使用glm函数构建逻辑回归模型(logistic)、使用subgroupAnalysis函数进行亚组分析并可视化森林图
- Character Statistics Histogram
- 嵌入式Qt-实现两个窗口的切换
猜你喜欢

C# 四舍五入 MidpointRounding.AwayFromZero

【kali-密码攻击】(5.1.2)密码在线破解:Medusa

Xi'an biotin-tetrapolyethylene glycol-amide-4phenol light yellow semi-solid

365天挑战LeetCode1000题——Day 052 逐步求和得到正数的最小值 贪心

Web性能测试模型小结

嵌入式Qt-实现两个窗口的切换

宝塔实测-搭建LightPicture开源图床系统

GB28181 sip和RTSP(Real-Time Streaming Protocol)实时流控制协议

万字总结:分布式系统的38个知识点

OSS-访问oss生成的url无法访问,直接下载问题
随机推荐
CAS:851113-28-5 (生物素-ahx-ahx-酪胺)
高校就业管理系统设计与实现
Biotin-Cy2 Conjugate,生物素-Cy2 偶联物_Cy2 生物素偶联物
Pyscript,创建一个能执行crud操作的网页应用
Win11怎么关闭系统保护功能?系统保护还原功能怎么关闭?
-象棋比赛-
mstsc/Mstsc (Microsoft terminal services client)远程桌面连接
鲜花线上销售管理系统的设计与实现
【无标题】
【软考软件评测师】软件测试基础知识
Stanford CS143 Speed Pass PA1 Tutorial
@PostConsturct注解作用及特点
以太网PHY芯片LAN8720A芯片研究
微信小程序tab切换时保存checkbox状态
-Pickling peanuts-
【ROS2原理10】Interface数据的规定
宝塔实测-搭建LightPicture开源图床系统
【报错】ModuleNotFoundError: No module named ‘scp‘
生物素叠氮化物中的(CAS:1527486-16-3TAMRA-azide-PEG3-Biotin)反应的特点!
20220809-PotPlayer如何设置默认字体色-设置默认字体色的方法