PAT20181209 甲级 7-2&&7-3

7-2 Decode Registration Card of PAT

 分数16/25

部分答案正确

三种情况分别考虑,用list存储,再用Collections 进行相关排序。



import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	public static class Stu{
		String allInfo;
		String level;
		String siteNum;
		String testData;
		String  testeeNumber;
		int score;
	}
	public static class type3{
		String num;
		int cnt;
	}
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		int M=sc.nextInt();
		Stu[] stu=new Stu[N];
		
		for(int i=0;i<N;i++){
			stu[i]=new Stu();
			String s=sc.next();
			char[] ch=s.toCharArray();
			stu[i].allInfo=s;
			stu[i].level=String.valueOf(ch[0]);
			stu[i].siteNum=String.valueOf(ch[1])+String.valueOf(ch[2])+String.valueOf(ch[3]);
			stu[i].testData=String.valueOf(ch[4])+String.valueOf(ch[5])+String.valueOf(ch[6])+String.valueOf(ch[7])+String.valueOf(ch[8])+String.valueOf(ch[9]);
			stu[i].testeeNumber=String.valueOf(ch[10])+String.valueOf(ch[11])+String.valueOf(ch[12]);
			stu[i].score=sc.nextInt();					
		}
		
		
		for(int i=0;i<M;i++){
			int typeNum=sc.nextInt();
			String content=sc.next();
			System.out.println("Case "+(i+1)+": "+typeNum+" "+content);
			ArrayList<Stu> list=new ArrayList<Stu>();
			if(typeNum==1){
				for(int j=0;j<N;j++){
					if(stu[j].level.equals(content))
						list.add(stu[j]);
				}
				if(list.size()>0){
					Collections.sort(list, new Comparator<Stu>() {
						public int compare(Stu s1,Stu s2){
							int socre1=s1.score;
							int score2=s2.score;
							if(socre1>score2)
								return -1;
							else if(socre1==score2){
								if(s1.allInfo.compareTo(s2.allInfo)>0)
									return 1;
								else return -1;
							}
							else
								return 1;
						}
						
					});
				}
				for(int m=0;m<list.size();m++){
					System.out.println(list.get(m).allInfo+" "+list.get(m).score);
				}
				
			}
			else if(typeNum==2){
				int sum=0;
				for(int j=0;j<N;j++){
					if(stu[j].siteNum.equals(content)){
						list.add(stu[j]);
						sum+=stu[j].score;
					}
						
				}
				if(list.size()>0){
					System.out.println(list.size()+" "+sum);				
				}
				
				
			}
			else if(typeNum==3){
				
				for(int j=0;j<N;j++){
					if(stu[j].testData.equals(content)){
						list.add(stu[j]);
					}
						
				}
				ArrayList<type3> tem=new ArrayList<type3>();
				for(int m=0;m<list.size();m++){
					String ss=list.get(m).siteNum;
					int cnt=0;
					for(int k=0;k<list.size();k++){
					   if(list.get(k).siteNum.equals(ss))
						   cnt++;
					}
					boolean f=false;
					for(int k=0;k<tem.size();k++){
						   if(tem.get(k).num.equals(ss))
							   {
							    f=true;break;
							   }
						}
					if(!f){
						type3 t=new type3(); 
						   t.cnt=cnt;
						   t.num=ss;
						   tem.add(t);
					}
					

				}
				Collections.sort(tem, new Comparator<type3>() {

					@Override
					public int compare(type3 t1, type3 t2) {
						// TODO Auto-generated method stub
						 if(t1.cnt>t2.cnt)
							 return -1;
						 else if(t1.cnt==t2.cnt){
							 if(t1.num.compareTo(t2.num)>0)
								 return 1;
							 else 
								 return -1;
						 }
						 else
							 return 1;
					}
					});
					
					for(int k=0;k<tem.size();k++){
						System.out.println(tem.get(k).num+" "+tem.get(k).cnt);
					}
			}
			if(list.size()==0)
			System.out.println("NA");
		
		
		}	
	}

	
}

7-3 Vertex Coloring

分数15/25

部分答案正确

只要存在一边的两端点,存在相同颜色 即No。没有用的深搜,建二维数组存储点与点之间的关系。





import java.util.ArrayList;
import java.util.Scanner;

public class Main{
	public static int findDif(int color[]){
		ArrayList<Integer> list=new ArrayList<Integer>();
		for(int i=0;i<color.length;i++){
			boolean flag=true;
			for(int j=0;j<list.size();j++){
				if(color[i]==list.get(j)){
					flag=false;
					break;
				}
			}
				
			if(flag)
					list.add(color[i]);
		}
		
		return list.size();
	}
	 public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int V=sc.nextInt();
		int E=sc.nextInt();
		int[][] graph=new int[V][V];
		
		for(int i=0;i<V;i++){			
			for(int j=0;j<V&&i!=j;j++){
				graph[i][j]=0;
			}
		}
		
		for(int i=0;i<E;i++){
			int e1=sc.nextInt();
			int e2=sc.nextInt();
			graph[e1][e2]=1;
			graph[e2][e1]=1;			
		}
		int K=sc.nextInt();
		for(int i=0;i<K;i++){
			int[] color=new int[V];
			for(int j=0;j<V;j++){
				color[j]=sc.nextInt();
			}

			boolean flag=true;
			for(int m=0;m<V;m++){				
				for(int j=0;j<V;j++){
					if(graph[m][j]==1&&color[m]==color[j]){
							flag=false;
							System.out.println("No");
							break;
						
					}
				}
				
				if(!flag)
					break;
							
			}
			if(flag){
				int n=findDif(color);
				System.out.println(n+"-coloring");
			}
		
		}	
	}
}

这两道题,不知道什么情况没有考虑到,望指教!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值