类型
选择30道,都是计算机基础。。。。
编程题俩
1、给定6组平面的长宽,问能否组成长方体。
如:
输入:
1345 2584
2584 683
2584 1345
683 1345
683 1345
2584 683
每行表示每个长方形的长或宽
输出:POSSIBLE
表示可以组成
思路
- 自己想了老半天没想出好法子。
- 就想着,能找出几种长方形,如果种类数超过3,肯定不行。
- 如果等于三种,长方体最起码要求对面是一样的,所以一定要让每种有2个面。才有可能成。
- 编写函数 triPossible(),判断三种平面,能否构成长方体。随便拿出一个平面记作a的一个边记作a.w,在其他两个平面要找到长度相同的边,比如b.h。那么剩下的 a.h 和 b.w组成的长方形一定要和剩下的平面c匹配才行。
- 对于只有两种平面的情况,只有 (a,a)2个,(a,b) 4个 ,才可能行。对于这种情况,转化成三种平面的情况。
- 对于只有一种平面的情况。也就是要形成立方体,那么只有是正方形才能行。
package jd;
import java.util.*;
public class Jd {
/*请完成下面这个函数,实现题目要求的功能
当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
******************************开始写代码******************************/
static boolean isPossible(ArrayList<Surface> data) {
HashMap<Surface,Integer> pair = new HashMap<>();
for (int i = 0; i < data.size(); i++) {
//获取第i个平面
Surface surface = data.get(i);
if(pair.containsKey(surface)){
pair.put(surface,pair.get(surface)+1);
}else {
pair.put(surface,1);
}
}
//六个面如果 有三种以上的平面 肯定不行
if(pair.size() > 3)
{
return false;
} else if(pair.size() == 1){
//如果所有平面一样 那么平面的长宽要一样
Set<Surface> surfaces = pair.keySet();
for(Surface s:surfaces){
if(s.w == s.h){
return true;
}else {
return false;
}
}
}else if(pair.size() == 2){
//如果有两种尺寸的平面 只能是 (a,a) 2个面 (a,b) 四个面 才能拼成立方体
Set<Surface> surfaces = pair.keySet();
ArrayList<Surface> list = new ArrayList<>();
for(Surface s :surfaces){
if((pair.get(s) == 4)||(pair.get(s) == 2)){
if(pair.get(s) == 4){
list.add(s);
list.add(s);
}else {
list.add(s);
}
}else{
//不合理
return false;
}
}
return triPossible(list);
}else if(pair.size() == 3){
//三种类型的平面 每个类型 要有两个 才有可能行
Set<Surface> surfaces = pair.keySet();
ArrayList<Surface> list = new ArrayList<>();
for(Surface s :surfaces){
if( pair.get(s)!= 2){
return false;
}
list.add(s);
}
return triPossible(list);
}
return false;
}
//判断三个平面能否匹配
static boolean triPossible(ArrayList<Surface> data){
if(data.size() != 3){return false;}
//data 前提是长度3
int h0 = data.get(0).h;
int w0 = data.get(0).w;
if(h0 ==data.get(1).h){
Surface surface = new Surface(w0, data.get(1).w);
return surface.equals(data.get(2));
}else if(h0 == data.get(1).w){
Surface surface = new Surface(w0, data.get(1).h);
return surface.equals(data.get(2));
}else if (h0 == data.get(2).h){
Surface surface = new Surface(w0, data.get(2).w);
return surface.equals(data.get(1));
}else if(h0 == data.get(2).w){
Surface surface = new Surface(w0, data.get(2).w);
return surface.equals(data.get(1));
}
return false;
}
/******************************结束写代码******************************/
public static void main(String[] args){
/*
* 2
*
1345 2584
2584 683
2584 1345
683 1345
683 1345
2584 683
POSSIBLE
1234 4567
1234 4567
4567 4321
4322 4567
4321 1234
4321 1234
IMPOSSIBLE
* */
Scanner in = new Scanner(System.in);
ArrayList<Surface> data = new ArrayList<>();
for(int j = 0;j <6;j++){
Surface temp = new Surface(in.nextInt(),in.nextInt());
data.add(temp);
}
boolean possible = isPossible(data);
System.out.println(possible?"POSSIBLE":"IMPOSSIPLE");
}
}
class Surface{
//h长 数大的
int h;
//w宽
int w;
Surface(int h,int w){
if(h > w){
this.h= h;
this.w = w;
}else {
this.w= h;
this.h = w;
}
}
private Surface(){}
@Override
public boolean equals(Object Surface)
{
if(this == Surface)
{
return true;
}
if(!(Surface instanceof Surface))
{
return false;
}
Surface pn = (Surface)Surface;
return pn.w == w && pn.h == h;
}
@Override
public int hashCode()
{
return w*h;
}
}
2、列车最少座位数,,,没懂。。。。