当前位置:网站首页>彩色袜子题
彩色袜子题
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);
}
}
}
}
边栏推荐
猜你喜欢

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

【毕业设计】 基于Stm32的家庭智能监控系统 - 单片机 图像识别 人体检测 AI

GBJ1510-ASEMI机器人电源整流桥GBJ1510

Quick responsiveness intelligent/smart responsiveness of polyethylene glycol type nano/reduction response hydrogels research and preparation

【毕业设计】基于ESP32的在线墨水屏桌面摆件 -物联网 单片机 嵌入式

即时通讯开发如何撸一个WebSocket服务器

跳房子游戏

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

How to turn off system protection in Win11?How to turn off the system protection restore function?

ASEMI整流桥GBJ1010参数,GBJ1010规格,GBJ1010封装
随机推荐
365天挑战LeetCode1000题——Day 052 逐步求和得到正数的最小值 贪心
C语言头文件组织与包含原则
数据建模已死,真的吗?
头脑风暴:单词拆分
DHCP——动态主机配置协议
assert利用蚁剑登录
-Pickling peanuts-
信息化和数字化的核心差异
跳房子游戏
Prometeus 2.31.0 新特性
基于SSM实现手机销售商城系统
-向量点积-
shell指定参数名传参
C语言--数据的存储(上)
有PEG-Biotin参与的(CAS:1778736-18-7)Biotin-PEG4-OH广泛用于分子靶点检测
Fedora 36 dnf 安装ModSecurity和 OWASP 核心规则集
Quick responsiveness intelligent/smart responsiveness of polyethylene glycol type nano/reduction response hydrogels research and preparation
y92.第六章 微服务、服务网格及Envoy实战 -- Envoy基础(三)
Data storage - the C language
Kubernetes YAML编写 讲解