2021级-JAVA09 集合框架

7-1 sdut-List--C~K的班级(I)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        ArrayList<String> list = new ArrayList<String>();
        int n = cin.nextInt();
        String s = cin.nextLine();
        while(n -- != 0)
        {
            s = cin.nextLine();
            if(list.contains(s))
                continue;
            list.add(s);
        }
        System.out.println(list.size());
        for(int i = 0; i < list.size(); i ++)
            System.out.println(list.get(i));
    }
}

7-2 sdut-Collection-sort--C~K的班级(II)

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        ArrayList<String> list = new ArrayList<String>();
        int n = cin.nextInt();
        String s = cin.nextLine();
        while(n -- != 0)
        {
            s = cin.nextLine();
            if(list.contains(s))
                continue;
            list.add(s);
        }
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.parseInt(o1.substring(0, 4)) - Integer.parseInt(o2.substring(0, 4));
            }
        });
        System.out.println(list.size());
        for(int i = 0; i < list.size(); i ++)
            System.out.println(list.get(i));
    }
}

 也可以

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        Map<String, String> map = new TreeMap<String, String>();
        cin.nextLine();
        for(int i = 0; i < n; i ++)
        {
            String s = cin.nextLine();
            String t = s.substring(0, 4);
            map.put(t, s);
        }
        System.out.println(map.size());
        for(String it : map.keySet())
        {
            System.out.println(map.get(it));
        }
    }
}

7-3 sdut-Map-Sort--C~K的班级(III)

import java.util.*;

class Student {
	String id,name;
	int age;
	char sex;
	
	public Student(String id, String name, int age, char sex) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return id+" "+name+" "+age+" "+sex;
	}
	
}

public class Main {
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		Map<String,Student> map = new HashMap<String,Student>();
		Student stu;
		String id;
		
		int n = input.nextInt();
		while(n-->0) {
			id = input.next();
			stu = new Student(id,input.next(),input.nextInt(),input.next().charAt(0));
			map.put(id, stu);
		}
		
		List<String> list = new ArrayList<String>(map.keySet());
		Collections.sort(list);
		
		System.out.println(map.size());
		Iterator<String> it = list.iterator();
		while(it.hasNext())
			System.out.println(map.get(it.next()));
		
		input.close();
	}
}

亦可以

import java.util.*;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        cin.nextLine();
        Map<String, String> map = new TreeMap<String, String>();
        for(int i = 1; i <= n; i ++)
        {
            String s = cin.nextLine();
            String t = s.substring(0, 4);
            map.put(t, s);
        }
        System.out.println(map.size());
        for(String it : map.keySet())
        {
            System.out.println(map.get(it));
        }
    }
}

 

7-4 sdut-LinkedHashMap--C~K的班级(IV)

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        String s = cin.nextLine();
        while(n -- != 0)
        {
            String id = cin.nextLine();
            String a = id.substring(0, 4);
            String b = id.substring(4);
            map.put(a, b);
        }
        System.out.println(map.size());
        for(String it : map.keySet())
        {
            System.out.println(it + map.get(it));
        }
    }
}

7-5 sdut-集合相等问题

import java.util.*;
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        Set<Integer> set1=new HashSet<Integer>();
        Set<Integer> set2=new HashSet<Integer>();        for(int i=0;i<n;i++) {
            int x=sc.nextInt();
            set1.add(x);
        }
        for(int i=0;i<n;i++) {
            int x=sc.nextInt();
            set2.add(x);
        }
        if(set1.containsAll(set2)&&set2.containsAll(set1)) {
            System.out.println("YES");
        }
        else {
            System.out.println("NO");
        }
    }
}

7-6 sdut-Collection-sort--C~K要找女朋友了!

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            int n = cin.nextInt();
            int a = cin.nextInt();
            int b = cin.nextInt();
            int c = cin.nextInt();
            int d = cin.nextInt();
            List<Girl> list = new ArrayList<Girl>();
            for(int i = 0; i < n; i ++)
            {
                Girl girl = new Girl(cin.next(), cin.nextInt(), cin.nextInt(), cin.next());
                if(girl.hh >= a && girl.hh <= b && girl.age >= c && girl.age <= d && !list.contains(girl))
                        list.add(girl);
            }
            list.sort(new Comparator<Girl>() {
                @Override
                public int compare(Girl o1, Girl o2) {
                    if(o1.hh == o2.hh)
                        return o2.age - o1.age;
                    return o1.hh - o2.hh;
                }
            });
            System.out.println(list.size());
            for(Girl it : list)
                System.out.println(it);
        }
    }
}
class Girl
{
    String name;
    int hh, age;
    String phone;

