当前位置:网站首页>彩色袜子题
彩色袜子题
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);
}
}
}
}
边栏推荐
- 以太网PHY芯片LAN8720A芯片研究
- 微信账户体系科普:什么是UnionId、OpenId与wxopenid?
- 基于SSM实现手机销售商城系统
- 小程序实现搜索功能续
- Fedora 36 dnf 安装ModSecurity和 OWASP 核心规则集
- 为什么字符串一旦创建就不可以改变?
- GB28181 sip和RTSP(Real-Time Streaming Protocol)实时流控制协议
- 【CAS:41994-02-9 |Biotinyl tyramide】生物素基酪氨酰胺价格
- 你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
- Minimum number of steps to get out of the maze 2
猜你喜欢

ITK编译remote库

Next.js获取路由参数及styled-jsx 的使用

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

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

以太网PHY芯片LAN8720A芯片研究

Penetration Testing and Offensive and Defense Confrontation - Vulnerability Scanning & Logic Vulnerability (Part1)

微服务【同步和异步通讯详解】第6章

这一次,话筒给你:向自由软件之父 Richard M. Stallman 提问啦!

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

Enhanced Deep Residual Networks for Single Image Super-Resolution
随机推荐
初步认识对象
数据建模已死,真的吗?
【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price
【毕业设计】基于ESP32的在线墨水屏桌面摆件 -物联网 单片机 嵌入式
-向量点积-
小程序实现搜索功能续
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
分析 20 个 veToken 生态系统协议 这种代币模型为何受欢迎?
egg.js中Class constructor BaseContextClass cannot be invoked without ‘new‘解决方法
What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?
【kali-密码攻击】(5.2.1)密码分析:Hash Identifier(哈希识别)
c语言结构体、函数以及指针练习(简单通讯录)
CAS:851113-28-5 (Biotin-ahx-ahx-tyramine)
使用 apxs 构建和安装 Apache 扩展共享对象模块
【Grpc】报错:status = StatusCode.UNIMPLEMENTED details = ““
Mysql database ALTER basic operations
How to activate the payment function on WeChat official account?
微服务【同步和异步通讯详解】第6章
Interlay集成至Moonbeam,为网络带来interBTC和INTR
宽带由20M换为100M