找老乡 参考代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
List<String> l1= new ArrayList<String>();
int n;
String str;
n=in.nextInt();
str=in.next();
for(int i=0;i<n;i++)
{
String s=in.next();
String p=in.next();
if(str.equals(p))
l1.add(s);
}
for(String q:l1)
{
System.out.println(q);
}
System.out.println(l1.size());
}
}
}
Q题 方法一
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
class Girl{
String name;
int height;
int age;
String tele;
public Girl(String name,int height,int age,String tele) {
this.name=name;
this.height=height;
this.age=age;
this.tele=tele;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + height;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((tele == null) ? 0 : tele.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {//判断某个对象是否在集合里面 判断当前对象与参数对象是否相同
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())//类型不同
return false;
Girl other = (Girl) obj;//强制类型转换
if (age != other.age)
return false;
if (height != other.height)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (tele == null) {
if (other.tele != null)
return false;
} else if (!tele.equals(other.tele))
return false;
return true;//把上述情况排除
}
@Override
public String toString() {
return name+" "+height+" "+age+" "+tele;
}
}
public class Main{
public static void main(String[] args){
Scanner reader=new Scanner(System.in);
int n=reader.nextInt();
int a=reader.nextInt();//高度下限
int b=reader.nextInt();//上限
int c=reader.nextInt();//年龄下限
int d=reader.nextInt();
List<Girl> list=new ArrayList<Girl>();//list集合 类型为girl类型
for(int i=0;i<n;i++)
{
String name=reader.next();
int height=reader.nextInt();
int age=reader.nextInt();
String tele=reader.next();
if(height>=a&&height<=b&&age>=c&&age<=d)
{
Girl girl=new Girl(name,height,age,tele);//
if(!list.contains(girl))//如果集合里面没有就加入
{
list.add(girl);
}
}
}
Collections.sort(list, new Comparator<Girl>() {//排序
@Override
public int compare(Girl o1, Girl o2) {
int r1=o1.height-o2.height;//按照身高从低到高排序 升序
if(r1!=0) {
return r1;
}
else
{
return o2.age-o1.age;//年龄从高到低
}
}
});
System.out.println(list.size());
for(Girl girl:list) {
System.out.println(girl);
}
reader.close();
}
}
Q题方法二:
import java.util.*;
class Girl
{
String name;
int height;
int age;
String telephone;
public Girl(String name, int height, int age, String telephone) {
super();
this.name = name;
this.height = height;
this.age = age;
this.telephone = telephone;
}
@Override
public String toString() {
return name + " " + height + " " + age + " " + telephone;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + height;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((telephone == null) ? 0 : telephone.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Girl other = (Girl) obj;
if (age != other.age)
return false;
if (height != other.height)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (telephone == null) {
if (other.telephone != null)
return false;
} else if (!telephone.equals(other.telephone))
return false;
return true;
}
}
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int d = in.nextInt();
Set<Girl> set = new HashSet<Girl>();
for(int i = 0; i < n; i++)
{
String name = in.next();
int height = in.nextInt();
int age = in.nextInt();
String telephone = in.next();
if(height >= a && height <= b && age >= c && age <= d)
{
Girl girl = new Girl(name, height, age, telephone);
set.add(girl);
}
}
ArrayList<Girl> list = new ArrayList<Girl>(set);
Collections.sort(list, new Comparator<Girl>() {
@Override
public int compare(Girl o1, Girl o2) {
int r1 = o1.height - o2.height;
if(r1 != 0)
{
return r1;
}
else
{
return o2.age - o1.age;
}
}
});
System.out.println(list.size());
for(Girl girl:list)
{
System.out.println(girl);
}
in.close();
}
}
java第三章
最新推荐文章于 2021-12-13 11:35:21 发布