    public Girl(String name, int hh, int age, String phone) {
        this.name = name;
        this.hh = hh;
        this.age = age;
        this.phone = phone;
    }

    @Override
    public boolean equals(Object obj) {
        Girl g = (Girl) obj;
        if(!name.equals(g.name))
            return false;
        if(!phone.equals(g.phone))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return name + " " + hh + " " + age + " " + phone;
    }
}

7-7 sdut-Map(dict)-String--单词和字符鉴别器

import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class Main {
    // Create a HashMap to store words and the count
    static HashMap<String, Integer> wordsMap = new HashMap<String, Integer>();
    // Create a HashMap to store characters and the count
    static HashMap<Character, Integer> charMap = new HashMap<Character, Integer>();
    public static void main(String[] args) {
        loadData();
        printReport();

    }
    private static void loadData() {

        Scanner fin = new Scanner(System.in);
        while (fin.hasNext()) {
            String word = fin.next().toUpperCase();
            if("0000".equals(word))
            {
                break;
            }
            // System.out.println(word);
            Integer count =wordsMap.getOrDefault(word, 0);
            wordsMap.put(word, count + 1);
            int len = word.length();
            for (int i = 0; i < len; i++) {
                char ch = word.charAt(i);
                Integer count1 =charMap.getOrDefault(ch, 0);
                charMap.put(ch, count1 + 1);
            }
        }
        fin.close();
    }
    private static void printReport() {
        int maxCount = 0;
        String maxWord = null;

        int minCount = wordsMap.size();
        String minWord = null;
        Set<String> words = wordsMap.keySet();
        for (String word : words) {
            int count = wordsMap.get(word);
            if (count > maxCount) {
                maxCount = count;
                maxWord = word;
            }
            else if(count==maxCount)
            {
                if(word.compareTo(maxWord)>0)
                {
                    maxWord = word;
                }
            }

            if (count < minCount) {
                minCount = count;
                minWord = word;
            }else if(count==minCount)
            {
                if(word.compareTo(minWord)<0)
                {
                    minWord = word;
                }
            }
        }
        System.out.printf("%-10s%8d\n", maxWord, maxCount);
        System.out.printf("%-10s%8d\n", minWord, minCount);

        for (char c : "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray()) {
            Integer charCount = charMap.get(c);
            if (charCount == null) {
                charCount = 0;
            }
            System.out.printf("%-10c%8d\n", c, charCount);
        }
    }
}

7-8 sdut-Collection(Map)-1 读中国载人航天史,汇航天员数量,向航天员致敬

import java.util.*;
import java.util.Map.Entry;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        Map<String, Integer> map = new TreeMap<String, Integer>();
        while(true)
        {
            String s = cin.next();
            if(s.equals("end")) break;
            if(map.containsKey(s))
            {
                map.put(s, map.get(s) + 1);
            }
            else
            {
                map.put(s, 1);
            }
        }
        Iterator it = map.entrySet().iterator();
        while(it.hasNext())
        {
            Entry ex = (Entry)it.next();
            System.out.println(ex.getKey() + " " + ex.getValue());
        }

    }
}

7-9 sdut-Collection(List)-2 读中国载人航天史,汇航天员数量,向航天员致敬

import java.util.*;
import java.util.Map.Entry;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        List<String> list = new ArrayList<String>();
        while(true)
        {
            String s = cin.next();
            if(s.equals("end")) break;
            if(list.contains(s))
                continue;
            else
            {
                list.add(s);
                System.out.println(s);
            }
        }
    }
}

7-10 sdut-String+array1(Collection-Map)-1 读中国载人航天史,汇航天员数量,向航天员致敬(字符串+数组、LinkedMap )

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        String[] c = new String[100];
        int n = 0, i;
        int[] t = new int[100];
        while(true)
        {
            int flag = 0;
            String s = cin.next();
            if(s.equals("end")) break;
            for(i = 0; i < n; i ++)
            {
                if(c[i].equals(s))
                {
                    flag = 1;
                    break;
                }
            }
            if(flag == 1)
            {
                t[i] ++;
            }
            else
            {
                t[n] = 1;
                c[n] = s;
                n ++;
            }
        }
        for(i = 0; i < n; i ++)
            System.out.println(c[i] + " " + t[i]);
    }

}

7-11 sdut-List-Map--购物车

import java.rmi.StubNotFoundException;
import java.util.Scanner;
import java.util.*;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        cin.nextLine();
        Set<Shop> my = new TreeSet<Shop>();
        Shop[] shops = new Shop[n];
        for(int i = 0; i < n; i ++)
        {
            String now[] = cin.nextLine().split("\\s+");
            shops[i] = new Shop(Integer.parseInt(now[0]), now[1], now[2], Double.parseDouble(now[3]), Integer.parseInt(now[4]), 0);
        }
        int m = cin.nextInt();
        while(m -- != 0)
        {
            int x = cin.nextInt();
            x --;
            int y = cin.nextInt();
            int z = cin.nextInt();
            if(z==1) {
                if(y > 0)
                {
                    int a = shops[x].save;
                    int b = shops[x].buy;
                    shops[x].save = Math.max(0, a - y);
                    shops[x].buy = Math.min(b + y, b + a);
                    my.add(shops[x]);
                }
            }
            else if(z==2) {
                if(y > 0)
                {
                    int a = shops[x].save;
                    int b = shops[x].buy;
                    shops[x].save += Math.min(y, b);
                    shops[x].buy = Math.max(0, b - y);
                    my.add(shops[x]);
                }
            }
            else {
                if(shops[x].buy>0) {
                    shops[x].save+=shops[x].buy;
                    shops[x].buy=0;
                    my.remove(shops[x]);
                }

            }
        }
        int cnt = 0;
        for(Shop it : my)
        {
            if(it.buy != 0) cnt ++;
        }
        System.out.println(cnt);
        double s = 0;
        for(Shop it : my)
        {
            if(it.buy != 0)
            {
                System.out.println(it.myshop());
                s += it.price*it.buy;
            }
        }
        System.out.printf("%.2f\n", s);
        System.out.println(shops.length);
        for(Shop it : shops)
            System.out.println(it.show());
    }
}
class Shop implements Comparable<Shop>
{
    int id;
    String name;
    String store;
    double price;
    int save;
    int buy;

    public Shop(int id, String name, String store, double price, int save, int buy) {
        this.id = id;
        this.name = name;
        this.store = store;
        this.price = price;
        this.save = save;
        this.buy = buy;
    }
    public String myshop() {
        return id + " " + name + " " + store + " " + String.format("%.2f", price) + " "+ save + " " + buy;
    }
    public String show() {
        return id + " " + name + " " + store + " " + String.format("%.2f", price) + " "+ save;
    }

    @Override
    public int compareTo(Shop o) {
        return id - o.id;
    }
}

7-12 sdut-Colleciton-5 学生信息的添加与查询(HashMap)

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

class Student
{
    String id, name, birth;
    double score;

    public Student(String id, String name, String birth, double score) {
        this.id = id;
        this.name = name;
        this.birth = birth;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student [" +
                "id=" + id +
                ", name=" + name +
                ", birthday=" + birth +
                ", score=" + score +
                ']';
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        Map<String, Student> stu = new TreeMap<String, Student>();
        for(int i = 0; i < n; i ++)
        {
            String id = cin.next();
            String name = cin.next();
            String birth = cin.next();
            String[] temp = birth.split("-");
            birth = temp[0] + "年" + temp[1] + "月" + temp[2] + "日";
            double score = cin.nextDouble();
            Student s = new Student(id, name, birth, score);
            if(stu.containsKey(id))
            {
                stu.replace(id, s);
            }
            else stu.put(id, s);
        }
        for(Map.Entry<String, Student> entry : stu.entrySet())
        {
            System.out.println(entry.getValue().toString());
        }
    }
}

7-13 list 存储动物对象

import java.util.*;
class Animal{
	String name;
	String age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Animal [name=" + name + ", age=" + age + "]";
	}
	public String play() {
		return "Animal";
	} 

}
class Bird extends Animal{
	public Bird(String s1,String s2) {
		name=s1;
		age=s2;
	}

	@Override
	public String toString() {
		return "Bird[name=" + name + ", age=" + age + "]";
	}
	public String play() {
		return "Bird";
	}
}

class Dog extends Animal{
	String leg;
	public Dog(String s1,String s2,String s3) {
		name=s1;
		age=s2;
		leg=s3;
	}
	public String getLeg() {
		return leg;
	}
	public void setLeg(String leg) {
		this.leg = leg;
	}
	@Override
	public String toString() {
		return "Dog[leg=" + leg + ", name=" + name + ", age=" + age + "]";
	}
	public String play() {
		return "Dog";
	}   
}

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		String s1,s2,s3;
		LinkedList<Animal> lk=new LinkedList<Animal>();
		for(int i=0;i<n;i++) {
		   int k=sc.nextInt();
		   if(k==1) {
			   s1=sc.next();
			   s2=sc.next();
			   s3=sc.next();
			   Dog d=new Dog(s1,s2,s3);
			   lk.add(d);
		   }else {
			   s1=sc.next();
			   s2=sc.next();
			   Bird d=new Bird(s1,s2);
			   lk.add(d);
		   }
		}
		
		System.out.printf("[");  
       for(int i=0;i<n;i++) {
    	   if(i==0) {
    		 System.out.printf(lk.get(i).toString());  
    	   }else
    	   {
    		   System.out.printf(", "+lk.get(i).toString());  
    	   } 	   
    	   
       }
       System.out.println("]");  
       int i;
       s1=sc.next();
       for(i=0;i<n;i++) {
    	  // System.out.println(lk.get(i).getName().length()); 
    	  // System.out.println(s1.length());
    	   if(s1.equals(lk.get(i).getName())) {
    		   System.out.println(lk.get(i).play());  
    		   System.out.printf(lk.get(i).toString()); 
    		   break;
    	   }
       }
       if(i==n) {
    	   System.out.print("no this one");
       }
	}

}

7-14 编程题:选修课成绩统计问题

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str[] = sc.nextLine().split(",");
        String str1[][] = new String[str.length][3];
        for (int i = 0; i < str.length; i++) {
            str1[i] = str[i].split(" ");
        }
        for (int i = 0; i < str.length; i++) {
            int grade = 0;
            for (int j = 0; j < 3; j++) {
                switch (str1[i][j]) {
                    case "A":
                        grade += 5;
                        break;
                    case "B":
                        grade += 4;
                        break;
                    case "C":
                        grade += 3;
                        break;
                    case "D":
                        grade += 2;
                        break;
                    case "E":
                        grade += 1;
                        break;
                }
            }
            if (i == str.length - 1)
                System.out.println(str1[i][0] + " " + grade);
            else
                System.out.print(str1[i][0] + " " + grade + ",");
        }
    }
}

7-15 sdust-Java-学生成绩读取与排序

import java.util.*;
import java.lang.*;
import java.io.*;

class Student implements Comparable<Student>{
	String name;
	String id;
	String course;
	int score;
	
	int num=1;
	public Student(String a,String b,String c,int d) {
		name=a;
		id = b;
		course = c;
		score = d;
	}
	public void getSum(int s) {
		num++;
		this.score+=s;
		
	}
	public double getScore() {
		return score/num;
	}
	public int compareTo(Student o) {
		if(this.getScore()==o.getScore())
			return id.compareTo(o.id);
		else return (int)(o.getScore()-getScore());
	}
	
}

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		List<Student> list = new ArrayList<>();
	   while(sc.hasNext()) {
		   String line = sc.next();
		   if(line.equals("exit"))
			   break;
		   String []stu = line.split(",");
		   int flag=0;
		   for(int i=0;i<list.size();i++) {
			   if(list.get(i).id.compareTo(stu[1])==0) {
				   list.get(i).getSum(Integer.parseInt(stu[3]));
				   flag=1;
				   break;
			   }
		   }
		   if(flag==0) {
			   Student a = new Student(stu[0],stu[1],stu[2],Integer.parseInt(stu[3]));
			   list.add(a);
		   }
		   
	   }
	   for(int i=0;i<list.size();i++) {
		   list.get(i).getScore();
	   }
	   Collections.sort(list);
	   for(int i=0;i<list.size();i++) {
		   System.out.println("No"+(i+1)+":"+list.get(i).id+","+list.get(i).name);
	   }
	}
}

  • 1
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值